Thursday, March 14, 2024

How to Install Composer on Ubuntu 22.04


Composer stands out as a robust dependency management tool tailored for PHP projects. Its functionality streamlines the installation and organization of project dependencies, making it an indispensable asset for developers. In this tutorial, we'll guide you through the seamless process of installing Composer on your Ubuntu 22.04 system, ensuring you're equipped to harness its capabilities effectively.

To embark on this journey, you'll need access to an Ubuntu 22.04 server. Additionally, make sure you're logged in with a non-root sudo user account, and have your firewall enabled to maintain security throughout the process.

Step 1 — Setting Up PHP and Essential Dependencies

Kickstart the installation process by refreshing the package manager cache:

sudo apt update

Next up, install the requisite packages:

sudo apt install php-cli unzip

Step 2 — Securing Composer: Download and Verify

Navigate to your home directory and procure the Composer installer:

cd ~

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

Retrieve the SHA-384 hash for verification:

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

Echo the obtained hash to maintain reference:

echo $HASH

After running the commands above, you should see an output similar to the following:

dac665fdc30fdd8ec78b38b9800061b4150413ff2e3b6f88543c636f7cd84f6db9189d43a81e5503cda447da73c7e5b6

Proceed to verify the integrity of the installation script using the retrieved hash:

php -r "if (hash_file('SHA384', '/tmp/composer-setup.php') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('/tmp/composer-setup.php'); echo 'Download the installer again and double-check the hash. Then, repeat the verification process.'; } echo PHP_EOL;"

Upon verification, you should receive either of the following outputs:

  • If the installer is verified: "Installer verified"
  • If the installer is corrupt: "Installer corrupt" along with instructions to download the installer again and revalidate the hash.


Once the installer's integrity is confirmed, proceed with the installation:

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

After executing the above command, you should receive an output similar to the following:

All settings correct for using Composer

Downloading...

Composer (version 2.7.2) successfully installed to: /usr/local/bin/composer

Use it: php /usr/local/bin/composer

This command ensures Composer is downloaded and installed globally, accessible as a system-wide command named composer, residing under /usr/local/bin.


Step 3 — Verifying Composer Installation

To ascertain that Composer is successfully installed, run the following command:

composer --version


With this, you're all set to harness the power of Composer for your PHP projects. Happy coding!

0 comments:

Post a Comment