Install PHP Composer and Use On Ubuntu 20.04 /19.10

We can install and use PHP composer on ubuntu 20.04. you can also check the PHP composer version using the command. When you have a PHP based application and require a composer then install it using these documents. Let’s see, how to install composer on ubuntu.

Install PHP Composer and Use On Ubuntu 20.04 /19.10

We will need to install the PHP on ubuntu 20.04. you can update and upgrade the system using the following commands.  

sudo apt update 
sudo apt upgrade -y
sudo apt install php -y

Check Out: How To Secure and Hardening Apache Web Server On Ubuntu 20.04

Once the PHP is installed, check the version with php -v command. 

ubuntu@ubuntu:~$ php -v
PHP 7.4.3 (cli) (built: May 26 2020 12:24:22) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
with Zend OPcache v7.4.3, Copyright (c), by Zend Technologies

Suppose, if you need to install other PHP versions on the ubuntu 20.04, we need to add third-party repositories using the following command because the default PHP version will be 5.4 in ubuntu 18.04/16.04. you can get PHP 7.2 version in the ubuntu 20.04. 

sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt update
sudo apt upgrade -y

Configure PHP Composer:

We already have PHP CLI installed on the ubuntu machine. So, We need to download the composer installer with the below command. 

php -r "copy('', 'composer-setup.php');"

You can verify the data integrity of the script by comparing the SHA-384 hash with the latest installer. we will use the wget command to download the latest expected signature of the installer from the Github. 

HASH="$(wget -q -O -"

Check Out: How To Install WordPress 5.5 On Ubuntu 20.04 Server

Now, run the below command to verify the installation script is corrupted or not. if you see “Installer verified“, means it’s not corrupted. 

ubuntu@ubuntu:~$ php -r "if (hash_file('SHA384', 'composer-setup.php') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
Installer verified

Use the below command to install the composer in the /usr/local/bin directory. 

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

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

Run the composer command to check the installation. If you see the output like below, means it’s installed. 

Check Out: How To Install R and Rstudio On Ubuntu 20.04/19.10

When you need a PHP based application that requires the Composer. Simply follow these documents. The Composer can upgrade itself by running the below command.

sudo composer self-update

That’s it.

Share on:

I'm the founder of Curious Viral. I hope this blog will provide you complete information about Linux Technology & I would like to share my technical knowledge with you which I have learned during this period.

Other Posts You May Like...

Leave a comment