Installing PHP 8 on Debian 10

PHP is a general-purpose open-source scripting language that can be embedded in HTML. It stands for HypertextProcessor and is widely used in web development. A scripting language is used to write ready-made programs that are later used to automate tasks. PHP scripts are often used on Linux, Unix, Windows, Mac OS, and other operating systems. With PHP, you have the freedom to choose an operating system and the underlying web server, according to your needs.

In this article, we will explain how to install PHP 8, PHP 7.4, and PHP 5.6 on Debian. After you have installed the multiple PHP versions, we will also explain how to disable one version and choose a default version on the system.

We have run the commands and procedures mentioned in this article on a Debian 10-buster system.

In this article we will use the Debian command line, the terminal, to install and configure PHP. You can open the terminal application using the application launcher's search function as follows:

Debian Terminal

You can access the Application Launcher by hitting the Super/Windows key on your keyboard.

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

http://php.net/releases/

Installing PHP 8 and PHP 7.4 on Debian

In order to install PHP version 8 and 7.4, open your Terminal and enter the following command in order

Update your system’s repository index:

$ sudo apt-get update

Update packages

In this article, we will use the SURY PHP PPA repository which contains all the released versions of PHP to date. So let us first download it’s gpg key through the following commands:

$ sudo apt-get install lsb-release apt-transport-https ca-certificates

Install required packages

$ sudo wget -O /etc/apt/trusted.gpg.d/php.gpg

Add sury GPG key

Now, it is time to add the required repository:

$ echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/php.list

Add sury repository

Once the SURY 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

Update packages

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

$ sudo apt-get install -y php8.0 php7.4

The software will then be installed on your system. You might also want to install some PHP modules, depending on the system requirements of the PHP based software you use. You can get a list of available Module like this:

For PHP 8

$ sudo apt-cache search php8.0

For PHP 7.4

$ sudo apt-cache search php7.4

Then install the PHP modules that you need using the apt command. E.g.:

$ sudo apt install php8.0-mysqli

To add the MySQL module to PHP 8.0.

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

$ php -v

or,

$ php --version

Check the PHP version

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

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

Install PHP version 5.6

In order to install PHP version 5.6, first, open your Terminal and follow all the steps described above to add the SURY repository to your system.

Once the SURY 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 5.6 to your system. Enter the following command as sudo as only an authorized person can install/uninstall and configure software on Linux:

$ 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.

PHP 5.6 installed successfully

The output from my system shows that PHP 5.6.4 is installed on my system. If you have a more latest version of php installed on your system, the returned version will be of that one.

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 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*

Available PHP versions in Apache

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

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 Debian and thus lists all the available alternatives.

$ sudo update-alternatives --config php

Switch PHP version

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

Switching from PHP 7.4 to PHP 5.6

We will describe two ways to switch from PHP 7.4 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.4

Enable PHP 7.4 in Apache

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

$ sudo a2enmod php5.6

Enable PHP 5.6 in Apache

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

Restart Apache

We have also verified that now version 5.6 is our default php version.

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

Set Default PHP version

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. For example, if I enter 2, PHP 7.4 will be enabled on my system.

Switching from PHP 5.6 to PHP 7.4

We will describe two ways to switch from PHP 5.6 to PHP 7.4; 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

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

$ sudo a2enmod php7.4

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

$ sudo service apache2 restart

Through CLI

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

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

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 helped you in installing your desired version of PHP on Debian 10 Buster. 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 between one version to the other.