In this tutorial, we will learn How to Install PHP on Ubuntu 20.04?
PHP 7.4 is the latest stable release available. Say thanks to Ondřej Surý for maintaining the PPA of most of the popular PHP versions on the launchpad. If you want to install a specific or multiple version of PHP, then this article can be helpful for you. This article will help you to install PHP 7.4, 7.3, 7.2, and 5.6 on Ubuntu 20.04 (Focal Fossa) via PPA.
You can install multiple PHP versions on the Ubuntu system and then switch PHP versions as per your requirements. You can also configure Apache to run PHP web applications with different-2 PHP versions.
Step 1 – Add PHP PPA
First of all, you need to configure the repository on your system. Run the following command to add the Ondrej PHP repository to your Ubuntu system.
sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php
The above command will also update the packages information list. But if you already have the repository added, then update the packages information before installing or updating the package:
sudo apt update
Once the update is complete, let’s begin the PHP installation.
Step 2 – Install Required PHP Version
You can install any required PHP version on your Ubuntu system. Use one of the following options to install PHP.
Install PHP 7.4
PHP 7.4 is the latest stable version available for installation. To install PHP 7.4 on your Ubuntu system run the command:
sudo apt install -y php7.4
Install PHP 7.3
PHP 7.3 is a current active release of PHP. The PHP team is still providing updates on this version. To install PHP 7.3 on Ubuntu, type:
sudo apt install -y php7.3
Install PHP 7.2
For PHP 7.2, the development is only providing security updates and fixed. Further development on this version is stopped. To install this version run commands:
sudo apt install -y php7.2Info: This repository also contains PHP 7.1 and PHP 7.0 versions. You can install them by changing the php version number in the above commands.
Install PHP 5.6
PHP 5.6 is an outdated version. Its development is stopped and no more security updates are available. But if your application still relies on this version use the below command to install it.
sudo apt install -y php5.6
But it is recommended to upgrade your applications and make them compatible with the latest PHP versions.
Step 3 – Check PHP Version
To view the current active PHP version run the following command:
PHP 7.4.8 (cli) (built: Jul 13 2020 16:46:22) ( NTS ) Copyright (c) The PHP Group Zend Engine v3.4.0, Copyright (c) Zend Technologies with Zend OPcache v7.4.8, Copyright (c), by Zend Technologies
Step 4 – Install PHP Modules
You may also need to install modules based on your application requirements. Use the following command to search available PHP 7 modules in the package repository.
sudo apt-cache search php7*
You can install the required PHP modules on your system. Just change the PHP version with the packages names as per your requirements:
sudo apt install php7.4-mysql php7.4-curl php7.4-json php7.4-cgi php7.4-xsl
Step 5 – Switch Between PHP Versions
You can use the update-alternatives command to set the default PHP version.
sudo update-alternatives --config php
Select PHP version number as per your requirement. This will change the PHP CLI version only.
There are 4 choices for the alternative php (providing /usr/bin/php). Selection Path Priority Status ------------------------------------------------------------ * 0 /usr/bin/php7.4 74 auto mode 1 /usr/bin/php5.6 56 manual mode 2 /usr/bin/php7.2 72 manual mode 3 /usr/bin/php7.3 73 manual mode 4 /usr/bin/php7.4 74 manual mode Press to keep the current choice[*], or type selection number: 4