Best Steps to Install and Use PHP Composer on Ubuntu 20.04


In this article, I will take you through the best steps to install and use PHP composer on Ubuntu 20.04. Composer is a tool for dependency management in PHP. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you. It is one of the famous PHP dependency manager tool currently available on Linux based systems.

Composer is not a package manager in the same sense as Yum or Apt are. Yes, it deals with "packages" or libraries, but it manages them on a per-project basis, installing them in a directory (e.g. vendor) inside your project. By default, it does not install anything globally. Thus, it is a dependency manager. It does however support a "global" project for convenience via the global command. More on Composer docs.

Best Steps to Install and Use PHP Composer on Ubuntu 20.04

Steps to Install and Use PHP Composer on Ubuntu 20.04

Also Read: Best Steps to Install Apache Maven on Ubuntu 20.04

Step 1: Prerequisites

a) You should have a running Ubuntu 20.04 Server.

b) You should have sudo access to run any privileged commands.

c) You should have apt and wget utility installed in your Server.

Step 2: Update Your Server

It is always recommended to first update your Server with the latest releases available on Ubuntu repo using sudo apt update command as shown below. This will download and install all the available updates from all the enabled repositories.

cyberithub@localhost:~$ sudo apt update
Hit:1 focal InRelease
Get:2 focal-updates InRelease [114 kB]
Get:3 focal-backports InRelease [101 kB]
Hit:4 stable InRelease
Get:5 focal-security InRelease [114 kB]
Get:6 focal InRelease [17.5 kB]
Get:7 focal-updates/main i386 Packages [500 kB]
Get:8 focal-updates/main amd64 Packages [1,081 kB]
Get:9 focal-updates/main amd64 DEP-11 Metadata [282 kB]
Get:10 focal-updates/universe i386 Packages [617 kB]
Get:11 focal-updates/universe amd64 Packages [828 kB]
Get:12 focal-updates/universe amd64 DEP-11 Metadata [329 kB]
Get:13 focal-updates/multiverse amd64 DEP-11 Metadata [2,468 B]
Get:14 focal-backports/universe amd64 DEP-11 Metadata [1,780 B]


Step 3: Install PHP

Composer has a dependency on PHP, so we need to first install PHP packages by using below apt install command. At the time of writing, by default Ubuntu 20.04 supports PHP 7.4 so we will go ahead and install this version.

cyberithub@localhost:~$ sudo apt install php7.4-common php7.4-cli php7.4-mysql
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
libaopalliance-java libapache-pom-java libatinject-jsr330-api-java libatomic1 libcdi-api-java libcommons-cli-java libcommons-io-java
libcommons-lang3-java libcommons-parent-java libgeronimo-annotation-1.3-spec-java libgeronimo-interceptor-3.0-spec-java libguava-java libguice-java
libhawtjni-runtime-java libjansi-java libjansi-native-java libjsr305-java libmaven-parent-java libmaven-resolver-java libmaven-shared-utils-java
libmaven3-core-java libplexus-cipher-java libplexus-classworlds-java libplexus-component-annotations-java libplexus-interpolation-java
libplexus-sec-dispatcher-java libplexus-utils2-java libsisu-inject-java libsisu-plexus-java libslf4j-java libwagon-file-java libwagon-http-shaded-java
libwagon-provider-api-java linux-headers-5.8.0-53-generic linux-hwe-5.8-headers-5.8.0-53 linux-image-5.8.0-53-generic linux-modules-5.8.0-53-generic
Use 'apt autoremove' to remove them.
The following additional packages will be installed:
php-common php7.4-json php7.4-opcache php7.4-readline
Suggested packages:
The following NEW packages will be installed:
php-common php7.4-cli php7.4-common php7.4-json php7.4-mysql php7.4-opcache php7.4-readline
0 upgraded, 7 newly installed, 0 to remove and 157 not upgraded.
Need to get 2,764 kB of archives.


Step 4: Download Composer

You can use below wget command to get the composer installer as composer-setup.php file in your current directory.

cyberithub@localhost:~$ sudo wget -O composer-setup.php
--2021-07-01 09:36:51--
Resolving ( 2001:41d0:302:1100::8:104f,
Connecting to (|2001:41d0:302:1100::8:104f|:443... failed: Connection timed out.
Connecting to (||: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 369KB/s in 0.2s

2021-07-01 09:39:02 (369 KB/s) - ‘composer-setup.php’ saved [58460/58460]


Step 5: Install Composer

Then run the downloaded installer using below php command and install it under /usr/local/bin directory path as composer file as shown below.

cyberithub@localhost:~$ sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
All settings correct for using Composer

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


Step 6: Test Installation

If everything goes well, then you can verify the composer installation by running composer -V command. This will show you composer installed version.

cyberithub@localhost:~$ composer -V
Composer version 2.1.3 2021-06-09 16:31:20

Leave a Comment