How Composer works publisher image
Lamai Institute

How Composer works

Composer Package manager Composer package manager

Some background

If you are a Linux fan, you probably know of package managers. These are used to install packages (or simply software) on the Operating System. Examples of these are debian's apt or yum for RPM distributions. These package managers install the software system-wide, i.e. on the entire system.

Now, say you have a PHP project, and this project needs several PHP dependencies.

A software dependency is an external standalone library that can be as small as a single file or as big as multiple files and folders organized into packages to perform a specific task.

There could be several ways to 'install' or 'introduce' these dependencies to your application code. You could go to the dependency website, download it, place it in your application directory and reference it in your code. 

But then say you have about 30 or more of these dependencies. And some of them are updated at irregular intervals, or an update in one of them needs several others to be updated as well. Managing this manually is really cumbersome work, especially when/if you have multiple projects. You need to keep track of the update schedules of the dependencies, you need to read the update logs to see if they affect others, et cetera.

Composer

From their own defination, '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.'

This means that Composer will pull in all the required libraries, dependencies and manage them all in one place.

This kind of management for dependencies in a project is not a new concept, and in fact, much of Composer is actually inspired from npm from Node.js and Bundler from Ruby.

To install and use libraries that are managed by Composer, you just need to declare them in your project in a standard format, and Composer will manage the rest. For example, if you want to install the phpmailer library by using Composer, you just need to run the following command in the root of your project.

$composer require phpmailer/phpmailer

There are two main files and one directory(folder) associated with composer. The directoryis named vendor and is located in your project root. The two files are composer.json and composer.lock.

When you run a composer install command, the dependencies are installed (stored) in the vendor directory. For example, with our phpmailer command above, composer will go to packagist and find the link to the dependency files, then download them to the vendor directory. It will then create an autoload.php file that you can include in your project and can then implement any of the installed dependency.

Composer is called a dependency manager and not a package manager because it deals with packages per-project-basis, by installing them in a directory inside your project. Yum and Apt are package managers in that they install their packages globally on the system.

This installs the phpmailer library and its dependencies in the vendor directory of your project. More importantly, it also creates composer.json and composer.lock files that will be used to track the dependencies of your project. 

How to Install Composer

When it comes to installing Composer, there are two ways. You could install it either locally on a per-project basis or globally. We’ll install it globally so that it can be used across different projects.

For Windows Operating Systems, simple visit the download page of composer, download the installer and run it. Just make sure you have PHP on your system (it is a PHP dependency manager after all).

Install composer on Mac OS

In order to install composer  on mac os follow the steps shown below:

Open your mac terminal window and run following commandsInstall composer on linux or ubuntu

Open linux terminal window and run following commands in order to install composer:

# get the composer phar file
curl -sS https://getcomposer.org/installer | php

# now we move composer.phar file to a executable directory
sudo mv composer.phar /usr/local/bin/

# we need to let our computer to run composer command globally
# in order to do that we need to add entry in our bash_profile file
# open bash_profile file using nano editor
nano ~/.bash_profile

# add this line below to bash_profile and save using CMD + x + Enter
alias composer="php /usr/local/bin/composer.phar"

# once file is saved we need to run following command to activate our changes
source ~/.bash_profile

# finally, composer installed on mac os and you can verify using any of
# the following commands
composer --version
composer --V

Install composer on linux or ubuntu

Open linux terminal window and run following commands in order to install composer:

# update package manager first
sudo apt-get update

# make sure you have curl installed first
# if you do not have curl installed run following
# command to install curl on your linux operating system
sudo apt-get install curl

# install composer globally
curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer

# verify the composer installation
composer -V

What Is Packagist?

Now, you’re aware of the basics of Composer and how to install it. The next question is: how do you know which libraries are available that you could install with Composer? Is there any central repository where Composer keeps a list of available libraries?

Packagist is the default Composer package repository from where Composer pulls libraries and its dependencies when you ask it to install a specific library. There are hundreds of libraries available on Packagist, which shows the popularity of Composer. In your PHP projects, if you need a feature which you think should be already available as a third-party library, Packagist is the first place you should check!

When it comes to searching libraries, Composer is also capable of looking beyond the Packagist repository. You could ask Composer to look at repositories other than Packagist  for installing libraries by altering the repositories key in the composer.json file. In fact, this is what you'll do if you want to manage your own private Composer packages.

How to Use Composer

Mostly, there are two ways when it comes to installing libraries with Composer. Let’s quickly go through it to understand how it works.

The install Command

To use install, you need to create a composer.json file in your project first. In the composer.json file, you just need to declare your project dependencies, as shown in the following snippet.

{
    "require": {
        "phpmailer/phpmailer": "~6.1"
    }
}

Next, when you run the composer install command from that folder, Composer installs the phpmailer package and its dependencies in the vendor directory. More importantly, it also creates the composer.lock file, which maintains a list of all of the packages and the exact versions of them that are installed.

The require Command

We can say that the composer require command is a sort of shortcut for the previous process of creating a composer.json file. require will add a package to your composer.json file automatically. The following command shows how to install the phpmailer package with the help of require.

composer require phpmailer/phpmailer

After installing the phpmailer package and its dependencies, require also adds an entry of the package which is installed in the composer.json file. If the composer.json file doesn’t exist, it’ll be created on the fly. Of course, it also updates the composer.lock file to write package information, along with the exact versions.

Conclusion

Package management is clearly the right route forward for PHP. 

Many of the most popular frameworks have already positioned themselves to use Composer, and many more individual developers are releasing code that is Composer ready from the get go.

As a PHP developer, Composer will become your best friend, and as it’s usage increases, it will become an essential part of writing PHP on a day-to-day basis.

By following conventions, PHP can become a much better language to use. Composer has already solved a big problem in the PHP community, so there’s really no reason why you shouldn’t start using it today.