Categories
Developers Linux

How to Install and Use PHP Composer on Ubuntu 20.04 LTS

Compose your PHP project with ease by letting composer manage all the dependencies

Composer is a dependency management tool for PHP. It differs from the traditional package manager such as apt and dnf, in a way such that it doesn’t install the packages & libraries globally on a system-wide level rather it handles them for each project separately. Thus it is a dependency manager and not a package manager.

Much like how a music composer takes a bunch of instruments and arranges them to work flawlessly in a concert, the Composer for PHP takes a bunch of libraries & frameworks, packages them to work together and create a solid foundation on which a PHP project can be composed.

Prerequisites

You need access to a Ubuntu 20.04 system with a non-root sudo user account. Additionally, you need to install some dependencies for Composer, which includes php-cli to run PHP scripts in your terminal, unzip to help Composer extract the downloaded packages and curl to download the composer installation script

To install all the required packages, update the Ubuntu package list by running the update command:

sudo apt update

Then install the php-cli, unzip and curl using the following command:

sudo apt install php-cli unzip curl

You will be prompted to confirm installation, press Y then hit enter. After you have fulfilled all the prerequisites, you can continue to install Composer.

Download and Install Composer

Composer offers a neat PHP script to download and install it on your machine from the command line. We need to use curl to download this script, verify its authenticity for security reasons and then execute it to install Composer.

Make sure your terminal is open in the home directory and then download the installation script by using curl:

cd ~ curl -sS https://getcomposer.org/installer -o composer-setup.php

Next, we need to verify the installation script we have downloaded is genuine by matching the SHA-384 hash of it to the one found on Composer Public Key/ Checksums page. To do so, obtain the SHA-384 hash from the Composer Public Key page and store it in a shell variable.

Hash=`curl -sS https://composer.github.io/installer.sig`

Then verify that you have succesfully obtained and stored the hash in the variable by running:

echo $Hash

You should get an output of some random string such as this from the terminal:

Output:
e0012edf3e80b6978849f5eff0d4b4e4c79ff1609dd1e613307e16318854d24ae64f26d17af3ef0bf7cfb710ca74755a

Now, run the following PHP code provided by Composer to verify the authenticity of the installation script:

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

If you see any other output such as Installer corrupt, then you’ll know that the script is corrupted and it is not safe to run. Download the script again using curl, then execute the PHP code in the terminal again to verify the installation script.

You can continue with the installation when you have verified installer successfully. You can install Install the Composer globally or locally for a single project.

Install Composer Globally

To install Composer globally as a system-wide command named composer, run the following command:

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

Composer will be installed in the /usr/local/bin directory on your Ubuntu 20.04 system and you will see output such as this:

Output:
All settings correct for using Composer
Downloading...
Composer (version 1.10.7) successfully installed to: /usr/local/bin/composer
Use it: php /usr/local/bin/composer

Verify that Composer is installed correctly and is functional by running:

composer
Output:
   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/
Composer version 1.10.7 2020-06-03 10:03:56
Usage:
  command [options] [arguments]

You have successfully installed composer on your Ubuntu 20.04 system. You can now start using Composer to manage, update and install dependencies in your PHP projects immediately.

Installing Composer Locally

Installing Composer locally is useful when you don’t have permission to install it on a system-wide level or if you want the Composer for a single project only. To install composer locally run:

php composer-setup.php

The above command will create a new file in your current directory called composer.phar. You will need to move this file in the project root folder to use the Composer features. This file can be run using the command shown below.

php composer.phar

Composer Basics

Now, that you have Composer installed on your Ubuntu 20.04 machine, let us look at some basics of Composer. Composer aims to facilitate easy installation and updates of dependencies and to do so it creates many files in the project root directory. Let us look at the directory structure of a project using Composer to manage dependencies.

Composer Project root Structure:
ProjectRoot/ ├── composer.json ├── composer.lock ├── Project.php ├── composer.phar *Only if you have installed Composer locally └── vendor ├── autoload.php ├── composer │   ├── ClassLoader.php │   ├── LICENSE │   ├── autoload_classmap.php │   ├── ..... ├── .......
  • The composer.json file located in project root directory stores all the information on the dependencies (packages) required by the project.
  • The composer.lock holds the information on the packages which are version locked for the project.
  • vendor is the directory where all the packages are stored, it also has some PHP scripts such as autoload.php, which facilitates the automatic inclusion of the packages in the vendor directory.
  • Finally, if you have installed Composer locally you must have composer.phar file in the project directory to install packages.

All these files are created when you use the composer or php composer.phar command to download and install packages for your project the first time. So let us look at the working of Composer by creating a Demo project.

Creating your First Project Using Composer

The first step is to create a root directory for your project, so create one with mkdir command and navigate to it by using cd command:

mkdir ~/ComposerDemo cd ~/ComposerDemo

Now, we need to find and install the packages/libraries required to build our demo project. Packagist is the main Composer repository which lists all the publicly available PHP packages that can be installed with Composer.

In this example, we will use a PHP package called cakephp/chronos, it is a simple API extension for date & time. Thus to generate a new Composer project and install Chronos package run the following command:

composer require cakephp/chronos
Output:
Using version ^2.0 for cakephp/chronos ./composer.json has been created Loading composer repositories with package information Updating dependencies (including require-dev) Package operations: 1 install, 0 updates, 0 removals - Installing cakephp/chronos (2.0.5): Downloading (100%) Writing lock file Generating autoload files

The require option fetches and installs the package you wish and generates the files and directories such as composer.json, composer.lock and vendor in the project root directory. You’ll see that cakephp/chronos is added to the composer.json if you run the following command:

cat composer.json
Output:
{ "require": { "cakephp/chronos": "^2.0" } }

Next let us make use of Chronos in our Demo project, open and create a PHP file called demo.php using nano:

nano demo.php

Then add the following code to the demo.php, the second line which includes vendor/autoload.php is a Composer file which automatically loads all the packages and libraries that are installed for the project. Save the file by pressing Ctrl+O and then exit the nano editor by pressing Ctrl+X.

<?php require 'vendor/autoload.php'; use Cake\Chronos\Chronos; printf("Now: %s \n", Chronos::now()); ?>

Execute the demo.php by running the following command:

php demo.php
Output:
Now: 2020-06-23 17:07:45

In future when you need to update the packages and libraries of your project, simply run the following command:

composer update

The above command will check for the newer version of installed packages and update them securely without breaking the project and its interdependent libraries.


We have looked at how to install Composer on Ubuntu 20.04 machine and looked at the basics you need to know about Composer. You can now try to build new projects by adding the required packages by searching on Packagist. If you wish to learn more about Composer and its option head on over to Composer online documentation page.