Introducing composer — PHP dependency manager

In one of my previous article, I got a question from one of the reader — what is Composer? How can we use it? PHP Composer is not the package manager like apt or yum. This is dependency manager influenced by NPM. In this article I will tell you about composer, way to install it on Ubuntu and how to use it.

#Introduction to PHP Composer

As per the definition given on the website

“Composer is a tool for dependency management in PHP. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you.”

Composer deals with packages and libraries and install it with in a directory called ‘vendor’. But, it manages them on project basis. So it’s a dependency manager, not the package manager.

#Prerequisites

Composer requires PHP version > PHP 5.3.2. Also, while installing any package it gives warning that please do not run composer with root user.

#Installation

PHP composer is very easy to install. I am using Ubuntu that’s why I am attaching screen shot of Ubuntu Terminal here.

The above command will first download composer.phar (PHP Archive) , Move it to ‘/usr/local/bin’ and then execute it to install.

#Verification

To verify that composer has been installed on system open the terminal and type ‘composer’.

$ composer

The above command will show the composer version, Usage, options and Available commands available for composer.

#Package Installation using Composer

Package installation using composer is really easy. You need to write just one line command and it will install the package. Suppose, you need to install monolog logger for your symfony project. Then, you need to write

$ sudo composer require monolog/monolog

#Autoloading

Once you install a package, you need the ability to autoload the dependencies. Composer comes with an autoload.php file, which have capability to use all the dependencies installed using composer. You just need to add autoload.php into your bootstrap file.