Tutorial

How To Install Composer on Ubuntu 20.04 [Quickstart]

Updated on March 18, 2022

Developer Advocate

English
How To Install Composer on Ubuntu 20.04 [Quickstart]

Introduction

In this quickstart guide, we’ll install Composer on an Ubuntu 20.04 server.

For a more detailed version of this tutorial, with more explanations of each step, please refer to How To Install and Use Composer on Ubuntu 20.04.

Prerequisites

To follow this guide, you’ll need access to an Ubuntu 20.04 server as a sudo user.

Step 1 — Install Dependencies

Start by updating your package manager cache and installing the required dependencies, including php-cli:

  1. sudo apt update
  2. sudo apt install php-cli unzip

Step 2 — Download and Install Composer

Make sure you’re in your home directory, then retrieve the Composer installer using curl:

  1. cd ~
  2. curl -sS https://getcomposer.org/installer -o /tmp/composer-setup.php

Next, we’ll verify that the downloaded installer matches the SHA-384 hash for the latest installer found on the Composer Public Keys / Signatures page.

Using curl, fetch the latest signature and store it in a shell variable:

  1. HASH=`curl -sS https://composer.github.io/installer.sig`

Now execute the following PHP code to verify that the installation script is safe to run:

  1. 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;"

You’ll see the following output:

Output
Installer verified

Note: If the output says Installer corrupt, you’ll need to repeat the download and verification process until you have a verified installer.

The following command will download and install Composer as a system-wide command named composer, under /usr/local/bin:

  1. sudo php /tmp/composer-setup.php --install-dir=/usr/local/bin --filename=composer

You’ll see output similar to this:

Output
All settings correct for using Composer Downloading... Composer (version 2.2.9) successfully installed to: /usr/local/bin/composer Use it: php /usr/local/bin/composer

To test your installation, run:

  1. composer
Output
______ / ____/___ ____ ___ ____ ____ ________ _____ / / / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/ / /___/ /_/ / / / / / / /_/ / /_/ (__ ) __/ / \____/\____/_/ /_/ /_/ .___/\____/____/\___/_/ /_/ Composer version 2.2.9 2022-03-15 22:13:37 Usage: command [options] [arguments] Options: -h, --help Display this help message -q, --quiet Do not output any message -V, --version Display this application version --ansi Force ANSI output --no-ansi Disable ANSI output -n, --no-interaction Do not ask any interactive question --profile Display timing and memory usage information --no-plugins Whether to disable plugins. -d, --working-dir=WORKING-DIR If specified, use the given directory as working directory. --no-cache Prevent use of the cache -v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug ...

This verifies that Composer was successfully installed on your system and is available system-wide.

Here are links to more detailed guides related to this tutorial:

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar

Developer Advocate

Dev/Ops passionate about open source, PHP, and Linux.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
2 Comments


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Not helpful - does not install composer as indicated

Thanks this was really useful and it works, in Composer this is a bit confusing.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.