Composer is the dependency manager for PHP. It allows you to declare project libraries, resolve version conflicts, and install them automatically. This guide installs Composer globally on Ubuntu 24.04 LTS.
Tested and valid on:
- Ubuntu 24.04 LTS
Prerequisites
- Ubuntu 24.04 LTS server
- PHP 8.3 installed
- A user with sudo privileges
Step 1 – Install Required Dependencies
Install required PHP extensions:
sudo apt install php8.3-cli php8.3-zip php8.3-mbstring unzip curl -y
Step 2 – Download and Verify the Composer Installer
Download the installer and verify its hash:
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
Get the expected hash from getcomposer.org and verify:
HASH=$(curl -sS https://composer.github.io/installer.sig)
php -r "if (hash_file('sha384', 'composer-setup.php') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
Step 3 – Install Composer Globally
Run the installer and move to the system path:
sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
php -r "unlink('composer-setup.php');"
Step 4 – Verify the Installation
Check the Composer version:
composer --version
Step 5 – Create a New Project
Example: create a new Laravel 11 project:
composer create-project laravel/laravel myapp "^11.0"
Step 6 – Update Composer
Keep Composer itself up to date:
sudo composer self-update
Step 7 – Use Composer in a Project
Common Composer commands:
composer require vendor/package # add a package
composer update # update all packages
composer install # install from composer.lock
composer dump-autoload # regenerate autoload files
Conclusion
Composer is now installed globally on Ubuntu 24.04 LTS and ready to manage PHP project dependencies. It is an essential tool for modern PHP development with Laravel, Symfony, and other frameworks.