Setting a Local Git repo in Composer

logo-composer-transparent3

So I’m working on a few libraries which a number of our applications depend on, and rather than having to commit to Bitbucket, update apps, checkout etc etc I wanted to find a way to set my composer file to use my local dev version of the library instead, and this is it.

It’s pretty easy to do really, you simply have to tell composer where your local repo is and then specify the branch etc you want to use…

1. Update composer.json to identify your local repo

Say my standard example composer.json file looks like this:

{
    "name": "My Application",
    "description": "My app which depends on my dev library",
    "require": {
        "myframework": "1.2.3",
        "mystuff/myamazinglibrary": "4.5.6"
    },
    "authors": [
        {
            "name": "my",
            "email": "my@email.com"
        }
    ],
    "minimum-stability": "dev",
    "autoload": {
        "psr-0": {
            "Stuff" : "src/"
        }
    },
    "repositories": [
        {
            "type": "git",
            "url": "git@bitbucket.org:mystuff/myamazinglibrary.git"
        }
    ]
}

If we run composer update this will install version 4.5.6 from our repo on Bitbucket as you would expect. What we now need to do though, is tell composer to use my local directory / repo as the one to get the version from…

If we make the following changes, you can see we have defined a path to the local repo, and also the branch name we want to use instead of version 4.5.6:

{
    "name": "My Application",
    "description": "My app which depends on my dev library",
    "require": {
        "myframework": "1.2.3",
        "mystuff/myamazinglibrary": "dev-my_local_branch"
    },
    "authors": [
        {
            "name": "my",
            "email": "my@email.com"
        }
    ],
    "minimum-stability": "dev",
    "autoload": {
        "psr-0": {
            "Stuff" : "src/"
        }
    },
    "repositories": [
        {
            "type": "vcs",
            "url": "path/to/my/local/library/root"
        }
    ]
}

If we then run composer update you will see it check out our local version instead of the one from Bitbucket:

Loading composer repositories with package information
Updating dependencies (including require-dev) - Updating mystuff/myamazinglibrary (v1.14.4 => dev-my_local_branch 554cb11)
Checking out 554cb1123cae3c8e0f723591164f0ecdc16a3a9d

Writing lock file
Generating autoload files

Job done – then once you’ve made all your changes and all is working as intended, simply commit your library changes, tag a version, upload and then update the composer.json file for your apps and update – you’re good to go.

Leave a comment