What is Composer and why should every PHP developer use it

Imagine with me the following situation: You are a PHP developer, you have a project that you would like to develop, you may choose a specific framework for this task, but you will need a few additional libraries to do this. Imagine that you want your application to post certain updates on the user's account on Twitter, and you found the library you want In its use, but it is a library that depends on another library.

The Stone Age PHP developer will do the following: He will download a copy of the framework, and then create a folder in which he places the additional libraries he needs and then try to understand how they work to link them together. This method may pay off, and it may allow you to develop your project "without any problems," but what happens, for example, if an update is launched to any of the libraries that you depend on? Will you reload it again and replace the old version with the new one? Could you do that if you use more than one interdependent library? Not sure about that. But what is the alternative? If you have ever used other programming languages ​​such as javascript with node.js or ruby, then you have dealt with what is called a package manager, where npm is used with node, for example, to install packages and plugins for node. The composer position in PHP can be said to be the npm placement of node.js, as composer allows you to download the libraries you need in your project and keep them up-to-date without having to manually download and move them. Despite all of this, the official composer site indicates that it is not possible to call it a package manager by virtue of the fact that it does not generally install these packages on the global system, but rather the packages within each project are managed locally, and that is why it is called the Dependency Manager.

Installing composer

Let's go in theory and let's install composer and look at how to use it. Although it can be installed locally within every project, it is generally preferred to have it installed on the system. On Linux / Unix systems, the following two commands are sufficient to install composer:

$ curl -sS https://getcomposer.org/installer | php
$ mv composer.phar /usr/local/bin/composer

You may need to add sudo before the second command if you need administrator privileges to execute the command.

On Windows systems, it is sufficient to download and install its official application.

You can check if composer is installed correctly on the system by executing the composer command at the command line which should display its help.

Use Composer

Now that we have installed composer, we will need to create a composer.json file through which we inform composer of the packages that we want to attach and rely on in our new project, and this file can contain other data that we will need in building the project.

In its simplest form, the composer.json file looks like this:

    "require": {
        "monolog/monolog": "1.0.*"

For this example we are relying on version 1.0. * Of the monolog library. Of course, it is possible to rely on more than one library in our current project, as it is sufficient to add a new line for each library between the two require incubators, and each line consists of the name of the library (which usually contains the name of the entity producing it followed by its name, and the same name is usually repeated twice) Add to the version number we want.

These libraries and the mechanisms for adding them to your search project can be found at https://packagist.org/.

Now that we have identified the desired libraries, it is sufficient for us to execute the command

composer install


php composer.phar install

If you haven't moved composer.phar to a directory, its path is in the system's PATH variable. The composer will load all these libraries and put them into a vendor folder that will be created inside the current project directory.


To prevent the developer from calling these libraries one by one when writing his project, composer creates a vendors / autoloader.php file that manages that, as this file suffices to be able to use the libraries that you have downloaded without having to do it manually:

require_once "vendors/autoloader.php";

Libraries update

When a new update is issued for the library you depend on, it is sufficient to execute the command to obtain it:

composer update

Of course, if you specified a specific version in the composer.json file, you will not get the most recent versions unless you specify the version in a way that allows automatic upgrade. In other words, if you are using for example the Laravel framework and add it using the following line "laravel / framework:" 4.1. * It will update to a version in the 4.1 branch and will not pass to versions 4.2 or later.


Packagist is a composer site that bundles open source PHP libraries that are freely available for everyone to use using a composer. According to the composer's official documentation, the library is not required to be on Packagist to be called by Composer, but it is preferred if you want your library to be made available to everyone to register it on this site.


If you are a PHP developer and you want to develop with the development of this language and not remain locked into the old versions of it (version 4), Then you must follow a different method of development from the method of PHP developer from the stone age. One of the first steps you will take to reach this is to use the composer in all of your projects that you work on, as the glue that adheres the components of your project to each other and makes it easier for you to develop.

For more information about composer and about the various libraries that can be used in your project, visit their official website and Packagist website.

No comments:

Powered by Blogger.