Install PHP 8.0/8.1/8.2/8.x on Debian 11

|
Last Updated:
|
|

In this tutorial, you will learn how to install PHP 8.0/8.1/8.2/8.x on Debian 11. PHP 8.x is a major update of the PHP language.
It contains many new features and optimizations including named arguments, union types, attributes, constructor property promotion, match expression, nullsafe operator, JIT, and improvements in the type system, error handling, and consistency
.

Install PHP 8.x on Debian 11

The default Debian 11 repositories provide PHP 7.4;

apt-cache policy php

php:
  Installed: (none)
  Candidate: 2:7.4+76
  Version table:
     2:7.4+76 500
        500 http://deb.debian.org/debian bullseye/main amd64 Packages

Install SURY APT Repository

To install PHP 8.x, you need to install SURY, third party repositories that provides PHP packages as follows;

apt -y install apt-transport-https lsb-release ca-certificates curl wget gnupg2
wget -qO- https://packages.sury.org/php/apt.gpg | gpg --dearmor > /etc/apt/trusted.gpg.d/sury-php-8.x.gpg
sh -c 'echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/php.list'

Run System Update

Re-synchronize your system packages to the latest versions;

apt update

Install PHP 8.x on Debian 11

PHP 8 is now available for installation;

apt-cache policy php

php:
  Installed: (none)
  Candidate: 2:8.2+93+0~20221211.45+debian11~1.gbpdb4dcc
  Version table:
     2:8.2+93+0~20221211.45+debian11~1.gbpdb4dcc 500
        500 https://packages.sury.org/php bullseye/main amd64 Packages
     2:7.4+76 500
        500 http://deb.debian.org/debian bullseye/main amd64 Packages

You can now install PHP 8.x as follows;

Install PHP 8.0 on Debian 11

To install PHP 8.0 on Debian 11, run the command below;

apt install php8.0

This command will install PHP 8.0 along with other required package dependencies.

Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  apache2 apache2-bin apache2-data apache2-utils libapache2-mod-php8.0 libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap liblua5.3-0 libsodium23 php-common
  php8.0-cli php8.0-common php8.0-opcache php8.0-readline ssl-cert
Suggested packages:
  apache2-doc apache2-suexec-pristine | apache2-suexec-custom www-browser php-pear
The following NEW packages will be installed:
  apache2 apache2-bin apache2-data apache2-utils libapache2-mod-php8.0 libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap liblua5.3-0 libsodium23 php-common
  php8.0 php8.0-cli php8.0-common php8.0-opcache php8.0-readline ssl-cert
0 upgraded, 18 newly installed, 0 to remove and 3 not upgraded.
Need to get 6,658 kB of archives.
After this operation, 28.5 MB of additional disk space will be used.
Do you want to continue? [Y/n] y

Confirming the installed version;

php -v

PHP 8.0.28 (cli) (built: Feb 14 2023 18:26:48) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.28, Copyright (c) Zend Technologies
    with Zend OPcache v8.0.28, Copyright (c), by Zend Technologies

Install PHP 8.1 on Debian 11

To install PHP 8.1 on Debian 11, run the command below;

apt install php8.1

This command will install PHP 8.1 along with other required package dependencies.

Confirming the installed version;

php -v

PHP 8.1.17 (cli) (built: Mar 16 2023 14:37:38) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.17, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.17, Copyright (c), by Zend Technologies

Install PHP 8.2 on Debian 11

To install PHP 8.2 on Debian 11, run the command below;

apt install php8.2

This command will install PHP 8.2 along with other required package dependencies.

Confirming the installed version;

php -v

PHP 8.2.4 (cli) (built: Mar 16 2023 14:37:38) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.4, Copyright (c) Zend Technologies
    with Zend OPcache v8.2.4, Copyright (c), by Zend Technologies

Installing other PHP 8.0/8.1/8.2/8.x modules;

You can install other PHP 8.x modules using the command;

apt install php8.N-EXTENSION

Where N is the minor version of PHP 8 which can either be 0, 1, 2…

Replace EXTENSION with the specific module/extension. For example, for PHP 8.0 modules;

apt install php8.0-mysql php8.0-xml

And that is it.

How to Set Default PHP Version on Debian

You can set the default PHP version using the update-alternatives command.

If you have multiple versions of PHP installed, there could be some situations that might make you choose a default version to use.

You can list the paths of the installed PHP versions by running the command update-alternatives --list php.

update-alternatives --list php

Sample output;

/usr/bin/php8.0
/usr/bin/php8.1
/usr/bin/php8.2

Thus, to set the default PHP version, you can use the update-alternatives command with the --config option followed by the path of the desired PHP version. For example, to set PHP version 8.1 as the default PHP version, run the command;

update-alternatives --set php /usr/bin/php8.1

You can also run the command interactively by omitting the path to PHP;

update-alternatives --config php

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

  Selection    Path             Priority   Status
------------------------------------------------------------
* 0            /usr/bin/php8.2   82        auto mode
  1            /usr/bin/php8.0   80        manual mode
  2            /usr/bin/php8.1   81        manual mode
  3            /usr/bin/php8.2   82        manual mode

Press <enter> to keep the current choice[*], or type selection number:

To set a specific version of PHP as the default version, simply choose a number and press ENTER.

For example, in the above prompt, you can enter 2 and press ENTER to set PHP 8.1 as the default version;


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

  Selection    Path             Priority   Status
------------------------------------------------------------
* 0            /usr/bin/php8.2   82        auto mode
  1            /usr/bin/php8.0   80        manual mode
  2            /usr/bin/php8.1   81        manual mode
  3            /usr/bin/php8.2   82        manual mode

Press <enter> to keep the current choice[*], or type selection number: 2
update-alternatives: using /usr/bin/php8.1 to provide /usr/bin/php (php) in manual mode

Confirm the version;

PHP 8.1.17 (cli) (built: Mar 16 2023 14:37:38) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.17, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.17, Copyright (c), by Zend Technologies

That is all on how to install PHP 8.x on Debian 11.

Other Tutorials

Install MySQL 8 on Debian 11

Install MariaDB 10.6 on Debian 11

SUPPORT US VIA A VIRTUAL CUP OF COFFEE

We're passionate about sharing our knowledge and experiences with you through our blog. If you appreciate our efforts, consider buying us a virtual coffee. Your support keeps us motivated and enables us to continually improve, ensuring that we can provide you with the best content possible. Thank you for being a coffee-fueled champion of our work!

Photo of author
koromicha
I am the Co-founder of Kifarunix.com, Linux and the whole FOSS enthusiast, Linux System Admin and a Blue Teamer who loves to share technological tips and hacks with others as a way of sharing knowledge as: "In vain have you acquired knowledge if you have not imparted it to others".

Leave a Comment