Composer: use private repositories from GitHub

Unfortunately not all your packages can be open source. Some companies don’t join the “open source train” while other packages must remain private for other reasons. These packages can be used in several projects and it would be lovely to use it just like any other package. Off course you use the private version of packagist but that’s a bit expensive (starting at 49 euro per month) and just isn’t necessary.

With some minor effort you can use your private repository in any composer project. For this example I’m using a fictional package called vdhicts/fix-world which I’m willing to use in my project. This is a private repository on GitHub with a release.

Composer.json

The first step is to add your GitHub repository to the composer.json of your package. You can use this snippet as example:

"repositories": [
  {
    "type": "vcs",
    "url": "https://github.com/vdhicts/fix-world"
  }
]

If you want you can add the package name (from composer.json) to the require or require-dev section of your composer.json:

"require": {
  "vdhicts/fix-world": "^1.0"
}

Or you can add the package by executing composer require vdhicts/fix-world.

Please be aware that the name of the repository doesn’t need to match the name of the package. For obvious reasons I would suggest to be sure you keep them the same.

Repository access

To be able to fetch the package, Git should be able to access your private repository from the command line. With the following steps we can achieve this:

  1. Open GitHub.
  2. Click top right on your icon and click on settings:
  3. On the menu at the left side, go to developer settings.
  4. On the menu at the left side, go to personal access token.
  5. Click on the generate new token button.
  6. Give the token a good name. I suggest you name the token after the device you are using it for. Place a check mark at the repo rights.
  7. Click on the generate new token button and copy the token. The token is only visible at this time!
  8. Copy the token and open your .gitconfig. This is usually located at ~/.gitconfig.

    The command git config --list --show-origin will reveal the location of your .gitconfig.

  9. Paste the following text (or update the accesstoken if this option already exists):
[github]
  accesstoken = [TOKEN]

At this moment you will be able to perform composer update or if you have skipped adding the package, perform composer require vdhicts/fix-world.