Install PHP 8.1 on Debian

How to Install PHP 8.1 on Debian 11

“HyperText Processor”, commonly known as PHP, is the open-source programming language used for Web application development. It is a scripting language, mostly used for the front end with HTML. The latest PHP 8.1 version is officially released on November 25th, 2021.

In this tutorial, we will show you how to install PHP 8.1 on a Debian 11 system.

Update Operating System

Update your Debian 11 operating system to make sure all existing packages are up to date:

$ sudo apt update && sudo apt upgrade -y

Add PHP Repository

By default, PHP 8.1 is not included in the Debian 11 default repository. So you will need to add the DEB.SURY.ORG repository to APT.

First, install the required packages using the following command:

$ sudo apt-get install ca-certificates apt-transport-https software-properties-common -y

Once all the packages are installed, add a Sury repository to APT using the following command:

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

Next, download and add the GPG key with the following command:

$ wget -qO - https://packages.sury.org/php/apt.gpg | apt-key add -

Once you are done, update the repository with the following command:

$ apt-get update -y

Install PHP 8.1

Now, you can install the PHP 8.1 using the following command:

$ sudo apt-get install php8.1
Install PHP 8.1
Install PHP 8.1

Once the PHP is installed, you can check the PHP version on your system with the following command:

$ php8.1 --version

Output:

$ php8.1 --version
PHP 8.1.1 (cli) (built: Dec 20 2021 21:35:13) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.1, Copyright (c) Zend Technologies
with Zend OPcache v8.1.1, Copyright (c), by Zend Technologies

Install PHP 8.1 FPM for Nginx

For the Nginx web server, you need to install the FPM service, you can install it using the following command:

$ sudo apt install php8.1-fpm

Once the installation has been completed, you can confirm that the PHP-FPM service has been installed correctly with the following command:

$ systemctl status php8.1-fpm

Output:

 php8.1-fpm.service - The PHP 8.1 FastCGI Process Manager
Loaded: loaded (/lib/systemd/system/php8.1-fpm.service; enabled; vendor preset: enabled)
Active: active (running)
Docs: man:php-fpm8.1(8)
Process: 43762 ExecStartPost=/usr/lib/php/php-fpm-socket-helper install /run/php/php-fpm.sock /etc/php/8.1/fpm/pool.d/www.conf 81 (code=exited, status=0>
Main PID: 43759 (php-fpm8.1)
Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec"
Tasks: 3 (limit: 2303)
Memory: 8.9M
CPU: 194ms
CGroup: /system.slice/php8.1-fpm.service
├─43759 php-fpm: master process (/etc/php/8.1/fpm/php-fpm.conf)
├─43760 php-fpm: pool www
└─43761 php-fpm: pool www

Install PHP Extension

Installing PHP extensions are simple with the below-mentioned syntax:

$ sudo apt install php8.1-[extension]

Replace [extension] with the extension you want to install, if you want to add multiple extensions then include them in braces:

$ sudo apt install php8.1-mysql php8.1-imap php8.1-ldap php8.1-xml php8.1-fpm php8.1-curl php8.1-mbstring php8.1-zip

To check loaded PHP modules use the command:

$ php8.1 -m

Example Output:


[PHP Modules]
..............
imap
json
ldap
libxml
mbstring
mysqli
mysqlnd
openssl
pcntl
pcre
PDO
pdo_mysql
..............

[Zend Modules]
Zend OPcache

Running PHP 8.1 with Other Versions

Instead of removing old PHP versions, it is also possible to run multiple PHP versions side-by-side.

Multiple PHP versions
Multiple PHP versions

The update-alternatives command provides an easy way to switch between PHP versions for PHP CLI.

$ sudo update-alternatives --config php

This brings up a prompt to interactively select the alternative PHP binary path that php points to.

There are 2 choices for the alternative php (providing /usr/bin/php).

  Selection    Path             Priority   Status
------------------------------------------------------------
* 0            /usr/bin/php8.1   81        auto mode
  1            /usr/bin/php7.4   74        manual mode
  2            /usr/bin/php8.1   81        manual mode

To set the path without the interactive prompt:

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

Comments and Conclusion

In the tutorial, you have learned how to install PHP 8.1 on Debian 11.

If you have any questions please leave a comment below.

4 thoughts on “How to Install PHP 8.1 on Debian 11

Leave a Reply

Your email address will not be published. Required fields are marked *