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

This article describes step-by-step how to install PHP versions 5.6, 8.0 and 8.1 on your Ubuntu. After installing the two versions, it also explains how you can disable one version and enable another version as the default version for the system.

We have run the commands and procedures mentioned in this article on an Ubuntu 20.04 LTS system. The same commands will work on the old Ubuntu 18.04 LTS version as well.

In this article, we use the Ubuntu command line, the terminal, to install and configure PHP. You can open the Terminal application using either the System Dash or the Ctrl+Alt+t key combination.

The official PHP website, php.net, provides a list of all PHP versions to date at 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 index 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

Update package repo lists

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 on Ubuntu

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.

Now you have PHP 5.6 installed, but most use cases require that you install additional PHP modules like MySQL support, Curl, GD, etc. Run the below command to install some commonly used modules.

apt-get install php5.6-gd php5.6-mysql php5.6-imap php5.6-curl php5.6-intl php5.6-pspell php5.6-recode php5.6-sqlite3 php5.6-tidy php5.6-xmlrpc php5.6-xsl php5.6-zip php5.6-mbstring php5.6-soap php5.6-opcache libicu65 php5.6-common php5.6-json php5.6-readline php5.6-xml

Install PHP version 8.0

In order to install PHP version 8.0, first, open your Ubuntu Terminal and enter the following command in order to add the Ondrej PHP repository to your Ubuntu system (in case you have not done that already in the above chapter to install PHP 5.6).

$ 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 8.0 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 php8.0

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.

Now you have PHP 5.6 installed, but most use cases require that you install additional PHP modules like MySQL support, Curl, GD, SQLite, XML, etc. Run the below command to install some commonly used modules.

apt-get install php8.0-gd php8.0-mysql php8.0-imap php8.0-curl php8.0-intl php8.0-pspell php8.0-sqlite3 php8.0-tidy php8.0-xsl php8.0-zip php8.0-mbstring php8.0-soap php8.0-opcache libonig5 php8.0-common php8.0-readline php8.0-xml

Install PHP version 8.1

In order to install PHP version 8.1, first, open your Ubuntu Terminal and enter the following command in order to add the Ondrej PHP repository to your Ubuntu system (in case you have not done that already in one of the above chapters).

$ 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 8.1 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 php8.1

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.

Now you have PHP 8.1 installed, but most use cases require that you install additional PHP modules like MySQL support, Curl, GD, SQLite, XML, etc. Run the below command to install some commonly used modules.

apt-get install php8.1-gd php8.1-mysql php8.1-imap php8.1-curl php8.1-intl php8.1-pspell php8.1-sqlite3 php8.1-tidy php8.1-xsl php8.1-zip php8.1-mbstring php8.1-soap php8.1-opcache libonig5 php8.1-common php8.1-readline php8.1-xml

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*

Choose PHP version for 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 8.0 or PHP 8.1

We will describe two ways to switch from PHP 5.6 to PHP 8.0; 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 php8.0

 

If you want to switch to PHP 8.1 instead, use this command:

$ sudo a2enmod php8.1

Enable PHP 8

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

$ sudo service apache2 restart

Through CLI

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

$ sudo update-alternatives --set php /usr/bin/php8.1

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 8.0 to PHP 5.6

We will describe two ways to switch from PHP 8.0 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 php8.0

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, 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 20.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 PHP 5.6, PHP 8.0 and PHP 8.1 on Ubuntu 20.04 LTS

Karim Buzdar

About the Author: Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. You can reach Karim on LinkedIn