How to install php5 and php7 on Ubuntu 18.04 LTS

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:

https://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.

https://sive.host/images/lwati/word-image-44.png

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

https://sive.host/images/lwati/word-image-45.png

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.

https://sive.host/images/lwati/word-image-47.png

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.

https://sive.host/images/lwati/word-image-48.png

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*

https://sive.host/images/lwati/word-image-49.png

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

https://sive.host/images/lwati/word-image-50.png

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

https://sive.host/images/lwati/word-image-51.png

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

$ sudo a2enmod php7.2

https://sive.host/images/lwati/word-image-52.png

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

https://sive.host/images/lwati/word-image-53.png

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

https://sive.host/images/lwati/word-image-54.png

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

$ sudo update-alternatives --config php

https://sive.host/images/lwati/word-image-55.png

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.

  • 0 Users Found This Useful
Was this answer helpful?

Related Articles

12 Useful “df” Commands to Check Disk Space in Linux

Useful df Command Examples This article explain a way to get the full information of Linux disk...

How to Disable SELinux

How to Disable SELinux You’ve setup a new system, or installed something new on your Linux...

How to set a Static IP in Ubuntu Linux

How to set a Static IP in Ubuntu LinuxOpen the terminal command line application or ssh into the...

Linux Check Disk Space Command To View System Disk Usage

Linux Check Disk Space Command To View System Disk Usage Linux command to check disk space...