How to Install PHP Composer on Ubuntu 22.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@vps:~# apt install wget php-cli php-zip unzip
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
wget is already the newest version (1.21.2-2ubuntu1).
wget set to manually installed.
The following additional packages will be installed:
  bzip2 libzip4 mailcap mime-support php-common php8.1-cli php8.1-common
  php8.1-opcache php8.1-readline php8.1-zip
Suggested packages:
  bzip2-doc php-pear zip

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@vps:~# cd ~
root@vps:~# wget -O composer-setup.php https://getcomposer.org/installer
--2022-05-26 10:16:07--  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: 58140 (57K) [application/octet-stream]
Saving to: ‘composer-setup.php’

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

2022-05-26 10:16:07 (732 KB/s) - ‘composer-setup.php’ saved [58140/58140]

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@vps:~#  php composer-setup.php --install-dir=/usr/local/bin --filename=composer
All settings correct for using Composer
Downloading...

Composer (version 2.3.5) 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@vps:~# composer
Do not run Composer as root/super user! See https://getcomposer.org/root for details
Continue as root/super user [yes]? y
     ______
    / ____/___  ____ ___  ____  ____  ________  _____
   / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
  / /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
  \____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
              /_/
    Composer version 2.3.5 2022-04-13 16:43:00

 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            

This verifies that Composer was successfully installed on your system.