The Helios Blogs

Bridging the Cultural & Communication Gap


Composer is a tool for dependency management in PHP. It allows you to declare the dependent libraries your project needs and it will install them in your project for you.

Dependency management

Composer deals with “packages” or libraries, but it manages them on a per-project basis, installing them in a directory (e.g. vendor) inside your project.

The problem that Composer solves is this:

  • You have a project that depends on a number of libraries.
  • Some of those libraries depend on other libraries.
  • You declare the things you depend on.
  • Composer finds out which versions of which packages need to be installed, and installs them (meaning it downloads them into your project).

Declaring dependencies

Let’s say you are creating a project, and you need a library that does logging. You decide to use monolog. In order to add it to your project, all you need to do is create a composer.json file which describes the project’s dependencies.

    "require": {
        "monolog/monolog": "1.2.*"

We are simply stating that our project requires some monolog/monolog package, any version beginning with 1.2.

System Requirements

Composer requires PHP 5.3.2+ to run.

Installing Composer
Installing Composer is really easy as it can all be done through the command line. I’m using OS X, but the following should be the same for any *nix operating system.

So fire up Terminal and run the following commands:

$ curl -s | php
$ sudo mv composer.phar /usr/local/bin/composer

The first command downloads the composer.phar file to your computer. The second line moves the composer.phar file in to your bin so that is accessible globally on your computer.

Now run the following command:

$ composer

If you have installed Composer successfully, you should be given a list of available commands and descriptions.

Using Composer

Now that you have Composer installed, you can use it to require some packages into your project. To make a Composer configuration file, we just need to make a JSON file in the root of the project.
For example, if you wanted to use monolog package you could create the following composer.json file.

  "require": {
   "monolog/monolog": "1.2.*"

To resolve and download dependencies, run the install command:

$ php composer.phar install

If you did a global install and do not have the phar in that directory run this instead:

$ composer install

Following the example above, this will download monolog into the vendor/monolog/monolog directory.


Besides downloading the library, Composer also prepares an autoload file that’s capable of autoloading all of the classes in any of the libraries that it downloads. To use it, just add the following line to your code’s bootstrap process:

require ‘vendor/autoload.php’;

That’s it, enjoy the new gained knowledge

Leave a Reply

Your email address will not be published. Required fields are marked *