Composer was introduced in March, 2012. It is a dependency manager for PHP. Dependency management is very simple concept. Suppose, you wan to use one PHP package in your project but that PHP package requires some other PHP packages. So you don’t need to download and load every PHP package separately. By using composer when you download a PHP package then all other required PHP packages also get downloaded automatically.
Composer also helps us to load downloaded packages automatically. Without composer, you will need to read instructions of different PHP packages and then load them individually. This is very time consuming job. Compose autoloads downloaded PHP packages. You just need to write the following line then all downloaded packages will be loaded automatically:
require_once ‘vendor/autoload.php’;
Composer uses two files composer.json and composer.lock
composer.json file contains a list of all required libraries and versions of your project. composer.lock file contains what is currently installed. For more information, please read Composer Basic Commands
The key benefits of using composer are:
- Auto downloading dependent PHP packages
- Auto loading downloaded PHP packages
- Easy to update a PHP package and it’s all dependent packages.
- When you remove a PHP package using composer then all other unused PHP packages also get removed automatically.
- Easy to apply patches using composer
- Reducing development effort
One drawback of using composer is that you cannot make modifications in downloaded packages directly. Because when you update or download package then all made modifications will be lost. You should use patch to make modification in packages and patches should be included in composer.json
How to install Composer on Linux:
Prerequisites: you have an user with sudo privilege and PHP is already installed.
1. Download the Composer installer with command:
curl -Ss https://getcomposer.org/installer | php
2. Now you need to move downloaded the composer.phar file into the /usr/local/bin directory:
sudo mv composer.phar /usr/local/bin/composer
3. Now, you need to make it executable:
chmod +x /usr/local/bin/composer
Congratulations, you have installed Composer on your machine. Please run command to confirm it:
composer