Imagine you had to use ten different third party libraries in your PHP project. What would you do? Well, you don’t need to imagine it as you must have faced the need and most probably you must have downloaded them from different sites and copied them to your project directory. While this is the normal process, wouldn’t it be easier if you could just type in the name of the packages and libraries and somehow they would automatically get downloaded and copied to your project folder? Well, it would be and this is exactly what a package manger does.

Package Manager

A package manager is a centralized service where information regarding different available packages for  a particular system or platform are stored. The author of the packages submit their package information to the service and from there users can install them as they need by using a client in their local system. The information submitted includes the download URL or repository URL of the packages corresponding to their different versions and some other details like the other library and packages the package is dependent upon.  So if you install the client in your system and tell it in someway what packages you need it will connect to the central server, get the required information and download the packages for you into your project folder. Undoubtedly this is much easier as you will not have to manage the dependencies of different libraries by yourself. So a package manager may act as a dependency manager which is the case for Composer. Development platforms like Node.js, Ruby, etc. have their own package mangers like npm and bundler. Even operating systems like Mac has a package manager called Homebrew. So why not PHP?

Composer : The PHP Dependency Manager

It is not that PHP didn’t have a package manager, it was just that it was not as easy and as convenient as the other package managers available in other platforms. We are all familiar with PEAR which works like a package manager for PHP, but it has become old and using it is not as convenient as Composer. Moreover, while Composer allows us the flexibility to have any package we like of any version we want per project basis, PEAR enforces global installation of package and libraries which can be a serous problem if we would like to use libraries of different versions in different projects. So inspired by the package mangers of the other platforms, Composer made its way into PHP and since then it has been adopted as the package manger of choice by many popular libraries like Zend, Symphony, etc.

How to use Composer

Using composer is very simple. First you need to install the Composer client in your local machine. If you are on Windows then its as easy as clicking an executable. So download the composer setup file and run it to have it installed.

Once you have installed composer, it will add the necessary PATH variables to the system environment so that you will have access to run the composer command in the command prompt. Once you have access to the composer command you just need to run the command prompt or terminal from your project directory and give the following command to install the packages you want:

But at this point it won’t work. Why? Because composer doesn’t actually know what packages you want to install. So before running the command, you need to specify the packages you want composer to install for you. So how would you do that? Just by creating a composer.json file in the root directory of your project.  The syntax of the file is very simple and you need to add lines like the following to specify the packages that your project requires:

The require object lists out the libraries and packages you want. In the require object key is the standard library name and the value is the version of the library you want. You can visit the Composer package listing site and search for the package you want. There after going to the page of the package you can view the package name and latest version you will need to require. The package page lists the previous versions also so you can specify in your require object any version you would like. The * in the version numbers you will see like 4.* means any sub-versions starting after 4. You may see @dev, @beta, etc. in the version numbers and those are only stability flags by which composer knows how to filter different versions of the package on the basis of their stability. You just need to use them as is without worrying about them as they are handled by the creators of the package.

So once you have created the composer.json file on the root of your folder, just start the command prompt from the folder which you can do by clicking on the Use Composer Here menu that Composer adds to the contexts menus on Windows. And then issue the command composer install. This will start downloading of the required files into a folder named Vendor inside your project and organize them as per the libraries’ folder structure. Composer will also create an autoload.php file for you so that you can easily use the different libraries in your project without needing to include individual libraries one by one. You just include the single autoload.php file and you are done.

So you see how easy and effective it is to use a package manager like Composer. Composer has been out since few years and if you have experience of other package mangers like npm then this is certainly nothing new.