Dependency Management is one of the important aspects of programming software using re-usable packages, libraries, and modules. Almost all the popular programming languages are supported by dependency management tools to manage project dependencies. We can manage the project dependencies in PHP using Composer. It resolves all the project dependencies and installs the dependencies using the appropriate version based on the version specified by the project configuration.

 

This tutorial provides all the steps required to install Composer on Ubuntu 20.04 LTS. The steps should be similar on other versions of Ubuntu and Linux systems.

 

Prerequisites

 

It assumes that PHP is already installed on the system.  

 

Install Composer

 

This section provides the steps to download the PHP script required to install Composer on Ubuntu. It also shows how to verify this script and install Composer on Ubuntu 20.04.

 

# Refresh Packages
sudo apt update

# Install Required packages
sudo apt install php-cli php-zip zip unzip

# Install CURL
sudo apt install curl

 

Now download the installer script as shown below.

 

# Setups or Home Directory

# Download installer script
curl -sS https://getcomposer.org/installer -o composer.php

 

We will also verify the installer script before installing the Composer as shown below.

 

# Get the latest HASH
HASH=`curl -sS https://composer.github.io/installer.sig`

# Check the HASH
echo $HASH

# Output
e0012edf3e80b6978849f5eff0d4b4e4c79ff1609dd1e613307e16318854d24ae64f26d17af3ef0bf7cfb710ca74755a

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

# Output
Installer verified

 

This completes the verification of the installer script. Now we can continue with the installation of the Composer on Ubuntu. In case the verification steps generate the output – Installer corrupt, we have to download the new installation script and verify it as shown above.

 

Now install Composer globally as shown below.

 

# Install Composer - Globally
sudo php composer.php --install-dir=/usr/local/bin --filename=composer

# 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

 

Also, verify the installation as shown below.

 

# Verify Installation
composer

# Output
   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/
Composer version 1.10.7 2020-06-03 10:03:56

Usage:
  command [options] [arguments]

Options:
  -h, --help                     Display this help message
-----
-----

# Check Version
composer --version

# Output
Composer version 1.10.7 2020-06-03 10:03:56

 

The Composer version 1.10.7 was installed while writing this tutorial. Now we can use the command composer to add and manage project dependencies.

 

Using Composer

 

In this step, we will learn using the composer in our projects. The composer specific configuration for each project can be managed using the composer.json file placed at the root of the project. We can either generate this file manually or it will be generated on adding the first dependency.

 

I have added the PHPMailer as the first dependency to my project using the command as shown below.

 

# Install PHPMailer as project dependency
composer require phpmailer/phpmailer

# Output
Using version ^6.1 for phpmailer/phpmailer
./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 phpmailer/phpmailer (v6.1.4): Downloading (100%)
phpmailer/phpmailer suggests installing psr/log (For optional PSR-3 debug logging)
phpmailer/phpmailer suggests installing league/oauth2-google (Needed for Google XOAUTH2 authentication)
phpmailer/phpmailer suggests installing hayageek/oauth2-yahoo (Needed for Yahoo XOAUTH2 authentication)
phpmailer/phpmailer suggests installing stevenmaguire/oauth2-microsoft (Needed for Microsoft XOAUTH2 authentication)
phpmailer/phpmailer suggests installing symfony/polyfill-mbstring (To support UTF-8 if the Mbstring PHP extension is not enabled (^1.2))
Writing lock file
Generating autoload files

 

It generates the composer.json, composer.lock files at the project root directory. It also creates the vendor directory having the PHPMailer as the dependency. It also creates the autoload.php file within the vendor directory.

 

The composer.json file content is shown below.

 

{
    "require": {
        "phpmailer/phpmailer": "^6.1"
    }
}

 

We can also manually update this file to add further dependencies or simply run the command composer require to add the required dependencies. In case we update this file manually, we must run the command composer update to apply the changes.

 

We must add the autoload.php file within the project’s PHP files to use the dependencies. It can be done as shown below:

 

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

use PHPMailer\PHPMailer\PHPMailer;

....
....
....

$mailer = new PHPMailer( true );

....
....

 

The above lines include the PHPMailer dependency to the PHP file. We can create the PHPMailer object without including its file since the autoload.php file add it as a dependency.

 

Autoload Project Classes

 

We can also autoload the project classes by adding the classes in the src directory and configuring the project composer file .

 

Summary

 

We have successfully installed the most recent version of Composer on Ubuntu 20.04 LTS and also learned to use it in our projects to manage the dependencies.