In today’s tutorial, you’ll learn how to install Composer step-by-step on Pop!_OS 22.04 LTS.
Introduction
Composer is a well-known open-source dependency management tool for PHP, created mainly to help distribute and maintenance of project dependencies. It checks which packages a specific project depends on and installs them using the right versions required by the project. PHP frameworks such as Laravel and Symfony use Composer to generate new projects.
Requirements
You will need access to a Linux system as a non-root sudo user, to follow this tutorial.
Step 1 — Install PHP and Additional Dependencies
Composer requires php-cli to enable you to execute PHP scripts in the terminal, and also to extract zipped archives. You will also need to make sure you have git and curl installed already.
You can check if git and curl are installed by running git -v && curl -V.
If you don’t have them installed, you can run sudo apt-get install git && curl
to install git and curl on your Linux system.
Now you can go ahead and install the php-cli
.
First, update the package lists by running:
sudo apt update
Next, run the command below to install the required packages:
sudo apt install php-cli unzip
You will receive a prompt to confirm installation; type Y and then ENTER to confirm and proceed.
Once the prerequisite dependencies are installed, you can install Composer.
Step 2 — Download and Install Composer
Composer is installed using an installer script written in PHP. We’ll download and verify that it’s not corrupted before we use it to install Composer.
Be sure you are in your home directory, then curl to access and download the installer:
cd ~
curl -sS https://getcomposer.org/installer -o /tmp/composer-setup.php
Next, we need to verify that the installer we downloaded matches the SHA-384 hash for the latest installer found on the Composer Public Keys / Signatures page. To help the verification step, you can run the following command in your terminal to programmatically obtain the latest hash from the Composer page and store it in a shell variable:
HASH=`curl -sS https://composer.github.io/installer.sig`
If you want to verify the obtained value, you can run the following:
echo $HASH
Output
e0012edf3e80b6978849f5eff0d4b4e4c79ff1609dd1e613307e16318854d24ae64f26d17af3ef0bf7cfb710ca74755a
Now, verify that the installation script is safe to run by running the following PHP code provided on the Composer download page:
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 in your terminal:
Output
Installer verified
If the output reads Installer corrupt, then you’ll need to download the installation script again and cross-check that you’re using the correct hash. Afterwards, repeat the verification process. Continue only when you have a verified installer.
Moving on to install composer globally, run this command:
sudo php /tmp/composer-setup.php --install-dir=/usr/local/bin --filename=composer
This will download and install Composer as a system-wide command named composer
, under /usr/local/bin
:
After the command runs successfully, you’ll see output similar to this:
Output
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
Finally, to test your installation, run:
composer
Output
______
/ ____/___ ____ ___ ____ ____ ________ _____
/ / / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__ ) __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
/_/
Composer version Composer version 2.2.9 2022-03-15 22:13:37
Usage:
command [options] [arguments]
Options:
-h, --help Display this help message
-q, --quiet Do not output any message
-V, --version Display this application version
--ansi Force ANSI output
--no-ansi Disable ANSI output
-n, --no-interaction Do not ask any interactive question
--profile Display timing and memory usage information
--no-plugins Whether to disable plugins.
-d, --working-dir=WORKING-DIR If specified, use the given directory as working directory.
--no-cache Prevent use of the cache
-v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
...
This output verifies that Composer was installed successfully on your Linux system and is available globally or system-wide.