How To Install Composer On Ubuntu 22.04 LTS

By bhagwatchouhan
How To Install Composer On Ubuntu 22.04 LTS

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.


It assumes that PHP is already installed on the system. You can also follow How To Install PHP 8 On Ubuntu 22.04 LTS.

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 22.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 -o composer.php

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

# Get the latest HASH
HASH=`curl -sS`

# Check the HASH
echo $HASH

# Output

# 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 2.4.4) successfully installed to: /usr/local/bin/composer Use it: php /usr/local/bin/composer

Also, verify the installation as shown below.

# Verify Installation

# Output
______ / ____/___ ____ ___ ____ ____ ________ _____ / / / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/ / /___/ /_/ / / / / / / /_/ / /_/ (__ ) __/ / \____/\____/_/ /_/ /_/ .___/\____/____/\___/_/ /_/ Composer version 2.4.4 2022-10-27 14:39:29 Usage: command [options] [arguments] Options: -h, --help Display help for the given command. When no command is given display help for the list command .......

The Composer version 2.4.4 gets installed on my system. 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
Info from #StandWithUkraine Using version ^6.7 for phpmailer/phpmailer ./composer.json has been created Running composer update phpmailer/phpmailer Loading composer repositories with package information Updating dependencies Lock file operations: 1 install, 0 updates, 0 removals - Locking phpmailer/phpmailer (v6.7.1) Writing lock file Installing dependencies from lock file (including require-dev) Package operations: 1 install, 0 updates, 0 removals - Downloading phpmailer/phpmailer (v6.7.1) - Installing phpmailer/phpmailer (v6.7.1): Extracting archive 6 package suggestions were added by new dependencies, use `composer suggest` to see details. Generating autoload files 1 package you are using is looking for funding. Use the `composer fund` command to find out more! No security vulnerability advisories found

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.7"

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:

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 as explained at - How To Autoload PHP Classes Using Composer.


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

share on :

Profile picture for user bhagwatchouhan