How to install Composer on Ubuntu 22.04?

Overview

Composer is a tool frequently used for managing dependencies in PHP. Its main purpose is to simplify the installation and updating process of project dependencies. Composer identifies the packages that a project relies on and installs them, utilizing the versions that are compatible with the project's specifications. It is also a popular choice for creating new projects based on well-known PHP frameworks.

Prerequisites

There are certain prerequisites that need to be met before you begin.

  • Ubuntu 22.04 LTS configured on your system.

  • Non-root sudo user privileges.

Get Started

Step 1: Configuring PHP and other dependencies

  • Update the package index:

sudo apt update
  • Proceed with installing the necessary prerequisites, such as php-cli:

sudo apt install php-cli unzip

Step 2: Installing Composer

  • Ensure that you are in the home directory and then use curl to obtain the Composer installer:

cd ~
curl -sS https://getcomposer.org/installer -o /tmp/composer-setup.php
  • Use the 'curl' command to retrieve the latest signature and save it in a shell variable:

HASH=`curl -sS https://composer.github.io/installer.sig`
  • Confirm the acquired value by executing the following command:

echo $HASH

Visit the Composer Public Keys / Signatures page and confirm that the installer matches to the SHA-384 hash of the most recent installer.

Note: To proceed, you must ensure that the installation script is not corrupted and that you have used the correct hash. If you encounter an 'Installer corrupt' error message, re-download the installation script and verify it again. Only after obtaining an 'Installer Verified' message should you proceed further.

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;"
  • To set up Composer globally, execute the command below. This will fetch and install Composer as a command that can be accessed from anywhere in the system, and it will be called "composer", located in the /usr/local/bin directory.

sudo php /tmp/composer-setup.php --install-dir=/usr/local/bin --filename=composer
  • Verify your installation:

composer

Conclusion

This confirms that Composer has been installed on your system successfully and is accessible throughout the system.

Last updated