Design a site like this with WordPress.com
Get started

Composer: PHP’s Package Manager

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.

Installation Process

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
    4c79ff1609dd1e613307e16318854d
    24ae64f26d17af3ef0bf7cfb710ca747
    55a’) {
    echo ‘Installer verified’;
    }
    else {
    echo ‘Installer corrupt’;
    unlink(‘composer-setup.php’);
    } = 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. 

Commands

Composer commands can be run flexibly from the cmd (command line interface). Widely used Composer commands are mentioned below:

  1. 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)
  2. Installing Libraries & Dependencies: To install libraries and dependencies mentioned in the composer.json file: composer install
  3. 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)
  4. 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.

Advertisement

Published by Farial Mahmod Tishan

Life-long learner. Developing Flutter apps on Parrot Linux OS .

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: