How to Install Composer on Ubuntu 22.04

Shaon Majumder
4 min readMar 13, 2024

--

Composer is a powerful dependency management tool for PHP projects, facilitating the installation and management of project dependencies. In this guide, we’ll walk you through the process of installing Composer on an Ubuntu 22.04 system and getting started with its basic usage.

Prerequisites: Before we begin, ensure you have access to an Ubuntu 22.04 server with a non-root sudo user account and the firewall enabled.

Step 1 — Installing PHP and Additional Dependencies: First, update the package manager cache:

sudo apt update

Next, install the required packages:

sudo apt install php-cli unzip

Step 2 — Downloading and Installing Composer: Navigate to your home directory and download the Composer installer:

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

Retrieve the SHA-384 hash for verification:

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

Echo the obtained hash for reference:

echo $HASH

Output:

dac665fdc30fdd8ec78b38b9800061b4150413ff2e3b6f88543c636f7cd84f6db9189d43a81e5503cda447da73c7e5b6

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

php -r "if (hash_file('SHA384', '/tmp/composer-setup.php') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('/tmp/composer-setup.php'); echo 'Download the installer again and double-check the hash. Then, repeat the verification process.'; } echo PHP_EOL;"

Output:

Installer verified

You should see one of the following outputs:

  • If the installer is verified: “Installer verified”
  • If the installer is corrupt: “Installer corrupt” and a message prompting you to download the installer again and double-check the hash. Then, repeat the verification process.

Once verified, proceed with the installation:

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

Output:

All settings correct for using Composer
Downloading...

Composer (version 2.7.2) successfully installed to: /usr/local/bin/composer
Use it: php /usr/local/bin/composer

This command will download and install Composer globally as a system-wide command named composer, under /usr/local/bin.

Step 3 — Testing Composer Installation: To test the installation, run:

composer

Output:


/ ____/___ ____ ___ ____ ____ ________ _____
/ / / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__ ) __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
/_/
Composer version 2.7.2 2024-03-11 17:12:18

Usage:
command [options] [arguments]

Options:
-h, --help Display help for the given command. When no command is given display help for the list command
-q, --quiet Do not output any message
-V, --version Display this application version
--ansi|--no-ansi Force (or disable --no-ansi) ANSI output
-n, --no-interaction Do not ask any interactive question
--profile Display timing and memory usage information
--no-plugins Whether to disable plugins.
--no-scripts Skips the execution of all scripts defined in composer.json file.
-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

Available commands:
about Shows a short information about Composer
archive Creates an archive of this composer package
audit Checks for security vulnerability advisories for installed packages
browse [home] Opens the package's repository URL or homepage in your browser
bump Increases the lower limit of your composer.json requirements to the currently installed versions
check-platform-reqs Check that platform requirements are satisfied
clear-cache [clearcache|cc] Clears composer's internal package cache
completion Dump the shell completion script
config Sets config options
create-project Creates new project from a package into given directory
depends [why] Shows which packages cause the given package to be installed
diagnose Diagnoses the system to identify common errors
dump-autoload [dumpautoload] Dumps the autoloader
exec Executes a vendored binary/script
fund Discover how to help fund the maintenance of your dependencies
global Allows running commands in the global composer dir ($COMPOSER_HOME)
help Display help for a command
init Creates a basic composer.json file in current directory
install [i] Installs the project dependencies from the composer.lock file if present, or falls back on the composer.json
licenses Shows information about licenses of dependencies
list List commands
outdated Shows a list of installed packages that have updates available, including their latest version
prohibits [why-not] Shows which packages prevent the given package from being installed
reinstall Uninstalls and reinstalls the given package names
remove [rm] Removes a package from the require or require-dev
require [r] Adds required packages to your composer.json and installs them
run-script [run] Runs the scripts defined in composer.json
search Searches for packages
self-update [selfupdate] Updates composer.phar to the latest version
show [info] Shows information about packages
status Shows a list of locally modified packages
suggests Shows package suggestions
update [u|upgrade] Updates your dependencies to the latest version according to composer.json, and updates the composer.lock file
validate Validates a composer.json and composer.lock

You should see Composer version information displayed, confirming that Composer is installed correctly.

Composer is an invaluable tool for managing dependencies in PHP projects. By following this tutorial, you’ve learned how to install Composer on Ubuntu 22.04 and verify its installation. Start using Composer to streamline dependency management in your PHP projects and enhance your development workflow.

--

--

Shaon Majumder
Shaon Majumder

Written by Shaon Majumder

Software Engineer | Author | Data Scientist

No responses yet