PHP is a recursive acronym for Hypertext Processor. It is an open-source, general-purpose scripting language that is widely used in web development due to its ability to be embedded into HTML. A scripting language is used to write pre-written programs that are later used for the automation of tasks. PHP scripts are commonly used on Linux, Unix, Windows, Mac OS, and other operating systems. When you use PHP in web development, you are free to choose your web server and the underlying operating system.

This article describes a step by step procedure to install PHP versions 5.6 and 7.2 on your Ubuntu. After installing the two versions, we will also explain how you can disable one version and enable another version to be treated as the default version by the system.

We have run the commands and procedures mentioned in this article on a Ubuntu 18.06 LTS system.

In this article, we are using the Ubuntu command line, the Terminal, in order to install and configure PHP. You can open the Terminal application either through the system Dash or the Ctrl+Alt+t shortcut.

The official PHP website, php.net, maintains a list of all the PHP releases till date on the following link:

http://php.net/releases/

From this list, you can choose whichever version you wish to install on your system. The list includes downloadable tar.gz packages but in this article, we will describe installing PHP through the Ondrej PPA repository.

Install PHP version 5.6

In order to install PHP version 5.6, first, open your Ubuntu Terminal and enter the following command in order to add the Ondrej PHP repository to your Ubuntu.

$ sudo add-apt-repository ppa:ondrej/php

This repository contains all the released versions of PHP till date.

Add Ondrej Ubuntu Repository

Once the Ondrej repository is added, you need to update your system’s repository indexwith that on the Internet. This way you can install the latest available version of a software on your system. Enter the following command in order to do so:

$ sudo apt-get update

Update the package list

Now is the time to install PHP 5.6 to your system. Enter the following command as sudo as only an authorized person can install/uninstall and configure software on Ubuntu:

$ sudo apt-get install -y php5.6

Install PHP 5.6

The software will then be installed on your system.

In order to check the version number of your installed PHP, run the following command:

$ php -v

or,

$ php --version

The command will also verify that PHP is now indeed installed on your system.

Check the PHP version

The output from my system shows that PHP 5.6.38 is installed on my system.

Install PHP version 7.2

In order to install PHP version 7.2, first, open your Ubuntu Terminal and enter the following command in order to add the Ondrej PHP repository to your Ubuntu.

$ sudo add-apt-repository ppa:ondrej/php

This repository contains all the released versions of PHP till date.

Once the Ondrej repository is added, you need to update your system’s repository with that on the internet. This way you can install the latest available version of a software on your system. Enter the following command in order to do so:

$ sudo apt-get update

Now is the time to install PHP 7.2 to your system. Enter the following command as sudo as only an authorized person can install/uninstall and configure software on Ubuntu:

$ sudo apt-get install -y php7.2

The software will then be installed on your system.

In order to check the version number of your installed PHP, run the following command:

$ php -v

or,

$ php --version

The command will also verify that PHP is now indeed installed on your system.

Check PHP version

The output from my system shows that PHP 7.2.13 is installed on my system.

Switching between installed PHP versions

If you have two or more versions of PHP installed on your system, you can configure your system to use one of them as the default PHP version. For this, it is first important to learn which version is currently enabled as default on your Ubuntu system.

Check which version is enabled

We will describe two ways to check which PHP version is enabled on your system; one is through Apache2 and the other is through the CLI.

Through Apache2

Change the current directory to /etc/apache2 as follows:

$ cd /etc/apache2

In the apache2 directory, run the following command to list all the available modes of PHP on your system and know which one of them is currently enabled:

$ ls -l mods-*/*php*

PHP version enabled in Apache

In the output, you can see that the currently enabled version of PHP is highlighted. In our case, it is PHP 5.6.

Through CLI

It is also very simple to check the currently enabled version of PHP through the CLI. Run the following command which is used to update the default alternative to a software on Ubuntu and thus lists all the available alternatives.

$ sudo update-alternatives --config php

Change PHP CLI version

In the output of the above command, the currently enabled version of PHP is indicated by a * symbol. You can see that in our case, it is PHP 5.6.

Switch from PHP 5.6 to PHP 7.2

We will describe two ways to switch from PHP 5.6 to PHP 7.2; one is through Apache2 and the other is through the CLI.

Through Apache2

First, disable the currently enabled version of PHP through the following command:

$ sudo a2dismod php5.6

Disable PHP 5.6

And then, enable the other version of PHP through the following command:

$ sudo a2enmod php7.2

Enable PHP 7.2

Now when you restart the apache2 service through the following command, the PHP 7.2 will be enabled on your system.

$ sudo service apache2 restart

Restart Apache
Through CLI

Use the following command to update your system to now use PHP 7.2 as the default PHP version.

$ sudo update-alternatives --set php /usr/bin/php7.2

Change PHP CLI version

Alternatively, you can use the following command to achieve the same purpose:

$ sudo update-alternatives --config php

Alternative way to change CLI PHP on Ubuntu

The command lists all the available versions of PHP installed on your system. Enter the selection number of the version you want to enable on your system and hit enter. For example, if I enter 2, PHP 5.6 will be enabled on my system.

Switch from PHP 7.2 to PHP 5.6

We will describe two ways to switch from PHP 7.2 to PHP 5.6; one is through Apache2 and the other is through the CLI.

Through Apache2

First, disable the currently enabled version of PHP through the following command:

$ sudo a2dismod php7.2

And then, enable the other version of PHP through the following command:

$ sudo a2enmod php5.6

Now when you restart the apache2 service through the following command, the PHP 5.6 will be enabled on your system.

$ sudo service apache2 restart

Through CLI

Use the following command to update your system to now use PHP 5.6 as the default PHP version.

$ sudo update-alternatives --set php /usr/bin/php5.6

Alternatively, you can use the following command to achieve the same purpose:

$ sudo update-alternatives --config php

The command lists all the available versions of PHP installed on your system. Enter the selection number of the version you want to enable on your system and hit enter so that the new version will be enabled.

This article will guide you in installing your desired version of PHP on Ubuntu 18.04. If you have more than one version of PHP installed on your system, the article also helps you in checking which version is currently enabled and also how to switch from one version to the other.

How to install php5 and php7 on Ubuntu 18.04 LTS