Install and Use Composer on Ubuntu 22.04

October 17, 2022 · 0 min read
Install and Use Composer on Ubuntu 22.04
Composer is a popular PHP dependency management tool designed to make project dependency installation and updates easier. It will determine which other packages a specific project requires and install them for you, using the correct versions based on the project requirements. Composer is also frequently used to launch new projects based on popular PHP frameworks like Symfony and Laravel.

Installing PHP and Other Dependencies

To run PHP scripts from the command line, Composer requires php-cli, and unzip to extract zipped archives.
In addition to prerequisites such as git and curl, which should already be available on your Ubuntu 22.04 system.
These dependencies are now being installed.
To begin, run the following command to clear the package manager cache.

sudo apt update

Run the following command to install the necessary packages

sudo apt install php-cli unzip

When prompted, type Y and then ENTER to confirm the installation.
After installing the requirements, you may begin installing Composer.

Downloading and Installing Composer

Composer includes a PHP-based setup script. We'll get it, make sure it's not corrupted, and then utilize it to install Composer.
Make sure you're in your home directory, then use curl to get the installer

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

The downloaded installer will then be compared to the SHA-384 hash for the most recent installer found on the Composer Public Keys / Signatures page. To speed up the verification process, use the following command to grab the most recent hash from the Composer page and store it in a shell variable:

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

If you wish to double-check the result, run:

echo $HASH
55ce33d7678c5a611085589f1f3ddf8b3c52d662cd01d4ba75c0ee0459970c2200a51f492d557530c71c15d8dba01eae

Now, run the PHP code supplied on the Composer download page to ensure that the installation script is safe to run:

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

You’ll see the following output:

Installer verified

If the output says Installer corrupt, you should redownload the installation script and double-check that you're using the right hash. The verification procedure should then be repeated. You can proceed once you have a validated installer.
To install composer worldwide, run the following command, which will download and install Composer as a system-wide tool called composer in /usr/local/bin: composer.

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

You'll get something like this:

All settings correct for using Composer
Downloading...

Composer (version 2.2.9) successfully installed to: /usr/local/bin/composer
Use it: php /usr/local/bin/composer

To test your installation, run:

composer

Conclusion

Composer is a powerful tool that may make handling dependencies in PHP projects much easier. It provides a dependable method for locating, installing, and upgrading PHP packages on which a project depends. In this guide, we learned how to install Composer, add new dependencies to a project, and update these dependencies as new versions become available.