sodcalculator

10 Tech How-To

Install & Use PHP Composer on Ubuntu 20.04

Guide 10-36566 · Updated · Step by step

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

  1. Update your system
    Open a terminal and run sudo apt update to refresh the package index, then sudo apt upgrade -y to upgrade existing packages. This ensures you have the latest security patches and dependency versions.
  2. 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 (like php-sqlite3), install them as well.
  3. Verify PHP installation
    Run php -v to confirm PHP is working and note the version. Composer works with PHP 7.2.5+ and PHP 8.x.
  4. 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.
  5. 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.
  6. Run the installer
    Execute the installer with:
    php composer-setup.php
    By default, it creates a file named composer.phar in the current directory.
  7. Make Composer globally accessible
    Move the phar file to a directory in your PATH and rename it to composer:
    sudo mv composer.phar /usr/local/bin/composer
    Then make it executable:
    sudo chmod +x /usr/local/bin/composer
  8. Verify the installation
    Run composer --version to see the installed version. You should see output like Composer version 2.7.1.
  9. Clean up the installer
    Remove the setup script: rm composer-setup.php.
  10. Use Composer in a project
    Navigate to your project directory (e.g., cd ~/myapp). To install dependencies from an existing composer.json, run composer install. To start a new project, run composer init and follow the prompts to create a composer.json file.
  11. Install a package
    For example, to install the popular monolog/monolog logging library, run:
    composer require monolog/monolog
    This adds the package to composer.json and installs it in the vendor/ directory.
  12. Update packages
    To update all dependencies to the latest versions allowed by your composer.json, run composer update. To update a single package, use composer update monolog/monolog.
  13. 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 zip or mbstring are absent. Install all recommended extensions.
  • Running composer install without a composer.json – You need a composer.json file first; use composer init to create one.
  • Using sudo composer unnecessarily – 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-autoload after 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 -v to check.

Tips

  • Use composer require --dev for development-only packages (e.g., PHPUnit) so they aren’t installed in production.
  • Keep composer.lock in version control to ensure everyone uses the same exact versions.
  • Run composer diagnose to check for common configuration problems.
  • Use composer show --tree to 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/bin directory is in your PATH. Run echo $PATH and 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-certificates and 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.json has the correct repository settings.
  • PHP memory limit exhausted – Increase memory limit temporarily with php -d memory_limit=-1 composer install or edit php.ini to raise memory_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

The whole Tech How-To bed →