PHP Dependency Management Tool – Composer and Packagist

Composer is an application-level PHP dependency management tool. It will seamlessly install and update the packages that you declare as your PHP project’s dependencies. Composer pulls the latest packages from the PHP package repository, Packagist. However, it is not a package manager. 

Nils Adermann and Jordi Boggiano have developed the tool and first released it in March 2012. Composer manages the packages within a specific directory by default, not globally. It does have a global command and support for the global project as well. In general, the composer handles package dependencies per-project basis. The idea behind Composer is based on the node‘s npm concept.

What is a library or a package?

As a PHP programmer, you must know that everyone searches on the internet for a readily available class file to achieve certain functionality. So, that way we don’t have to code some functionality on our own. Therefore, we could save some time.

Remember the early age techniques of including a third party class file in your PHP project for handling different requirements like the file upload or to export a data grid into an excel file format or send an email using third party SMTP! Things are not that simple anymore. Now you have to handle User Authentication with social media login support, Request Routing with custom routing pattern matching, File Uploading with extension restriction and remote storage support, etc. It is good that the functionalities are still readily available on the internet, however, the fact is, the single class files have now become packages including multiple class files and directories.

What is a PHP dependency management tool

Different code repositories now have a handful of these PHP packages available for you to just pick and use. It would be a pain if you had to manually do that job to include the packages to your project. Here comes the concept of the dependency manager for PHP!

You generally need to have a central configuration file where you would mention the names and versions of the packages you need in your project. The dependency manager will grab and put those packages in a separate directory under your project. Even the packages that you need might have a dependency on other third-party packages. The PHP dependency management tool will take care of that too in a chain so that any dependent package doesn’t miss its own dependency.

The Composer as a Dependency Manager for PHP

Composer requires at least PHP 5.3.2+ to run. Also, it runs in CLI mode and installs the dependency libraries for a PHP application. Most of all open-source PHP projects including Laravel use the composer as a part of the project.

Composer PHP Dependency Manager – Installation and Usage

You need to download the Composer tool locally into your project directory from the official download page. This is the standard installation process mentioned below:

curl -sS | php

Sometimes, if the server is not configured properly and the common modules are not installed, the above command might fail. Know how to create and configure a server properly for the development environment.

If the command above doesn’t work, run the following commands.

php -r "copy('', 'composer-setup.php');"

php -r "if (hash_file('sha384', 'composer-setup.php') === 'e0012edf3e80b6978849f5eff0d4b4e4c79ff1609dd1e613307e16318854d24ae64f26d17af3ef0bf7cfb710ca74755a') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"

php composer-setup.php

php -r "unlink('composer-setup.php');"

php dependency management tool

It will download the composer.phar to your working directory. Then you can run the php composer.phar command to run the composer. On the other hand, if you want to have composer installed globally, run this command mv composer.phar /usr/local/bin/composer. Now, instead of php composer.phar, you can simply run composer. For testing, you can run composer -V to check the installed composer version. If you need to update the composer software, run composer self-update

Now all you need to have is the dependency management file which is generally a composer.json file with content like below:

"require": {
"vendor/package": "1.3.2",
"vendor/package2": "1.*",
"vendor/package3": "^2.0.3"

This is the file where you mention all the required packages. The package names can be found from the Packagist. Your composer.json file should look something like below, for example.

"name": "acme/hello-world",
"type": "project",
"require": {
"monolog/monolog": "1.0.*",
"guzzlehttp/guzzle": "5.1.0",
"jenssegers/agent": "~2.1",
"maatwebsite/excel": "1.*"

First of all, have the composer.json file ready with the required dependencies mentioned. Subsequently, run the composer update command. As a result, it will install all the mentioned libraries and their dependencies as well (if any) in the /vendor/ directory of your project. In addition, it will create a /vendor/autoload.php file as well, which is an optimized class loader file. Finally, you just need to include the autoload file, i.e. require 'vendor/autoload.php'; and you’re done!

By Supratim Roy

I'm a technology enthusiast with over fifteen years of hands-on work experience in the IT industry. My specialization in web development technology, allows me to quickly adopt innovative approaches and leading-edge technology solutions. After passing the HS, I joined B.Com. under the CU. From the second year, I started studying computer courses. After graduating in Computer Applications (BCA) from Indira Gandhi National Open University (IGNOU), I started my career as a Jr. Software Dev. in 2005. After four years, in 2009, I got the opportunity to pursue a master's degree. In 2011, I completed MCA from Sikkim Manipal University (SMU). I carried out the journey of my carrier as Jr. Software Dev., Sr. Software Dev., TL, and PM. Now I'm working as the VP of Technology & Product at CodeClouds. I use my technical knowledge, skill, expertise, and previous experience to analyze the requirements of the clients and understand their business philosophy. I try to deliver quality solutions and products in a timely manner with a high percentage of accuracy. I also prepare and manage project specifications, scope documentation, workflow, data diagram, and development documentation. I lead a team of around 50 highly talented members in a well-formed and structured way. I love listening to music and watching the action, adventure, thriller, horror movies, and Discovery, Animal Planet, and Nat Geo TV channels. Photography is also one of my hobbies, however, I'm really an amateur! I like to read technical articles, journals, and documentaries about Indian Himalayas and wildlife. I'm passionate about adventure sports. I encourage peoples to love adventure sports. I've traveled to many places and trekked in several parts of the Himalayas in India and Nepal. I'm a member of "MTG - Miles To Go" - a nature lover's association in Kolkata. I've done the Rock Climbing course from Parvat Abhiyatri Sangha affiliated with Anandabazar Patrika. I also participate in several community initiatives taken by an NGO.

Leave a comment

Your email address will not be published. Required fields are marked *