How To Install Php 5.6, Php 8.0 And Php 8.1 On Ubuntu 20.04 Lts

PHP is a recursive acronym for Hypertext Processor. It is a general-purpose open source scripting language 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 deactivate one and enable the other as the default version for the system.

We ran the commands and procedures mentioned in this article on a Ubuntu 20.04 LTS system. The same commands will work on Ubuntu 18.04 LTS.

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

The official PHP website, php.net, provides a list of all PHP versions so far at the following link:

http://php.net/releases/

From this list, you can choose whichever version you want 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

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

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

This archive contains all the PHP versions released so far.

Once the Ondrej repository is added, you need to update your system’s repository index with it on the Internet. This way you can install the latest software version available on your system. Enter the following command to do so:

$ sudo apt-get update

Updated repo package list

Now it’s time to install PHP 5.6 to your system. Enter the following command as sudo because only authorized people 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.

To check your installed PHP version number, run the following command:

$ php -v

or,

$ php --version

This 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 you to install additional PHP modules like MySQL support, Curl, GD etc. Run below command to install some modules- Boiler is commonly used.

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

To install PHP 8.0 version first, open your Ubuntu Terminal and enter the following command to add the Ondrej PHP repository to your Ubuntu system (in case you haven’t done that in the chapter above to install PHP 5.6).

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

This archive contains all the PHP versions released so far.

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

$ sudo apt-get update

Now it’s time to install PHP 8.0 on your system. Enter the following command as sudo because only authorized people can install/uninstall and configure software on Ubuntu:

$ sudo apt-get install -y php8.0

The software will then be installed on your system.

To check your installed PHP version number, run the following command:

$ php -v

or,

$ php --version

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

You now have PHP 5.6 installed, but most use cases require you to install additional PHP modules such as MySQL support, Curl, GD, SQLite, XML, etc. Run the command below to install one commonly used module numbers.

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

To install PHP 8.1 version, first open your Ubuntu Terminal and enter the following command to add the Ondrej PHP repository to your Ubuntu system (in case you haven’t done that in one of the chapters above) ).

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

This archive contains all the PHP versions released so far.

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

$ sudo apt-get update

Now it’s time to install PHP 8.1 on your system. Enter the following command as sudo because only authorized people can install/uninstall and configure software on Ubuntu:

$ sudo apt-get install -y php8.1

The software will then be installed on your system.

To check your installed PHP version number, run the following command:

$ php -v

or,

$ php --version

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

You now have PHP 8.1 installed, but most of the use cases require you to install additional PHP modules like support for MySQL, Curl, GD, SQLite, XML, etc. Run the command below to install one commonly used module numbers.

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

Switch 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 important to first find out which version is currently enabled as default on your Ubuntu system.

Check which version is activated

We will describe two ways to check which PHP version is enabled on your system; one is through apache2 and the other is through 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 available PHP modes on your system and know which of them are currently enabled:

$ ls -l mods-*/*php*

Select PHP version for Apache

In the output you can see that the currently active PHP version is highlighted. In our case it was PHP 5.6.

Through CLI

It is also very simple to check the currently active PHP version via CLI. Running the following command is used to update the default alternative for a piece of software on Ubuntu and thus list all available alternatives.

$ sudo update-alternatives --config php

Change PHP CLI version

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

Switch from PHP 5.6 to PHP 8.0 or PHP 8.1

We will describe two ways to migrate from PHP 5.6 to PHP 8.0; one is through apache2 and the other is through CLI.

Through Apache2

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

$ sudo a2dismod php5.6

Turn off PHP 5.6

And then, enable another PHP version via the following command:

$ sudo a2enmod php8.0

If you want to switch to PHP 8.1, use the following command:

$ sudo a2enmod php8.1

Enable PHP 8

Now when you restart the apache2 service via 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 available PHP versions installed on your system. Type the selection number of the version you want to enable on your system and press enter. For example, if I enter 2, PHP 5.6 will be enabled on my system.

Moving from PHP 8.0 to PHP 5.6

We will describe two ways to migrate from PHP 8.0 to PHP 5.6; one is through apache2 and the other is through CLI.

Through Apache2

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

$ sudo a2dismod php8.0

And then, enable another PHP version via the following command:

$ sudo a2enmod php5.6

Now when you restart the apache2 service via 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 available PHP versions installed on your system. Type the selection number of the version you want to enable on your system and press enter so the new version will be enabled.

This article will guide you to install the desired PHP version on Ubuntu 20.04. If you have multiple versions of PHP installed on your system, the article also helps you check which version is currently active and also how to switch from one version to another.

Leave a Comment