Installing PHP Composer on Ubuntu Bionic Beaver

Composer Overview

PHP Composer is a package dependency management tool for PHP similar to NPM for Nodejs and bundle for Ruby. Composer facilitates the installation and updates for project dependencies. In short, Composer will pull in all the required PHP packages your project depends on and manage them for you.


Installing Composer

1 . Update the packages index and install the necessary requirements:

apt update
apt install wget php-cli php-zip unzip

2 . Download the composer installer. The command below downloads the composer-setup.php file in the current working directory.

php -r "copy('', 'composer-setup.php');"

3 . Verify the data integrity of the script by comparing the script SHA-384 hash with the latest installer hash found on the Composer Public Keys/Signatures page. Download the expected signature of the latest Composer installer from the Composer’s Github page and store it in a variable named HASH:

HASH="$(wget -q -O -"

4 . Verify that the installation script is not corrupted

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

If the hashes match, you will see the following output: Installer verified

If it does not match, download the Composer installation script again and double check the value of the $HASH variable with echo $HASH. Once the installer is verified, you can continue with the next step.

5 . Install Composer in the /usr/local/bin directory

sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer

6 . Verify the installation


which returns

root@composer:~# composer
Do not run Composer as root/super user! See for details
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
Composer version 1.8.3 2019-01-30 08:31:33

Using Composer PHP Project

1 . Create a directory which will be the project root directory and which will hold the composer.jsonfile. This file describes your PHP project including the PHP dependencies and other metadata.

mkdir ~/composer-project1
cd composer-project1

2 . Initialize a new composer.json using the composer requirecommand and specify the package to download.

In this example we will create a sample application that will print the current time using a package called carbon. Carbon can help make dealing with date/time in PHP much easier and more semantic so that our code can become more readable and maintainable.

Note: You can search the Composer repository Packagist for PHP packages.

3 . Run the following command to initialize a new composer.json and install the carbon package:

composer require nesbot/carbon

Which returns

Using version ^2.11 for nesbot/carbon
./composer.json has been created
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 4 installs, 0 updates, 0 removals
  - Installing symfony/polyfill-mbstring (v1.10.0): Downloading (100%)
  - Installing symfony/contracts (v1.0.2): Downloading (100%)
  - Installing symfony/translation (v4.2.2): Downloading (100%)
  - Installing nesbot/carbon (2.11.0): Downloading (100%)
symfony/polyfill-mbstring suggests installing ext-mbstring (For best performance)
symfony/contracts suggests installing psr/cache (When using the Cache contracts)
symfony/contracts suggests installing psr/container (When using the Service contracts)
symfony/contracts suggests installing symfony/cache-contracts-implementation
symfony/contracts suggests installing symfony/service-contracts-implementation
symfony/translation suggests installing symfony/config
symfony/translation suggests installing symfony/yaml
symfony/translation suggests installing psr/log-implementation (To use logging capability in translator)
Writing lock file
Generating autoload files

Composer provides autoload capabilities which allows us to use PHP classes without the need to require or include the files.

Composer creates the composer.json. It also downloads and installs carbon and all its dependencies.If you list your project’s directory, it contains two files and a directory:

  • The vendor directory is the directory where the project dependencies are stored.
  • The composer.lock contains a list of all installed packages including the exact packages versions.
  • The composer.json describes the PHP project and all PHP dependencies.

4 . Create a file named testing.php and add the following code:


require __DIR__ . '/vendor/autoload.php';

use Carbon\Carbon;

printf("Now: %s", Carbon::now());

In the first line after the opening php tag, we include the vendor/autoload.php, automatically generated by Composer. It will autoload all of the libraries.

Then, we alias Carbon\Carbon as Carbon and with the last line we are printing the current time using the Carbon now method.

5 . Run the script:

php testing.php

Which returns

Now: 2019-02-01 11:11:56

Note: If you want to update your PHP packages you can simply run composer update

To learn more about Composer visit the official documentation.

Discover a New Cloud Experience

Deploy SSD Cloud Servers in seconds.