How to Install Different / Multiple PHP Versions of PHP in Debian Bullseye


PHP (recursive acronym for PHP: Hypertext Preprocessor) is a server-side scripting language designed for web development but also used as a general-purpose programming language. Different applications require various versions of PHP, so in this tutorial, we are going to install multiple versions of PHP on Debian Bullseye.

We will also show how to set the default version of PHP to be used on the Debian system as when required. We will be installing PHP 7.4 and PHP 8 on a single Debian Server.

Currently, the supported stable version of PHP in the Debian APT software repositories is the PHP 7.4. You can confirm this by running the apt command below.

$ sudo apt show php-fpm -a

The output should be similar with below:

Package: php-fpm
Version: 2:7.4+76
Priority: optional
Section: php
Source: php-defaults (76)
Maintainer: Debian PHP Maintainers <[email protected]>
Installed-Size: 13.3 kB
Depends: php7.4-fpm
Download-Size: 6424 B
APT-Sources: bullseye/main arm64 Packages
Description: server-side, HTML-embedded scripting language (FPM-CGI binary) (default)
This package provides the Fast Process Manager interpreter that runs
as a daemon and receives Fast/CGI requests.
PHP (recursive acronym for PHP: Hypertext Preprocessor) is a widely-used
open source general-purpose scripting language that is especially suited
for web development and can be embedded into HTML.
This package is a dependency package, which depends on latest stable
PHP version (currently 7.4).

We can verify that the default PHP version is 7.4.

PHP 7.4 Installation

To install the default PHP version from the Debian software repositories, use the command below:

$ sudo apt install php-fpm

The system will respond like below:

Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
php7.4-cli php7.4-common php7.4-fpm php7.4-json php7.4-opcache php7.4-readline
Suggested packages:
The following NEW packages will be installed:
php-fpm php7.4-cli php7.4-common php7.4-fpm php7.4-json php7.4-opcache php7.4-readline
0 upgraded, 7 newly installed, 0 to remove and 0 not upgraded.
Need to get 3728 kB of archives.
After this operation, 17.7 MB of additional disk space will be used.
Do you want to continue? [Y/n] y

Just press "y" followed by "Enter" and the installation process will continue.

When the installation process finished, let's verify our installation by typing this command:

$ php -v

It should respond with something like this:

PHP 7.4.14 (cli) (built: Jan 12 2021 10:26:28) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
with Zend OPcache v7.4.14, Copyright (c), by Zend Technologies

PHP 8 Installation

Next, we'll install the latest PHP, version 8. The command is similar with PHP 7.4 installation, but we add 8.0 so that system will not install the default one:

$ sudo apt install php8.0-fpm

Next steps should be similar with previous PHP 7.4 installation.

Set Default PHP Version

We can set the default PHP version to be used on the system with the update-alternatives command.

$ sudo update-alternatives --set php /usr/bin/php8.0

System will respond with something like this:

update-alternatives: using /usr/bin/php8.0 to provide /usr/bin/php (php) in manual mode

After setting it, check the PHP version to confirm as follows:

$ php -v

We should get something like below:

PHP 8.0.1 (cli) (built: Jan 12 2021 10:05:21) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.1, Copyright (c) Zend Technologies
with Zend OPcache v8.0.1, Copyright (c), by Zend Technologies

To switch back to PHP 7.4, we'll use update-alternatives command again:

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

Final Words

I hope that you now know how to install different / multiple PHP versions in Debian Bullseye. If you run into any issues or have any feedback feel free to drop a comment below.

Tags PHP Linux

0 comment

Leave a reply

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