Composer is an indispensable dependency manager for PHP, simplifying the process of declaring, installing, and updating libraries your PHP projects rely on. For developers working on Ubuntu 24.04, having Composer set up correctly is a fundamental step towards efficient project management and development. This Tutorial will guide you through the process of installing Composer globally on your Ubuntu 24.04 system.
Before we begin, ensure your Ubuntu 24.04 system is up-to-date and you have PHP installed, as Composer requires PHP to run. If you don’t have PHP, you’ll need to install it first.
1. Update System Packages
It’s always a good practice to update your package list and upgrade any existing packages:
sudo apt update
sudo apt upgrade -y
2. Install PHP CLI and Required Extensions
Composer needs several PHP extensions to function correctly. Install them using the following command:
sudo apt install php-cli php-unzip php-json curl -y
3. Download Composer Installer Script
Composer provides an installer script that you can download and run directly. Use `curl` to download it:
curl -sS https://getcomposer.org/installer -o composer-setup.php
4. Verify the Installer (Optional but Recommended)
To ensure the installer script is not corrupted or tampered with, you can verify its SHA-384 hash. Visit the Composer download page to get the latest installer hash. For example:
HASH="$(wget -q -O - https://composer.org/installer | sha384sum | awk '{print $1}')"
php -r "if (hash_file('SHA384', 'composer-setup.php') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
If the installer is corrupt, the script will remove it.
5. Install Composer Globally
Now, run the installer script using PHP. We’ll move the `composer.phar` file to `/usr/local/bin` to make it globally accessible:
sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
This command installs Composer as a system-wide command named `composer`.
6. Verify Composer Installation
After installation, you can verify that Composer is correctly installed and accessible by checking its version:
composer --version
You should see output similar to `Composer version 2.x.x …`.
7. Clean Up (Optional)
You can remove the installer script after a successful installation:
rm composer-setup.php
Using Composer
With Composer installed globally, you can now use it in any of your PHP projects. Navigate to your project directory and run:
composer install
to install dependencies defined in your `composer.json` file, or:
composer require vendor/package
to add a new dependency to your project.
Congratulations! You have successfully installed Composer on your Ubuntu 24.04 system. You are now ready to manage PHP dependencies effortlessly and streamline your development workflow.
