Back to Top

A Beginner’s Guide To Composer

A Beginner’s Guide To Composer

A composer is a tool for dependency management in PHP. It basically allows you to declare your project dependent libraries and helps to manage dependencies.The composer is one of the most recommended tools which is used in all modern PHP frameworks and solves fundamental issues in the projects. A Guide To Composer explains all about the composer and it’s usage.

The Composer Dependency management tool is very helpful to easily set different useful things and facilitates the installation, upgrade, and support for various libraries in your project

This article will give you an introduction to Composer and how to manage it in PHP. It will also explain what you need to change in your deployment process after integrating composer.

What is a dependency manager and Why do I need one?

Sometimes you write the code which is dependent on third party libraries, For example, you are using PHPExcel or bootstrap etc in your application and you require to update the library with new version release. All the libraries are dependent on the project so Composer allows you to manage dependencies easily and also you can upgrade all the dependent libraries easily.

All the third party tools managed through packages or we can say every dependency is for a package. Package constitutes a local file or folder, remote file or folder, local or remote Git repository etc.Most of the developers use a global registry for available packages. Packagist is most widely used for Composer.

For third party PHP library and package, instead of manually download and add them to our code base, the composer should be used instead.

There is also a Packagist repository so that you can find php packages here.

How to install Composer?

Installing Composer is pretty simple just it requires PHP 5.3 or greater version. You can follow the instructions for either *nix or Windows.

More information about composer setup can be found here: Introduction – Composer

Install/Update package with composer

To install the composer, Run the following line in your terminal to get the latest Composer version:

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

The composer is a package manager that works according to the rules written in composer.json.

For example,

  "require": {
     "phpoffice/phpexcel": "^1.8"

composer.json is the required file to use the composer. Composer.json is mainly used to get the information about what packages you’re using in your project. Here you can see require takes an object that maps package names and version.

How to Manually Download Composer?

If you prefer to download the phar manually, you can get the latest version here

Once the composer is installed into your local development area, run `composer update` to update all existing packages.This command will updates all installed (or reinstalled accidentally deleted) packages to the latest versions.

Packages will be stored in the vendor directory. We won’t track this via our repository, rather, we tracked the composer.json and composer.lock file.

To install or use new PHP lib/package, first you need to check if they are available on and you just have to run composer require package-name to add new PHP package.

For example, find the PHPExcel package on and then run

composer require phpoffice/phpexcel


The best way to add a new dependency to the file composer.json is to use the following command.

composer require somepackage/somepackage:someversion

This will add all the necessary dependencies to file.

If you need to add the packages to the section the require-dev , add the command option –dev :

composer require phpunit/phpunit --dev

Also, a require allows you to add multiple packages at the same time, just simply separate it by a space.

When you install new packages, the composer.json and composer.lock file will be updated. This will make sure all developers will be using the same third party packages.

More information can be found on the composer basic usage page here:

Load third party packages into code base to use

To load/include the third party package into the code base to use, instead of manually using PHP include to call for the library, composer provides an easy way to load the packages. Simply put:

require 'vendor/autoload.php';

at the top of the PHP file that you are going to use the third party lib.

When installing packages Composer iterates by their rules and generates the most important file – / the vendor / autoload.php ,

Composer solves the following problems:

  1. To solve the dependency of PHP packages
  2. Provide autoload solution for PHP packages
  3. Easily update all packages

There is a brief overview of features.There are only two package management and autoloading classes and without the composer, both of these tasks take a lot of time.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

Drupal Tutorial for Beginners

Posted on 6 years ago


Table layout using DIV tags

Posted on 6 years ago


How to check user is logged in Modx

Posted on 7 years ago


Hello world

Updated 1 month ago

Nick Carter

In depth Understanding of Laravel Core

Updated 3 months ago


ECMAScript 6 New Features – 2

Updated 1 year ago


The Reader’s Poll – June 2015

Posted on 4 years ago


The Reader’s Poll – August 2014

Posted on 5 years ago


The Reader’s Poll – June 2014

Posted on 5 years ago


The Readers’ Poll – May 2014

Posted on 5 years ago


The Readers’ Poll – August 2013

Posted on 6 years ago