How to Install PHP Composer on Ubuntu 20.04


PHP Composer is a dependency management tool for PHP. It is widely used in the PHP development community to manage and install libraries or packages required by a PHP project. Actually, Composer simplifies the process of managing dependencies in PHP projects by providing a standardized and efficient way to handle package installation, versioning, and autoloading.

This tutorial will guide you on how to install PHP Composer on a Linux Ubuntu 20.04 system. let's start!

How to Install PHP Composer on Ubuntu 20.04?

Step 1: Install PHP and Other Dependencies

To ensure that Composer functions properly on your Ubuntu 20.04 system, some dependencies need to be installed. These dependencies include git, curl, php-cli, and unzip.

First, run the following command to check for any new available packages and update the list of available packages on your system.

sudo apt update
php composer install

Next, execute the following commands to install php-cli and unzip on your Ubuntu system, allowing you to run PHP scripts in the command line and extract ZIP files.

sudo apt install php-cli unzip
php composer install

When the system prompts: Do you want to continue? Enter Y and then enter ENTER.

php composer install
Explanation of command meaning

sudo: This is a command that allows you to execute subsequent commands with administrative or superuser privileges.

apt: This is the package management command-line tool on the Ubuntu system. It is used to install, update, and uninstall software packages.

install: This is a parameter of the apt command, indicating that you want to install the specified software packages.

php-cli: This is a command-line version of PHP that provides the ability to run PHP scripts in the command-line interface.

unzip: This is a utility program used to extract files and directories from ZIP archives.

Step 2: Download and Install Composer

The following command switches the current directory to the home directory of the current user. This helps in organizing files and simplifying subsequent installation steps.

cd ~

Use the curl command to download the Composer installation script from the specified URL and save it to the file /tmp/composer-setup.php.

curl -sS -o /tmp/composer-setup.php
php composer install
Explanation of command meaning

curl: This is a command-line tool used for URL transfers. Here, it is used to download the Composer installation script.

-sS: This is one of the parameters of the curl command. -s enables silent mode, preventing curl from displaying progress or error messages during the execution. -S ensures that error messages are still shown if any occur.

-o /tmp/composer-setup.php: This is a parameter of the curl command, specifying that the downloaded content should be saved to the specified file path /tmp/composer-setup.php. -o stands for output, followed by the desired file path.

/tmp: The /tmp directory is typically used for temporary file storage and is automatically cleaned up after the system restarts.

Execute the following command to retrieve the hash value of the Composer installation script from the specified URL ( and save it in a variable named HASH. This hash value will be used in the subsequent steps to verify the integrity of the downloaded Composer installation script, ensuring that the script has not been tampered with or corrupted.

HASH=`curl -sS`

If you want to verify the obtained hash, you can run the following command:

echo $HASH
php composer install

Now execute the following PHP command provided on the Composer download page. If the hash value of the installation script matches the expected hash value, it will output 'Installer verified.' If the hash value doesn't match, it will output 'Installer corrupt' and delete the installation script file. This verification step ensures that the downloaded Composer installation script hasn't been tampered with or corrupted, validating the safe execution of the installation script.

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

You will see the following output:

Installer verified

If the output displays 'Installer corrupt,' you will need to download the installation script again and carefully check if you are using the correct hash value. Then, repeat the verification process. Once you have a verified installer, you can proceed.

To install Composer globally, please use the following command. This command executes the PHP script /tmp/composer-setup.php with sudo privileges. The script will install Composer in the specified installation directory /usr/local/bin and name the executable file as composer.

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

Step 3: Test Installation

To test your installation, run the following command:


This will verify if Composer has been successfully installed on your system and is available system-wide. As shown in the image below, Composer has been successfully installed on the Ubuntu system and is available system-wide.

php composer install