3 1 2
In one of my previous article, I got a question from one of the reader — what is Composer? How can we use it? PHP Composer is not the package manager like apt or yum. This is dependency manager influenced by NPM. In this article I will tell you about composer, way to install it on Ubuntu and how to use it.
#Introduction to PHP Composer
As per the definition given on the website
“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.”
Composer deals with packages and libraries and install it with in a directory called ‘vendor’. But, it manages them on project basis. So it’s a dependency manager, not the package manager.
Composer requires PHP version > PHP 5.3.2. Also, while installing any package it gives warning that please do not run composer with root user.
PHP composer is very easy to install. I am using Ubuntu that’s why I am attaching screen shot of Ubuntu Terminal here.
To install Composer open terminal and write command
$ curl -sS https://getcomposer.org/installer | sudo php — –install-dir=/usr/local/bin –filename=composer
The above command will first download composer.phar (PHP Archive) , Move it to ‘/usr/local/bin’ and then execute it to install.
To verify that composer has been installed on system open the terminal and type ‘composer’.
The above command will show the composer version, Usage, options and Available commands available for composer.
#Package Installation using Composer
Package installation using composer is really easy. You need to write just one line command and it will install the package. Suppose, you need to install monolog logger for your symfony project. Then, you need to write
$ sudo composer require monolog/monolog
Once you install a package, you need the ability to autoload the dependencies. Composer comes with an autoload.php file, which have capability to use all the dependencies installed using composer. You just need to add autoload.php into your bootstrap file.
<?php require_once '/vendor/autoload.php'; ?>
Packagist is main repository of composer packages. Means when we are searching or installing any package or library in our project basically we are hitting Packagist.
Packagist also encourages developers to submit their packages to strengthen the community.
I will tell you “How to submit your package to packagist?” in my another article.
That’s all from my side about composer. If you have more information about composer please use comment section or use “Speak up here” link.