Composer is a package manager for the PHP programming language, its frameworks (i.e. Laravel, Symphony, CodeIgniter and more), Ecommerce platforms (i.e. Magento) and Content Management Systems (i. e. Drupal, SilverStripe) . Developed by Nils Adermann and Jordi BoggianoIt (2012), it deals with software distributions as well as performing package-related operations from installing to updating to removing. As a dependency manager, Composer handles necessary libraries and dependencies.
There are several ways to download and install Composer.
- Through script: Running this script initiates the installer, runs and after successfully installing, removes the installer:
php -r “copy(‘https://getcomposer.org/installer’, ‘composer-setup.php’);”
php -r “if (hash_file(‘sha384’, ‘composer-setup.php’) === ‘e0012edf3e80b6978849f5eff0d4b4e
echo ‘Installer verified’;
echo ‘Installer corrupt’;
} = echo PHP_EOL;”
php composer-setup.php php -r “unlink(‘composer-setup.php’);”
This script has been developed for version 1.10.1 (As of 2020-03-13). Kindly, have a look at https://getcomposer.org/download to see changes for newer versions.
- Directory-specific Installation: To install to a specific directory, open your command-line terminal and run:
php composer-setup.php –install-dir=bin
To download a specific version, for example, ” 1.0.0-alpha8”, add the –version flag like below:
php composer-setup.php –version=1.0.0-alpha8
- Manual Downlaod: To manually download, visit https://getcomposer.org/download/ and click on your preferred version.
Composer commands can be run flexibly from the cmd (command line interface). Widely used Composer commands are mentioned below:
- Adding Packages: To add any package to your project, open your command-line and type:
composer require vendor/package
Example: composer require laravel/socialite
(adding –dev inserts the package into composer.json’s require-dev section and then install)
- Installing Libraries & Dependencies: To install libraries and dependencies mentioned in the composer.json file: composer install
- Updating Packages: There are various types of updating options available.
(a) To update all the packages: composer update
(b) To update all the packages along with their dependencies:
composer update –with-dependencies
(c) To update a certain package: composer update vendor/package
(d) To update a specific vendor’s all packages: composer update vendor/*
(e) To update composer.lock without updating any package:
composer update –lock
(f) To update the autoloader: composer dumpautoload
(adding -o ensures optimization)
- Removing Packages: To remove a specific vendor & its package name from the composer.json and uninstall that package:
composer remove vendor/package
Composer is the de facto dependency cum package manager for PHP and its allies. Moreover, packages from Packagist can be easily installed using Composer.