How to Install PHP Composer on Ubuntu 21.04

Composer is basically a dependency manager for the programming language, PHP. It functions as some sort of project manager that helps the programmer manage dependencies that will be used on a project to project basis.

Install Dependencies

apt update

apt install wget php-cli php-zip unzip

Output:

root@server:~# apt install wget php-cli php-zip unzip
Reading package lists... Done
Building dependency tree
Reading state information... Done
wget is already the newest version (1.20.3-1ubuntu1).
wget set to manually installed.
The following additional packages will be installed:
  libzip5 php7.4-zip
Suggested packages:
  zip
The following NEW packages will be installed:
  libzip5 php-cli php-zip php7.4-zip unzip

Download and Install Composer

Make sure you’re in your home directory and download the Composer installer using wget.

cd ~

wget -O composer-setup.php https://getcomposer.org/installer

Output:

root@server:~# wget -O composer-setup.php https://getcomposer.org/installer
--2021-04-29 19:22:56--  https://getcomposer.org/installer
Resolving getcomposer.org (getcomposer.org)... 142.44.245.229, 2607:5300:201:2100::4:d105
Connecting to getcomposer.org (getcomposer.org)|142.44.245.229|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 58460 (57K) [application/octet-stream]
Saving to: ‘composer-setup.php’

composer-setup.php  100%[===================>]  57.09K  --.-KB/s    in 0.1s

2021-04-29 19:22:56 (489 KB/s) - ‘composer-setup.php’ saved [58460/58460]

Now, download and install Composer, under /usr/local/bin by running the following command.

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

Output

root@server:~#  php composer-setup.php --install-dir=/usr/local/bin --filename=composer
All settings correct for using Composer
Downloading...

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

To test your installation, run the following command.

composer

Output:

root@server:~# composer
Do not run Composer as root/super user! See https://getcomposer.org/root for det               ails
Continue as root/super user [yes]? yes
   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/
Composer version 2.0.13 2021-04-27 13:11:08

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 workin               g directory.
      --no-cache                 Prevent use of the cache
  -v|vv|vvv, --verbose           Increase the verbosity of messages: 1 for norma               

This verifies that Composer was successfully installed on your system.