How to Install Latest PHP 8.1 on Debian 11/10

Debian is a secure and stable Linux-based operating system distribution offering unmetered server performance to web-based applications. Debian 11Bullseye” is profiled with improved security, exFAT file systems support, and multiple languages support as features that go hand in hand with numerous web-based applications.

Among the features and improvements paraded in PHP 8, the JIT compiler for web apps is most notable. Its benefits include better numerical code performance, better PHP web application code execution and performance, and potentially fast PHP code due to supported C to PHP code migration.

Other notable new/improved features in PHP 8 include strict type checks, especially for Bitwise/Arithmetic operators, saner numeric strings, nullsafe operator, named arguments, weak maps, throw expression, and constructor property promotion.

This article will give a walk-through on installing PHP 8 on Debian 11/10.

Prerequisites

Have root/sudoer user privileges on the Debian 11 system you are using.

Installing PHP 8 on Debian Linux

Updating your Debian system ensures it stays in the best-performing state on top of applying the needed security patches.

$ sudo apt update && sudo apt upgrade -y 

If you have older PHP versions like 7.x, you might want to uninstall it to primarily benefit from the features and improvements attached to PHP 8.

$ sudo apt remove --purge php*
Remove PHP in Debian
Remove PHP in Debian

Next, install the required PHP 8 installation dependencies and also configure the UFT-8 Unicode standard.

$ sudo apt install -y lsb-release ca-certificates apt-transport-https software-properties-common gnupg2   
$ sudo locale-gen en_US.UTF-8

Now add the Ondřej Surý PPA repository, which will be responsible for tracing, retrieving, and installing the latest PHP version on your Debian 11 system.

$ echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/sury-php.list
$ sudo wget -qO - https://packages.sury.org/php/apt.gpg | sudo gpg --no-default-keyring --keyring gnupg-ring:/etc/apt/trusted.gpg.d/debian-php-8.gpg --import
$ sudo chmod 644 /etc/apt/trusted.gpg.d/debian-php-8.gpg

After adding a repository, update the Debian system to recognize the inclusion of the Ondřej Surý PPA repository. Also, if future PHP versions are released, upgrading to them will require a simple Debian 11 system update command like the one below:

$ sudo apt update && sudo apt upgrade -y

Finally, install PHP 8.1 on Debian 11.

$ sudo apt install php8.1
Install PHP in Debian
Install PHP in Debian

Confirm the installed php version.

$ php --version 
Check PHP in Debian
Check PHP in Debian

To install other PHP extensions, reference the syntax:

$ sudo apt install php8.1-name_of_extension

For instance, to install a php-fpm extension, we will execute the command:

$ sudo apt install php8.1-fpm 

This article has taught us how to install PHP and any of its associated extensions on Debian 11. You are now ready to pursue your web-based applications/projects.

Got something to say? Join the discussion.

Have a question or suggestion? Please leave a comment to start the discussion. Please keep in mind that all comments are moderated and your email address will NOT be published.