Quick start guide to Composer , The dependency manager for PHP

2 min read

Dependency management became crucial in development process this days . It will help you manage your application dependencies effectively and stay away from outdated , incompatible or vulnerable packages.

To understand the problem imagine this scenario. You are building an awesome application and you want to send transnational emails and you find this awesome package that allows you to send a professional emails so you decided to use.

In the traditional way you will go and grab a compressed version of the package and extract it in your code , then require it’s files within your code. after a while you install about 10 different packages and suddenly one package release update .. a security update for example .. it will take forever to download the package again and extract it and edit your code to include the new package .. and what if you download unstable version ? or version not compatible with your code ??
So here we go , the dependency management comes to the process.

In 2011 Nils Adermann and Jordi Boggiano begin to develop one for php , heavily inspired from Node.js’s “npm” and Ruby’s “bundler”. They released the first version Jul , 2013
Now packagist – the main repository for composer – now have over 238,454 package.

Ok now lets dive in.

– Installation

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php composer-setup.php
php -r "unlink('composer-setup.php');"

Now you should have a composer.phar file at the directory, please refer to download page  if you need more info about the installation process.

Optionally , make it accessible  global to your system by updating

mv composer.phar /usr/bin/composer

Now you can use composer globally

composer --version

– Usage

Now lets introduce you to composer.json

composer.json is a file contain the basic info about your project like name , author .. and the info that composer require to include and load your dependencies correctly.

Lets create sample composer.json

composer init

it will ask you some questions , answer what you can and hit enter , new file will be generated called `composer.json` have some basic details

    "name": "eslam/composer",
    "require": {}

Ok now lets try to include a sample library (symfony debug – which help you to debug and track errors more effectively see here )

composer require symfony/debug

This will grab symfony/debug package latest stable version and will save it in /vendor folder as well as at your composer.json , so your composer.json now will show

    "name": "eslam/composer",
    "require": {
    "symfony/debug": "^4.1"

As you can see it has symfony/debug and the version family you installed. now lets write small script to test it

include 'vendor/autoload.php';
use Symfony\Component\Debug\Debug;


function view($arg){
    return add($arg[1],$arg[2]);
function add($x,$y){
    return $x + $y;

echo view($argv);

This is a just small script to take two arguments and preform simple addition then return the result.

php index.php 2 3

It will give you the result of : 5

Ok now lets test the package and generate error

php index.php 2 a
PHP Fatal error: Uncaught ErrorException: Warning: A non-numeric value encountered in /home/eslam/Desktop/composer/index.php:11
Stack trace:
#0 /home/eslam/composer/index.php(8): add('2', 'a')
#1 /home/eslam/composer/index.php(14): view(Array)
#2 {main}
thrown in /home/eslam/Desktop/composer/index.php on line 11

Awesome. Package loaded successfully and showed you the error and stack trace for your code.

– cheat sheets

Here is some list of useful commands you should be aware of

Updating Dependencies to their Latest Versions

composer update

Removing Dependency

composer remove symfony/debug

Shows a list of installed packages that have updates available, including their latest version.

composer outdated

Shows information about packages.

composer info

Clear composer internal cache

composer clear-cache

Update composer to the latest version

composer self-update

That’s all for now , feel free to see the composer documentation yourself. Also make sure to check our Shieldfy PHP package it’s available on Packagist for your composer ;).