How to Install Composer on Ubuntu 24.04, 22.04, 20.04

Composer is a widely used dependency manager for PHP, allowing developers to manage libraries and packages efficiently within their projects. It automates the process of installing, updating, and managing the various packages and libraries that a PHP project might depend on. Composer ensures that the correct versions of dependencies are installed and can handle complex dependency trees, making it an essential tool for PHP development.

On Ubuntu 24.04, 22.04, or 20.04, you can install Composer using the command-terminal by utilizing the official “getcomposer” installer. This method provides the latest version of Composer directly from the source, ensuring that you have access to the most up-to-date features and security improvements. This guide will walk you through the steps to install Composer on your Ubuntu system, enabling you to manage your PHP dependencies effectively.

Composer Pre-Installation

Update Ubuntu Before Composer Installation

Start by refreshing your system’s local package database. The command sudo apt update does exactly that – it resynchronizes the package index files from their sources specified in /etc/apt/sources.list. This ensures your system is aware of the latest updates and versions of packages.

sudo apt update

Upgrade Existing Packages

To maintain your system’s stability and security, upgrading the packages already installed on your machine is a good practice. By executing sudo apt upgrade, you allow the system to install available upgrades of all packages currently installed from the sources configured via APT.

sudo apt upgrade

Install PHP CLI and Unzip

Now, we will install the necessary packages that Composer depends upon. The php-cli package gives us the ability to run PHP scripts on the command line, while php-zip will be used to extract downloaded packages, and php-curl enables fetching data from URLs.

To install these packages, you can use the following command:

sudo apt install php-cli php-zip php-curl

After this step, you have successfully installed all the necessary prerequisites and are ready to dive into installing Composer on your system.

Download and Install Composer via CLI Commands

The process of installing Composer revolves around an installer script written in PHP, which Composer itself provides. The primary steps involve downloading this script, ensuring its integrity, and finally using it to set up Composer on your system.

Fetch the Composer Installer

Begin by navigating to your home directory. Following that, employ the curl command to download the installer. The -sS flags make curl silent while still showing errors, and -o directs the output to a specific location.

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

Verify the Installer’s Integrity

Composer provides the latest installer’s SHA-384 hash on its Public Keys/Signatures page. We need to compare the hash of the downloaded file with this to ensure its integrity.

You can fetch the latest hash programmatically from the Composer page and store it in a shell variable:

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

To inspect the fetched hash, execute the following:

echo $HASH

Proceed to verify the integrity of the installation script by matching the hash of the downloaded file with the obtained hash. The following PHP command checks if they match:

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

Upon running this command, if the output displays ‘Installer verified’, it means the installer script is good to go. If it says ‘Installer corrupt’, re-download the script, ensure you’re using the correct hash, and repeat the verification process.

Install Composer

With the verified installer, you’re now ready to install Composer. We’ll be doing a global installation, which means Composer will be accessible system-wide as a command named ‘composer’, residing under /usr/local/bin. Use the following command:

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

If all goes well, you should see output indicating a successful installation:

All settings correct for using Composer
Downloading...

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

Validate Composer Installation

To confirm that Composer has been installed correctly and is functioning as expected, run:

composer

This should display the Composer version and a list of available commands, confirming its successful installation on your system.

Note: If you prefer having distinct Composer executables for each project on your server, or if your system user lacks the necessary permissions to install software system-wide, you might want to consider local installations. These are per-project based and can be accomplished using the command php /tmp/composer-setup.php, which generates a composer.phar file in your current directory, executable via php composer.phar.

Utilize Composer in a PHP Project

In this section, we will delve deeper into the practical usage of Composer in a PHP project. By illustrating its functionality with an example, we aim to provide you with an understanding of how to manage dependencies effectively in your projects.

Initialize Composer in Your Project

To commence, let’s first navigate to the directory of your project. Once you are within the project directory, initiate Composer. This action creates a composer.json file, which is essentially the blueprint for your project’s dependencies.

cd /path/to/your/project
composer init

Define Your Project Dependencies

After initializing Composer, the next step involves defining the dependencies for your project. This can be achieved using the “require command” followed by the package name and the version you wish to use.

For instance, if you would like to include the monolog/monolog package, a popular logging library for PHP, you would execute:

composer require monolog/monolog

The require command fetches the necessary package and its dependencies and automatically updates the composer.json file. Moreover, the installed packages are stored in the vendor directory, and an autoloader script is created, ensuring that all the dependencies are correctly loaded when needed.

Utilize Your Dependencies

Now that the required dependencies are installed, you can use them in your project. The easiest way to use the Composer autoloading feature is by including the autoloader in your PHP scripts.

require __DIR__ . '/vendor/autoload.php';

After adding this line to your PHP script, you can easily use any classes from your installed dependencies without worrying about requiring each one individually.

Keep Your Dependencies Up to Date

Maintaining your project’s dependencies up to date is crucial. With Composer, updating is a breeze. Navigate to your project directory and execute the update command. This command checks for newer versions of the libraries specified in your composer.json file and updates them if necessary.

cd /path/to/your/project
composer update

Closing Thoughts

Installing Composer on Ubuntu using the “getcomposer” installer provides you with a powerful tool for managing PHP dependencies in your projects. The process is straightforward and ensures that you are working with the latest version of Composer. Once installed, Composer simplifies the management of libraries and packages, allowing you to focus on development rather than dependency management. Regularly updating Composer will keep your development environment secure and up-to-date, ensuring compatibility with the latest PHP packages.

Leave a Comment