How to Install Composer on Ubuntu 24.04, 22.04 or 20.04

This guide will demonstrate how to install Composer on Ubuntu 24.04, 22.04, or 20.04 LTS using the command-line terminal.

Composer is a key tool in PHP development, simplifying dependency management and ensuring consistent environments. It automates library inclusion and version control, aiding in project setup and maintenance. Developers use Composer to manage dependencies efficiently, utilizing its extensive PHP package repository. Its version control integration enhances team collaboration, maintaining uniformity in components across contributors.

Here are some key highlights of Composer:

  • Simplifies Dependency Management: Composer allows developers to easily declare the libraries their project depends on, automatically installing and updating them as needed.
  • Automates Library Inclusion and Version Management: With Composer, you can define the version of each library your project requires, ensuring compatibility and stability across all project elements.
  • Facilitates Efficient Project Setup and Maintenance: Developers can set up new projects quickly and maintain existing ones with ease, as Composer handles all underlying package installations and updates.
  • Ensures Consistency Across Development Environments: By locking down the exact versions of packages, Composer ensures that all developers on a team, as well as deployment environments, use the same versions, avoiding “it works on my machine” problems.
  • Supports a Vast Repository of PHP Packages: Access thousands of packages through Packagist, the default package repository for Composer, which serves as a comprehensive source for PHP libraries and tools.
  • Handles Package Versions to Minimize Compatibility Issues: Composer’s version management system avoids conflicts between package versions, helping maintain the overall integrity and compatibility of your project.
  • Integrates with Version Control Systems: Composer works seamlessly with version control systems like Git, allowing team members to easily synchronize project dependencies with their codebase.

Now, let’s proceed with the installation steps to get Composer up and running on your Ubuntu system.

Composer Pre-Installation

Update Ubuntu Before Composer Installation

Start off by refreshing the local package database of your system. 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 unzip will be used to extract downloaded packages.

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

sudo apt install php-cli unzip curl

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

Download and Install Composer on Ubuntu 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 an SHA-384 hash of the latest installer on their 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 are going to 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 in your projects effectively.

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, along with its own 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 start using them in your project. The easiest way to make use of 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 of the 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. Simply navigate to your project directory and execute the update command. This command checks for newer versions of the libraries you have specified in your composer.json file and updates them if necessary.

cd /path/to/your/project
composer update

Closing Thoughts

Alright, we’ve just walked through how to install Composer on your Ubuntu machine, covering versions 24.04, 22.04, or 20.04 LTS. Pretty straightforward, right? Just remember, keeping Composer updated ensures you get all the latest features and security fixes. And hey, don’t forget to regularly check your project dependencies to keep everything running smoothly. That’s about it – you’re all set to streamline your PHP projects with Composer.

Leave a Comment