Installing PHP 8 on Debian 10

PHP is an open source general-purpose 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 commonly used on Linux, Unix, Windows, Mac OS and other operating systems. With PHP, you have the freedom to choose the operating system and 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 multiple PHP versions, we will also explain how to disable one version and choose the default version on the system.

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

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

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

The official PHP site, php.net, maintains a list of all PHP releases to date at the following link:

http://php.net/releases/

Installing PHP 8 and PHP 7.4 on Debian

To install PHP versions 8 and 7.4, open your terminal and enter the following command in order

Update your system’s repository index:

$ sudo apt-get update

Service packs

In this article, we will be using the SURY PHP PPA repository, which contains all PHP versions released to date. So let’s first load its gpg key with the following commands:

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

Install the required packages

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

Add Sury GPG key

It’s 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

After adding the SURY repository, you need to update your system repository by adding it to the Internet. This way you can install the latest available software version on your system. To do this, enter the following command:

$ sudo apt-get update

Service packs

It’s time to install PHP 7.4 on 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

Then the software will be installed on your system. You can also install some PHP modules, depending on the system requirements of the PHP based software you are using. You can get a list of available modules like this:

For PHP 8

$ sudo apt-cache search php8.0

For PHP 7.4

$ sudo apt-cache search php7.4

Then install the required PHP modules using suitable command. For instance:

$ sudo apt install php8.0-mysqli

To add MySQL module to PHP 8.0.

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

$ php -v

or,

$ php --version

Check PHP version

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

My system output shows PHP 7.4.3 is installed on my system.

Install PHP version 5.6

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

After adding the SURY repository, you need to update your system repository by adding it to the Internet. This way you can install the latest available software version on your system. To do this, enter the following command:

$ sudo apt-get update

It’s time to install PHP 5.6 on 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

Then the software will be installed on your system.

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 actually installed on your system.

PHP 5.6 installed successfully

My system output shows PHP 5.6.4 is installed on my system. If you have a later version of php installed on your system, the version returned will be this one.

Switching between installed PHP versions

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

Check which version is included

We will describe two ways to check which PHP version is enabled on your system; one via Apache2 and the other via the command line interface.

Via 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 find out which one is currently enabled:

$ ls -l mods-*/*php*

Available PHP versions in Apache

In the output, you can see the currently active PHP version is highlighted. In our case, this is PHP 7.4.

Via CLI

It is also very easy to check the currently active PHP version via the CLI. Run the following command, which is used to update the default software alternative in Debian and thus lists all available alternatives.

$ sudo update-alternatives --config php

Change PHP version

In the output of the above command, the currently active PHP version is indicated by an asterisk *. As you can see, in our case it is PHP 7.4.

Migrating from PHP 7.4 to PHP 5.6

We will describe two ways to migrate from PHP 7.4 to PHP 5.6; one through Apache2 and the other through the command line interface.

Via Apache2

First, disable the current PHP version with the following command:

$ sudo a2dismod php7.4

Enable PHP 7.4 in Apache

Then enable another PHP version with the following command:

$ sudo a2enmod php5.6

Enable PHP 5.6 in Apache

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

$ sudo service apache2 restart

Restart Apache

We also checked that 5.6 is now our default php version.

Via 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 goal:

$ sudo update-alternatives --config php

Installing PHP 8 on Debian 10

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

Migrating from PHP 5.6 to PHP 7.4

We will describe two ways to migrate from PHP 5.6 to PHP 7.4; one via Apache2 and the other via the command line interface.

Via Apache2

First, disable the current PHP version with the following command:

$ sudo a2dismod php5.6

Then enable another PHP version with the following command:

$ sudo a2enmod php7.4

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

$ sudo service apache2 restart

Via 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 goal:

$ sudo update-alternatives --config php

The command lists all available PHP versions installed on your system. Enter the selection number of the version you want to enable on your system and press Enter to activate the new version.

This article helped you install your desired PHP version on Debian 10 Buster. If you have more than one PHP version installed on your system, this article will also help you check which version is currently enabled and how to switch from one version to another.

Installing PHP 8 on Debian 10

Sidebar