Getting started with composer (composer.phar)

I am kicking off a new PHP project with the Zend Framework 2 (ZF2), and chose to use Doctrine as the persistence layer with ORM capabilities. Knowing that I need two Zend modules for Doctrine support, I've cloned the two modules (DoctrineModule and DoctrineORMModule) from GitHub to enable doctrine support in my ZF2 project. Oops, just installing those two Zend modules from GitHub do not make doctrine work! What am I doing wrong? Do I need a core Doctrine library from installed in addition to those two modules in order to make this work in ZF2? After careful review of documentation, the preferred installation method was using "composer" not "git clone".

What is composer? Composer is a dependency management tool for PHP, and it installs all dependent libraries that require for a given project. So, as part of DoctrineModule installation, if core Doctrine/Common and Doctrine/DBAL are required, the dependency installer "composer" will automatically install them for me.

How do you define dependencies in composer? You'll either create/update a composer.json file which describes the projects' dependencies. This file is also created and/or updated by composer when you install modules and libraries. A sample composer.json may look like below:

    "require": {
        "doctrine/doctrine-module": "0.*",
        "doctrine/doctrine-orm-module": "0.*"

How do you install composer?

$ curl -sS | php

If you already have the composer.phar binary, and would like to update to the latest version you may run the self-update command. Running the command will replace your composer.phar binary file with the latest version. Some library update may require you to update the composer as well.

$ php composer.phar self-update

Running the command above will check your PHP settings and download composer.phar, a Composer binary file into your local working directory.

How do you use composer?

If you have the composer.json file, the Composer will install dependent libraries for the project.

$ php composer.phar install


$ php composer.phar require doctrine/doctrine-module:0.*
$ php composer.phar require doctrine/doctrine-orm-module:0.*


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.