Krishnan's Personal Website


Home | About | Blog | Interesting Reads | Tutorials | Skills | Personal Projects | Books | Fun | Connect with me


What is composer and how is it useful?


Published On: Apr 12 2022
Written By: Krishnan Sethuraman
Category: PHP


For an everyday Php developer composer comes second to Php itself but for a new Php developer composer might be really confusing (that’s how it was for me). For a long time I was not able to get my head around on what composer was and how to use it in my projects. I would download the third party libraries and commit them along with my source code. 

As years went by the third party libraries started installation only via composer which put me in a tough spot and I had to figure on how I can use composer to install third party packages in my application.  

 I am writing this article with hopes to help some newbie programmer who might find it difficult to understand composer and its uses. 

So without further adieu let’s get to it. 

In layman’s term composer is that one file which has the record of all third party packages that might be used by the application. 

Installing composer

To add composer to your source code first we need to install composer on the local environment (ie your computer). The installation steps might change in your computer depending on the operating system. 

$ cd ~ 
$ curl -sS https://getcomposer.org/installer -o /tmp/composer-setup.php
$ sudo php /tmp/composer-setup.php --install-dir=/usr/local/bin –filename=composer

To verify composer installation type the following command.

$ composer

With composer installed on your computer the next step is to start using it in your application which can be achieved by creating a composer.json file in the root of your application.

$ vi composer.json

Add the following content to the composer.json file and save it.

{
    "name": "krish/webapp",
    "type": "project",
    "description": "My web app.",
}

After the composer.json file is created you can simply add required third party packages by simply typing something similar to the following command. Almost all third party packages have the command on their web page or github repo page. 

$ composer require vlucas/phpdotenv

Please note that all the third party packages will be installed inside a directory called vendor. While committing the source code to the repository the vendor folder should not be committed. Hence we need to add it to the .gitignore file. Committing the composer.json file is enough as it has all the information about the third party packages to run the application. 

With composer and the required third party packages added you can use them by adding the following line at the top of the source code file below the <?php tag. 

require __DIR__ . '/vendor/autoload.php';

Running composer on existing source code

As developers you will seldom start a project from scratch and mostly work on existing source code. So in that case after the source code is checked out run the following command after navigating to the location where you find the composer.json file in the checked out source code. 

$ composer install

This will create a new directory called vendor with all the required third party packages. 

Updating composer packages

To update the composer packages run the following command and all your packages will be updated to the latest stable version. 

$ composer update

Benefits of using composer

  1. with composer there is no need to search and download the third party packages to the source code. Composer takes care of the same. 
  2. By using composer the third party packages need not be committed along with the source code to the repository. Only composer.json file is enough. 
  3. Updating third party packages is super easy and can be done with just one command.