How to Setup a Private Composer Repository
PHP Composer is a great way to share libraries with... just about anyone. If you have code you'd like to keep private, however, Composer doesn't cater to that. Packagist, Composer's primary repository, only hosts public packages. The Composer docs have some recommended practices for hosting your own private packages. This post will walk through the steps required to set up your own private composer repository:
1. Get composer. Here is how to install it on *nix system. For other OS'es, check out the Composer installation docs Â
curl -sS https://getcomposer.org/installer | php mv composer.phar /usr/local/bin/composer
2. Download and install Satis. Satis is a static, lightweight tool for generating a Composer repository. There are other options, but Satis seems to be the quickest way to get up and running. Please note that if you installed composer globally you can omit the 'php composer.phar'part and replace it with 'composer'
php composer.phar create-project composer/satis --stability=dev --keep-vcs
3. Create a satis config file. This lists out all of the packages that your repository has available. Â **Important**Â Each package you want to have in your repository must have a composer.json file located at the document root (see below)
require-all: this option tells the repository to focus on all versions and branches of your package. Alternately you can definea 'require' property that will focus on specific versions for each repository
repositories.type: This is the package type. VCS stands for Version Control System. This is the best type to use for, you guessed it, packages under version control such as git or svn. See the Composer documentation about repository types for more info.
satis-config.json for defining a repository
{ "name": "My Repository", "homepage": "[home page url]", "repositories": [ //FORMAT 1 { "type": "vcs", "url": "[http://host.com]/[namespace (usually company name)]/[repo name]" }, //FORMAT 2 { "type": "vcs", "url": "[user]@[host.com]:[namespace (usually company name)]/[repo name]" }, ], "require-all": true }
Composer.json for defining packages
{ "name": "project-namespace-or-company-name/project-name", "description": "Package description!", "version": "0.3.0", "type": "library", "require": { "monolog/monolog": "1.0.*", "joli/ternel": "@dev", "acme/foo": "dev-master" }, "require-dev": { "debug/dev-only": "1.0.*" } }
4. Create the repository. In this step, Satis creates all the files needed for a Composer repository and scans the packages you defined in config file (step 3). The build dir referenced below is an empty directory with a name of your choosing. It's just a place to hold the files generated by Satis. I usually move the config file into the build dir after I created it (just to keep everything in one place).
mkdir [build dir] cd [satis dir] php bin/satis build [path to config file you created in step 3] [build dir]
5. Keep the pacckage details fresh. Composer does not automatically fetch new package details, so you need to set up a cron process to keep the package definitions up-to-date (this is the same command ran to build the repository in step 3).
This step is important because it pulls in new code, branches, and tags. I recommend running this script no less than every hour, but this depends on how often your packages are updated. If you are rusty on your crontab syntax, take a peak at the crontab documentation (this will likely require root access to your server).
php [absolute path to satis]/bin/satis build [absolute path to config file] [path to build dir]
6. That's it! You now have a private composer repository.
7. Make the repository accessible. If you are planning to share the repository with your team members, it needs to be available online. This can be set up like your basic virtual host. Once this is done, restart apache and add a new repository entry to your composer.json (see snippet at bottom).
To use the packages in your repository, you need to add them as dependencies in the composer.json file in your project. See documentation for composer.json or example below:
Composer.json for requiring packages
{ "repositories": [{ "type": "composer", "url": "[URL to repository]" }], "require": { "package1-namespace/package1-name": "1.0.0", "package2-namespace/package2-name": "1.0.1" }, "minimum-stability": "dev" }
Note the 'minimum-stability' property. This will allow you to download and use the package even in there isn't a stable release. More info for the 'minimum-stability' property.