Home » Code » What is PHP Composer?

What is PHP Composer?

Posted by on January 7th, 2013

What is PHP Composer
If you have ever written anything in PHP before, you have probably found that it feels like you have to keep re-inventing the wheel anytime you want to do a common task such as User Authentication, Database Management or Request Routing. PHP now has a handful of mature frameworks that have already solved all of these problems, so wouldn’t it be easier to cherry pick the bits that you needed from each framework?

If you were to start manually picking the bits you wanted from Zend, or Laravel or Symfony, then it would become very difficult to manage. Each library might also have dependencies, and so you would end up in a mess, particularly if you required other people to work on your project.

This is where Composer comes in. Composer is a dependency manager for PHP. Composer will manage the dependencies you require on a project by project basis. This means that Composer will pull in all the required libraries, dependencies and manage them all in one place.

This kind of management for dependencies in a project is not a new concept, and in fact, much of Composer is actually inspired from npm from Node.js and Bundler from Ruby.

You might also be aware of PEAR. PEAR is an established PHP package manager that has been around for years. PEAR however, has been abandoned by many PHP developers for a number of reasons. Firstly, much of the code in PEAR is out-of-date. Secondly, PEAR forces you to install packages system wide, rather than on a project-by-project basis. This means that if you already have a project that relies on a slightly older package, you are screwed. For an excellent history of PHP packages, read Packages: The Way Forward for PHP by Phil Sturgeon.

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 https://getcomposer.org/installer | 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.

Installing on Windows?

If you are looking to install Composer on a Windows machine, take a look at the guide on the Composer website.

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 Slim Framework you could create the following composer.json file.

  "require": {
    "slim/slim": "2.*"

To install Slim via Composer, you can simply run the following command through Terminal.

$ composer install

This will automatically download Slim into your project under the directory vendor/slim/slim.

See, how easy was that?


Now that you have all of these different packages, you need the ability to autoload them into your project. Fortunately Composer also comes with an autoload file, which is capable of autoloading any of the files in the projects that you download.

To use the Composer autoloader, simply include the following line in your project’s index or bootstrap file.

require 'vendor/autoload.php';

Now you can start using the new libraries without having to worry about loading them up.

For example:

// Autoload
require 'vendor/autoload.php';

// Instantiate a Slim application
$app = new \Slim\Slim();

// Define a HTTP GET route
$app->get('/hello/:name', function ($name) {
    echo "Hello, $name";

// Run the Slim application


Package management is clearly the right route forward for PHP. Languages such as Ruby have shown how incredibly easy it is to use packages within projects so common problems can be solved once, and you can stop wasting time as a developer by constantly reinventing the wheel.

Many of the most popular frameworks have already positioned themselves to use Composer, and many more individual developers are releasing code that is Composer ready from the get go.

As a PHP developer, Composer will become your best friend, and as it’s usage increases, it will become an essential part of writing PHP on a day-to-day basis.

By following conventions, PHP can become a much better language to use. Composer has already solved a big problem in the PHP community, so there’s really no reason why you shouldn’t start using it today.

Philip Brown

Hey, I'm Philip Brown, a designer and developer from Durham, England. I create websites and web based applications from the ground up. In 2011 I founded a company called Yellow Flag. If you want to find out more about me, you can follow me on Twitter or Google Plus.

  • Pingback: Getting started with Laravel 4 | Culttt()

  • Pingback: Creating a Laravel 4 package | Culttt()

  • Praveen

    Very helpful article.

  • Louis

    Composer is great. But I am on a shared hosting, how can I run command line to install composer by myself? Do I have to ask the hosting provider to do it for me all the time?

    • Do you have SSH access? If it’s shared hosting you probably won’t. I would look at a host like http://fortrabbit.com who will probably be more suited to your requirements.

      • charmaine Sorila

        fortrabbit is quite interesting. I hope I could give it a try one day

  • Alex

    One thing I don’t quite understand. You said it’s possible to pick bits of other frameworks like user authentication. How does that work if you’re already using a PHP framework which maybe doesn’t provide those features. Is it realistically possible to integrate portions of another framework into your own just like that?

    • Absolutely, but you would have to be using a framework that supports this. Have a look into Laravel or Symfony

  • Tareq89

    good thing !

    • Yeah, a very good thing :)

      • hardik

        one thing i don’t understand is how does it work ? i mean
        if i write

        php composer.phar require sonata-project/doctrine-orm-admin-bundle

        how does it know the exact path of git hub. also it only works with git hub ?

        • Hmm, I’m not sure to be honest. But it’s open source https://github.com/composer/composer

        • There is a repository of available packages for composer, so it uses the packages from packagist.org – that’s the place where you can find all the available vendor/package names for composer. When a developer submits its own package to packagist repository it configures that vendor/package link to github.

  • nitin

    thanks so much to explain in easy steps

  • Nagarajan Babu

    thanks man, this explains a lot

  • Zero

    Its spelled dependency and dependencies.

  • Ismael

    thanks mate. :D

  • eyeBoogiez

    Thanks a lot for the article! I am following the getting started with laravel, very informative.

  • Dan

    Please don’t use this shit on public projects, there is no cross platform easy way to install it and use it.

    • How is curl not cross platform? What would you use instead?

  • Biranchi Narayan Nayak

    Excellent step-by-step tutorial

    • Thank you :)

      • Dray Kenns

        how do you uninstall symfony, and how do you uninstall composer googling this is impossible to find

  • Nirmal Thapa

    basically you told,

    1. need to have composer.json in the project
    2. composer install
    3. creates autoload file to load newly downloaded files/dependencies that can be included in project startup file

    Awesome tutorial. Simple and elegant. Now I know why there is an exact folder named ‘vendor’ in LARAVEL. It Helped me understand Laravel much better

  • Richard

    Nice article Phillip.

    Here I found another good article about Composer dependency manager.

    Have a look.

  • Pingback: What are Modules in Domain Driven Design? | Culttt()

  • Dray Kenns

    how do you uninstall symfony, using composer or another way, and how do you uninstall composer

  • Gregor Gonzalez

    Easy and fast, so simple. Too many websites don’t give a clue, this helped me a lot to start. i’m not use to work with third party software, like frameworks, because we use shared hostings.

  • محمد المشايخي


  • Pingback: PHP Composer چیست ؟ - Hive | مجله طراحی و برنامه‌نویسی()

  • Simplest explanation I’ve found. Thank you!

  • Zeshan Momin

    question for a relative newbie: i’ve got php on a server, how do i install composer on that server? I don’t have php on a local computer, all my files are on the server accessed via the hosting websites platform.

  • Pingback: PHP Composer چیست ؟ – My Likes Here()

  • Pingback: Using Composer for PHP development |()