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 on 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, 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?

Being in PHP programming, you 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 of including the packages to your project. Here comes the concept of 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 dependency.

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 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 https://getcomposer.org/installer | php
If that doesn’t work, run the following commands.
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('SHA384', 'composer-setup.php') === 'e115a8dc7871f15d853148a7fbac7da27d6c0030b848d9b3dc09e2a0388afed865e6a3d6b3c0fad45c48e2b5fc1196ae') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"

It will download the composer.phar to your working directory. Then you can run the command php composer.phar to run composer. 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 finally look something like below:
{
"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 file ready composer.json with the required dependencies mentioned. Finally, run the command composer update. As a result, it will install all the mentioned libraries and their dependencies as well (if any) in the /vendor/ directory of your project. Consequently, it will create a /vendor/autoload.php file as well which is an optimized class loader file. You now just need to include the autoload file, i.e. require 'vendor/autoload.php'; and you’re done!

Published 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 the 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 Sr. Software Dev., TL, and PM. Now I'm working as the VP of Technology 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 25-30 highly talented developers 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 in 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 Rock Climbing course from Parvat Abhiyatri Sangha affiliated with Anandabazar Patrika. I also participate in several community initiatives taken by an NGO.