Composer — The package manager for PHP

PHP has grown a lot the last few years … In my mind it seems that ever since PHP received namespace support back in 2009 (5.3.0), the language finally seems to have been breaking up with it’s long time buzz as being kind of a wild child!

One thing has allowed itself long in coming though — A standard for managing and distributing packages for it’s developers. Attempts has been made, true, most known is probably PEAR (did anyone say phpclasses.org o_O), but it never really seems to have been taken in by the community. Honestly, it’s been a while since I’ve last used PEAR, but I recall dropping out most of the times, as it was always just to much a hassle!

Those days are finally over though — Composer is here and it seems that this time the community has finally accepted the courier!

What is Composer?

Composer is basically a dependency manager, meaning a tool for which you can describe what dependencies your application relay on — Even better, Composer can download and include these dependencies (and their dependencies) into your application with at single command.

How does Composer work?

Composer uses a simple JSON formatted file as it’s configuration/instructions called composer.json.

Say you wan’t to include the monolog package/library, which allow you to send your logs to a specific file, socked, inbox or database — This would be described to Composer as following:

{
    "require": {
        "monolog/monolog": "1.9.*"
    }
}

This literally tells Composer that you require monolog version above or equal to 1.9 but below 1.10 for your application to work — A more detailed explanation of package version targeting can be found here.

Running a simple command composer will look for the package through it’s repository packagist.org (which you can also manually search through) and download the package matching your version criteria.

Installing Composer

First things first … installing Composer is actually really simple. You’ll need to execute the following command from you command prompt:

curl -sS https://getcomposer.org/installer | php

Windows user can download the installer here: Composer-Setup.exe

When this is done you should have a composer.phar file available. You can execute the following to verify that everything is in place — You should get a nice ascii-banner along with the typical usage/options/commands list:

php composer.phar

The composer.phar file will be installed in the directory you are currently in, if you want to be able to use it globally, you can simply move that file to a global space, maybe even rename it for convenience. Personally I’ve done the following:

mv composer.phar /usr/local/bin/composer

Using Composer

So you have installed Composer … next let’s have a look at how to actually use it.

Initializing composer

To get started. This will initiate a step-by-step setup for your composer.json file:

php composer.phar init

Installing dependencies

Now you have a, more or less, empty composer.json file, time to add your dependencies — Try adding the require-statement from above and save the file.

Next and final step is simply to tell Composer to install the dependencies you’ve added — This is done by running:

php composer.phar install

Autoloading and utilizing

By default Composer installs you dependencies into a folder called “vendor”. You’ll notice that more dependencies, beside the ones you’ve required are installed. This is because Composer comes with an autoloader. You might noticed in the console output that Composer automatically updated the autoload-files when you installed the dependencies?

You’ll also notice the new file composer.lock. This is a file Composer uses to keep track of which versions it has installed and should really not be tampered with manually — Actually you should be able to control everything through the command-line.

Finally you’ll see the monolog package under the vendor folder as well. In order to actually utilize it, you would require the autoloader and use the namespaces accordingly — Here’s a quick example which allow us to use monolog to log to a file:

<?php
require 'vendor/autoload.php';
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
$log = new Logger('MyLogger');
$log->pushHandler(new StreamHandler('./monolog.log', Logger::WARNING));
$log->addWarning('This is a logged warning!');
$log->addError('And this is a logged Error!');

Now obviously the namespaces to import and the way to use the packages you’ve installed vary, but as long as you have required the Composer autoloader, you should be able to use the example above.

Updating installed packages

As mentioned before, most everything you want to do with Composer, should be done using the console. Updating your installed packages is no exception — and it’s probably just as powerful and used feature as the previous installation feature … One of the main reasons for using a package manager in the first place.

Say that monolog has been released in a new version (which it has). All we have to do in order to try this out is simply to change the target version in our composer.json file to the new version. Let’s change the version from “1.9.0” to “1.10.0” — Go ahead, try it out. When you have saved the file, all you have to do, is to write the following in the terminal:

php composer.phar update

Notice the output … Composer now removes the old version and installs the new version — On top of that it makes sure that the autoloader is up-to-date :)

In this case we explicitly changed the composer.json because we knew that a new version was out. Another scenario could be that we have targeted a wider range of versions, say “1.*”, meaning any latest version to a major version 1 — Or you could be a bit of a wild-child and simple always request the latest release “*”. In any case it’s always just a matter of executing the composer update statement above, to get the later version … pretty neat yes!

Conclusion

As of this moment Composer is host to more than 37.700 packages — compare that to the ~600 found at PEAR which has been alive far longer :p

The PHP community is really getting organized and with the language evolving and tools like Composer being a base component in most modern frameworks (like Laravel and Symfony), the future is looking just a little brighter.