Installing PHP Composer on Ubuntu Focal Fossa (20.04 LTS)

Composer Overview

PHP Composer is a package dependency management tool for PHP similar to NPM for Node.js 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.

Requirements

Installing Composer

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

apt update 
apt install wget php7.4-cli php7.4-zip unzip -y

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

php -r "copy('https://getcomposer.org/installer', '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 - https://composer.github.io/installer.sig)"

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

composer

which returns

root@composer:~# composer
Do not run Composer as root/super user! See https://getcomposer.org/root for details
   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/
Composer version 1.10.10 2020-08-03 11:35:19
[...]

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 file 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 file, and install the carbon package:

composer require nesbot/carbon

Which returns

Using version ^2.39 for nesbot/carbon
./composer.json has been created
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 5 installs, 0 updates, 0 removals
  - Installing symfony/translation-contracts (v2.1.3): Downloading (100%)
  - Installing symfony/polyfill-php80 (v1.18.1): Downloading (100%)
  - Installing symfony/polyfill-mbstring (v1.18.1): Downloading (100%)
  - Installing symfony/translation (v5.1.4): Downloading (100%)
  - Installing nesbot/carbon (2.39.0): Downloading (100%)
symfony/polyfill-mbstring suggests installing ext-mbstring (For best performance)
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
5 packages you are using are looking for funding.
Use the `composer fund` command to find out more!

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 open it in a text editor (for example: nano):

touch testing.php
nano testing.php

Then add the following code to the file, save it, and exit the text editor:

<?php

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: 2020-09-01 09:19:23

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 the Cloud That Makes Sense