Add a Git repository as a package using Composer for PHP

There isn’t much documentation on how to setup PHP libraries using Composer that are not in Packagist; one way to do this is as a package. If you specify the version number of the library (or other code) that you want you can fetch them from zip files, Subversion or Git. This way you don’t need to add any Composer files (such as composer.json) to the library itself, which is handy if they don’t support Composer yet.

{
    "name": "andrewkirkpatrick/sample",
    "description": "Sample project",
    "homepage": "http://www.andrew-kirkpatrick.co.uk",
    "repositories": [{
	"type": "package",
	"package": {
	    "name": "andrewkirkpatrick/library",
	    "version": "0.1.0",
	    "source": {
                "url": "git@server.co.uk:Repository.git",
	        "type": "git",
		"reference": "0.1.0"
	    }
        }
    }],
    "require": {
        "php": ">=5.3.3",
        "zendframework/zendframework": "2.*",
	"andrewkirkpatrick/library": "0.1.*"
    }
}

If you are using Git as above, the version number should correspond to a tag of the same name. Composer will try to clone using that tag.

Drawbacks of this is that you have to specify an exact version in composer.json and if you are updating the library also (unlike say using a specific version of WordPress) then you’ll need to tag every library update and update the project’s composer.json to get the changes.

9 comments

  • If you want to do it like Mike, you may reference the right branch: “reference”: “origin/master”

    An additional comment: If you fork a project which you usually resolve and update through packagist.org and now need to reference your fork you can easily do it like this:

    “repositories”: [{
    “type”: “vcs”,
    “url”: “git://github.com/you/repo.git”
    }],

    This will override the packagist repository.

  • thank for the post,

    to get the latest version from Git, I used for the reference the main branch: “master”

  • thanks for “If you are using Git as above, the version number should correspond to a tag of the same name. Composer will try to clone using that tag.”

  • @Andrew, thanks for this, very useful!

    and @Sven, your suggestion of “reference”: “origin/master” saved the day : )

    – Sean