How To Install PHP 8.1 on Debian 11

Update the OS and Packages

It’s good practice to update what you’ve already got installed before installing something new.

sudo apt update
sudo apt -y upgrade

If the kernel is updated, reboot the system before proceeding.

Add the DEB.SURY.ORG Repository

This is a home for packaging various software into Debian and Ubuntu. Most notable package under DEB.SURY.ORG is the PHP packaging.

sudo apt update
sudo apt install -y lsb-release ca-certificates apt-transport-https software-properties-common gnupg2
echo "deb $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/sury-php.list
wget -qO - | sudo apt-key add -
sudo apt update

Install PHP

sudo apt update 
sudo apt install php8.1

When asked, “Do you want to continue?”, press Y and then ENTER. Once installation has completed, you can verify it with the command below. It will return version information for the PHP CLI.

php -v

Install PHP Extensions

You can use the command below to install various PHP extensions.

sudo apt install php8.1-<extension>

Replace <extension> with the identifier of the actual extension you want to install. For example, to install the MySQL extension for PHP:

sudo apt install php8.1-mysql

You can install some of the most common PHP extensions with the following command:

sudo apt install php8.1-{bcmath,bz2,cgi,cli,curl,fpm,gd,intl,ldap,mbstring,mysql,opcache,pgsql,soap,xml,zip}

If you want to know what PHP modules are loaded, you can use the following command:

php --modules

Purge Old PHP Versions

Once you’ve verified the installation and configuration of PHP 8.1 is functioning correctly, you may want to remove the previous version(s).

sudo apt purge '^php7\.4.*'
sudo apt purge '.*php7\.4$'

The above commands would remove PHP 7.4 related packages. If you want to remove a different version of PHP, just change 7.4 to the relevant version.


  • I tried to fix the world, but God wouldn't give me his source code.

    Formerly, CEO and lead developer of a technology company, focusing on the merchant services space. Formerly, of WHMCompleteSolution (WHMCS).

    An avid gamer.

Leave a comment

%d bloggers like this: