Installing PHP 8.x on Debian 12 Linux Server

PHP 8 is the major current version of the Hypertext Preprocessor scripting language. It offers multiple features and performance improvements as compared to the previous releases. In this tutorial, we learn how to install PHP 8.x version on Debian 12 Bookworm desktop or server Linux systems.

Released on November 26, 2020, PHP 8 introduces numerous features, improvements, and optimizations, making it a significant milestone in the language’s evolution. Such as -the introduction of just-in-time (JIT) compilation, named arguments, Union types, the concept of attributes, match expression, nullsafe operator, consistent type errors, and more…

1. Update Debian 12 Package Repositories:

We can install PHP 8 using the default system repository of Debian 12, therefore before going further ensure all the essential packages and repositories are up to date.

sudo apt update && sudo apt upgrade

The above command not only updates the package list but also upgrades existing packages to the latest versions, if available.

2. Install PHP 8.x on Debian 12

While performing this tutorial the latest version of PHP available through the Debian 12 repo, officially, was PHP 8.2. So, to install the same we can use the given command.

sudo apt install php 
php installation in Debian 12

Whereas, if you want to install a few common extensions of PHP, then the command will be like this:

sudo apt install phpextension-name

Example:

sudo apt install php-cli php-fpm php-common php-mbstring php-xml php-mysql

However, those who are looking for some newer or older versions of PHP that are not available by default through the system repository, need to add a third-party repo called Sury.


2. Install Dependencies:

Before adding the Sury PHP repository certain dependencies should be on your system. So, install them first using the given command and then move to the next step.

sudo apt install wget gnupg2 lsb-release

3. Add ondrej/php Repository:

Now, first, we download the GPG key used to sign the Sury repo, and after that will create a repository file to save the link to the Sury repo, which provides up-to-date PHP versions. Run the following commands:

sudo wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg
sudo sh -c 'echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/php.list'

4. Update Repositories Again:

We need to tell our Debian 12 system about the newly added PHP repository, and for that simply run the system update command once more to refresh the APT’s package index cache.

 sudo apt update

5. Install PHP 8.3, 7.4, 7.3, or any other version :

Now, we can install any actively supported version of PHP on Debian 12 Linux using the newly added Sury repository including all the common extensions. Even multiple versions of PHP can be installed now if required by your project.

The installation command will be the same as we saw earlier, however, this time you just need to mention the version of PHP you want in the syntax.

For example, to get PHP 8.3

sudo apt install php8.3

Similarly for php 7.4

sudo apt install php7.4

And if you want to get the extension as per the version you have installed then for that the command will be similar, as shown previously in Step 2.

sudo apt install php8.3-{extension1, extension2, extensions3}

Replace 8.3 with whatever version of PHP you want to install whereas {extension1.. and so on} will be replaced by the exact name of the extension you want to configure.

For example:

sudo apt install php8.3-{cli,fpm,common,mbstring,xml,mysql}

Adjust the list of extensions based on your specific needs.


6. Verify PHP Installation:

After the installation is completed to confirm what version of PHP is configured on your system, you can run:

php -v

This should display information about PHP 8, confirming a successful installation.

PHP version check

If you want to know what modules are installed and activated for your current PHP to use for various web applications, simply type:

php -m

This will list all the loaded php Extensions.

7. Manage multiple versions using update-alternative

In case you have installed multiple versions of PHP on Debian 12, let’s say you have both PHP 7.4 and PHP 8.3. Now, the system will automatically use the higher version as the system’s default but what if you want PHP 7.4 to be the one? In such a case, we can use the “update alternative” command to configure or set the default PHP version on Linux.

sudo update-alternatives --config php

You will see the list of all installed PHP versions along with their priorities of usage by the system. To set the one as the default, manually, enter the Selection number of that and hit the Enter key.

Here in the below screenshot, we have selected PHP 7.4 to set it as a system default version (globally).

Manage multiple PHP versions

To check use php -v

To check the system default PHP

7. Configure PHP (optional):

Furthermore, depending on your needs, you may want to adjust the PHP configuration. The main configuration file is typically located at

 /etc/php/8.3/cli/php.ini 

Replace 8.3 with a version of PHP you have installed and want to edit its configuration file.

For example:

sudo nano /etc/php/8.3/cli/php.ini 

Make the necessary changes and save the file.


8. To Restart PHP-FPM (optional):

If you have installed PHP-FPM (FastCGI Process Manager), then to restart it for applying the changes you have made can be done using the given command:

sudo systemctl restart php8.3-fpm

Again replace 8.3 with your PHP version.


Conclusion:

So, these were the steps anybody can use to install not only the latest version of PHP 8 on Debian 12 even for older versions. Also, make sure the version you are about to configure is supported by your web applications. To get more details, one can always check the PHP documentation for any additional configurations or extensions you might need for your specific projects.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.