Use Composer to install packages not on Packagist

Packagist and Composer are a great combination for installing the packages that we need for our projects (in my case for Laravel 4 project). For example you want to use Sentry 2 so you simply add "cartalyst/sentry": "2.0.*" as required package in your composer.json file. But what if we need a package that is not on Packagist? There are several ways but I will show you two solutions (and maybe add others later).

Installing package that has no composer.json file

In my case I wanted to add Jquery.Gantt package to my Laravel 4 project. This package is not listed on Packagist and doesn’t have its own composer.json file so you can’t just add "Jquery.Gantt": "1.0.*" under "require": in your composer.json file.

What we need to do is add some code to our composer.json file to tell Composer something about the new package,where to find it etc.

Just so you can imagine how this looks when it is all done, here is my entire composer.json file for one of my projects.Keep in mind that you only have to add what is highlighted in grey (lines 2-19 and line 26) to your composer.json file and not everything. The rest of the code is for my project and is included only so you can see where to put your code. Don’t forget to backup the original composer.json before doing this 🙂

  {
    "repositories": [
        {
            "type": "package",
            "package": {
                "name": "taitems/jQuery.Gantt",
                "version": "1.0",
                "dist": {
                    "url": "https://github.com/taitems/jQuery.Gantt/archive/master.zip",
                    "type": "zip"
                },
                "source": {
                    "url": "https://github.com/taitems/jQuery.Gantt.git",
                    "type": "git",
                    "reference": "dev-master"
                }
            }
        }
    ],
	"require": {
		"laravel/framework": "4.0.*",
		"cartalyst/sentry": "2.0.*",  
		"jasonlewis/expressive-date": "1.*",
		"opauth/opauth": "*",
		"opauth/facebook": "*",  
		"taitems/jQuery.Gantt": "1.0",  
		"way/generators": "dev-master"
	},
	"autoload": {
		"classmap": [
			"app/commands",
			"app/controllers",
			"app/models",
			"app/database/migrations",
			"app/database/seeds",
			"app/tests/TestCase.php"
		]
	},
	"scripts": {
		"post-update-cmd": "php artisan optimize"
	},
	"minimum-stability": "dev"
}

Lines 2-19: You have to change few things of course, depending on what package you are installing. Copy the Name from GitHub page (I took mine from https://github.com/taitems/jQuery.Gantt)). You can find both ZIP and .git URLs on GitHub page of that package. Version should be the package version number you want to use – if you are not sure then try * sign.

Line 26: You add this just like you would normaly add every dependency/package that is listed on Packagist.

That’s it. Just run php composer.phar update and your new package will be installed into /vendor/ folder, just like every other package/dependency.

Installing package that is not on Packagist but has composer.json file

I have not tried this yet but in case the package has composer.json file you can just use this code:

"repositories": [
    {
        "type": "vcs",
        "url": "https://github.com/taitems/jQuery.Gantt"
    }
]

It’s the same as in first example – you start from line 2, after the opening {. Do we are using VCS repository instead of the package repository. Composer will use the GitHub API to fetch the branch names and check if the version dev-soft-deleteable-products-disabled exists. If it does, it will clone the repository and check out said branch.

Again, run php composer.phar update and your new package should be installed into /vendor/ folder, just like every other package/dependency.

Note: VCS example will not work on the jQuery.Gantt package because this package has no composer.json file so you have to use the first method. VCS example is here only to demonstrate what to do when the package has composer.json file included.