Using Composer to install PHP dependenciesJanuary 22, 2014

Sometimes you’ll want to install and use a project that depends on other projects in order to work properly. One example is the PHP framework Symfony, which uses other things developed by other people, such as the twig bundle (twig is a templating system) and the doctrine bundle (which includes an object relational mapper and a database abstraction layer).

Now suppose the current version of Symfony is 2.5, and it works really well with Doctrine 4.5, and twig 2.0; but strangely not so well with Doctrine 4.4 or Doctrine 4.6. Is this the fault of the makers of Symfony? Or the fault of the makers of Doctrine? In any case, this makes it difficult for either Symfony or Doctrine to provide you with a convenient install bundle that includes versions of each project that work perfectly well together.

In comes Composer – you can easily install and uninstall bundles and test whether they work well together with the same finesse with which you slowly build a unix-based operating system – one at a pains-taking time!

The composer.json file

The composer file itself is written in JSON format. Here is a sample (cut-down) composer.json file:

    “name”: “symfony/framework-standard-edition”,
    “type”: “project”, etc…
    “require”: {
        “php”: “>=5.3.3”,
        “symfony/symfony”: “~2.4”,
        “doctrine/orm”: “~2.2,>=2.2.3”,
        “twig/extensions”: “~1.0”,
    “scripts”: {
        “post-install-cmd”: [etc],
        “post-update-cmd”: [etc]
    “config”: {
        “bin-dir”: “bin”

Pretty self-explanatory; I’ll go ahead and explain anyway. The first bit is obviously the properties of the Symfony project, and underneath is a list of required extra bundles this project will need in order to work. Composer will run along and fetch those bundles and install them for you. In this example, Composer will go and find the specified versions of PHP, Doctrine, twig, etc and install them. Also in the composer file you can see scripts that can be run pre and post installation, as well as configuration properties for the project and extras.

Installing composer

To install composer, you open a command prompt and run the command:

curl -sS | php

Running composer

To run a composer file to get it to install everything your project needs you use a command prompt and navigate to the directory your project is in:

composer install

To run a composer file to get it to install additional requirements, add the requirements to composer and then:

composer update

And it’s that simple. With both this and git on your side you’ll think you’re using Linux even when you’re using Windows!

Category: Tutorials

Leave a Reply

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