This guide walks you through installing Composer, the PHP dependency manager, on Ubuntu 20.04 LTS, and shows you how to use it to manage project libraries. You’ll start with a system update, install the required PHP extensions, then download and verify Composer, and finally run your first commands to install and update packages.
What You’ll Need
- Ubuntu 20.04 LTS server or desktop with sudo access
- PHP 7.2 or newer installed (check with
php -v) - Command-line terminal access
- An internet connection to download Composer and packages
Step-by-Step
- Update your system
Open a terminal and runsudo apt updateto refresh the package index, thensudo apt upgrade -yto upgrade existing packages. This ensures you have the latest security patches and dependency versions. - Install required PHP extensions
Composer needs several PHP extensions. Install them with:sudo apt install -y php-cli php-zip php-mbstring php-curl php-xml php-gd
These cover common needs; if your project uses others (likephp-sqlite3), install them as well. - Verify PHP installation
Runphp -vto confirm PHP is working and note the version. Composer works with PHP 7.2.5+ and PHP 8.x. - Download Composer installer
Use the official installer script:cd ~curl -sS https://getcomposer.org/installer -o composer-setup.php
This downloads a small PHP script that will install Composer. - Verify the installer’s integrity
Compare the SHA-384 hash of the downloaded file with the official one. Fetch the expected hash:HASH=$(curl -sS https://composer.github.io/installer.sig)
Then check:php -r "if (hash_file('sha384', 'composer-setup.php') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); }"
You should see “Installer verified”. If not, delete the file and try again. - Run the installer
Execute the installer with:php composer-setup.php
By default, it creates a file namedcomposer.pharin the current directory. - Make Composer globally accessible
Move the phar file to a directory in your PATH and rename it tocomposer:sudo mv composer.phar /usr/local/bin/composer
Then make it executable:sudo chmod +x /usr/local/bin/composer - Verify the installation
Runcomposer --versionto see the installed version. You should see output likeComposer version 2.7.1. - Clean up the installer
Remove the setup script:rm composer-setup.php. - Use Composer in a project
Navigate to your project directory (e.g.,cd ~/myapp). To install dependencies from an existingcomposer.json, runcomposer install. To start a new project, runcomposer initand follow the prompts to create acomposer.jsonfile. - Install a package
For example, to install the popularmonolog/monologlogging library, run:composer require monolog/monolog
This adds the package tocomposer.jsonand installs it in thevendor/directory. - Update packages
To update all dependencies to the latest versions allowed by yourcomposer.json, runcomposer update. To update a single package, usecomposer update monolog/monolog. - Autoload classes
In your PHP code, include the autoloader at the top:require 'vendor/autoload.php';This lets you use installed classes without manual requires.
Common Mistakes
- Missing PHP extensions – Composer will fail with cryptic errors if extensions like
zipormbstringare absent. Install all recommended extensions. - Running
composer installwithout acomposer.json– You need acomposer.jsonfile first; usecomposer initto create one. - Using
sudo composerunnecessarily – Running Composer as root can cause permission issues in your project. Use a regular user and only use sudo for the global installation. - Forgetting to run
composer dump-autoloadafter changing classes – If you add new classes manually, run this to regenerate the autoloader. - Not checking the PHP version – Some packages require PHP 8.x; if you have 7.4, you’ll see compatibility errors. Use
php -vto check.
Tips
- Use
composer require --devfor development-only packages (e.g., PHPUnit) so they aren’t installed in production. - Keep
composer.lockin version control to ensure everyone uses the same exact versions. - Run
composer diagnoseto check for common configuration problems. - Use
composer show --treeto see the dependency tree and understand what’s installed. - If you’re on shared hosting, you can install Composer locally in your project directory instead of globally.
Troubleshooting
- “Composer is not recognized as a command” – Ensure the
/usr/local/bindirectory is in your PATH. Runecho $PATHand check. If not, add it to your.bashrc. - “The zip extension and unzip command are missing” – Install them with
sudo apt install php-zip unzip. - “Failed to open stream: Permission denied” – This often happens when Composer was run as root and created files owned by root. Change ownership with
sudo chown -R $USER:$USER vendor composer.lock. - “SSL certificate problem” – If you get SSL errors, update CA certificates with
sudo apt install ca-certificatesand ensure your PHP curl settings are correct. - “Could not find package” – Check the package name spelling and that it exists on Packagist. Also make sure your
composer.jsonhas the correct repository settings. - PHP memory limit exhausted – Increase memory limit temporarily with
php -d memory_limit=-1 composer installor editphp.inito raisememory_limit.
Frequently Asked Questions
Do I need to install Composer globally?
No. You can also install it per-project by downloading composer.phar into your project and running php composer.phar. Global installation is convenient for frequent use.
How do I update Composer itself?
Run composer self-update to update to the latest version. This works for global installations.
What is the difference between composer install and composer update?
composer install reads the composer.lock file and installs the exact versions listed. composer update resolves dependencies again and updates the lock file to the latest allowed versions.
How do I uninstall Composer?
Simply remove the binary: sudo rm /usr/local/bin/composer. There are no other system files to clean up.
Can I use Composer with PHP 8.0 on Ubuntu 20.04?
Yes, but you may need to add the Ondřej Surý PPA to get PHP 8.0, as the default Ubuntu 20.04 repos only include PHP 7.4. Composer itself works with both.
Why do I get a warning about running as root?
Composer warns when run as root/superuser because it can create files that are difficult for other users to manage. It’s best to use a regular user account for development.
Related
General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.
More in this bed
- Access Your Computer From Anywhere: Step-by-Step
- How to Get More Disk Space on Mac: 12 Fixes
- Wii Browser Games With a USB or Bluetooth Controller
- Delete Focus on iPhone: Quick Steps
- How to Reinstall Windows 10 Fast & Easily
- MacBook vs MacBook Air: Key Differences