Install PHP 7.1/7.2/7.3/7.4 on Debian 11

|
Last Updated:
|
|

If you want to learn how to install PHP 7.1/7.2/7.3/7.4 on Debian 11, then follow through this guide.

PHP is a fast, flexible, pragmatic, popular general-purpose scripting language that is especially suited to web development.

Install PHP 7.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 PHP 7.4 on Debian 11

As seen from the command output above, the default Debian 11 main repositories provide PHP 7.4.

This make it easy install PHP 7.4 on Debian 11.

apt install php

The following additional packages will be installed:
  apache2 apache2-bin apache2-data apache2-utils libapache2-mod-php7.4 libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap libcurl4 liblua5.3-0 libsodium23
  php-common php7.4 php7.4-cli php7.4-common php7.4-json php7.4-opcache php7.4-readline psmisc 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-php7.4 libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap libcurl4 liblua5.3-0 libsodium23 php
  php-common php7.4 php7.4-cli php7.4-common php7.4-json php7.4-opcache php7.4-readline psmisc ssl-cert
0 upgraded, 22 newly installed, 0 to remove and 0 not upgraded.
Need to get 7,262 kB of archives.
After this operation, 28.3 MB of additional disk space will be used.
Do you want to continue? [Y/n] y

Once the installation command completes, run the command below to verify the installed version of PHP on Debian 11;

php -v
PHP 7.4.21 (cli) (built: Jul  2 2021 03:59:48) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.21, Copyright (c), by Zend Technologies

Install PHP 7.4 Modules on Debian 11

By default, quite a number of PHP 7.4 modules are already enabled.

php -m

[PHP Modules]
calendar
Core
ctype
date
exif
FFI
fileinfo
filter
ftp
gettext
hash
iconv
json
libxml
openssl
pcntl
pcre
PDO
Phar
posix
readline
Reflection
session
shmop
sockets
sodium
SPL
standard
sysvmsg
sysvsem
sysvshm
tokenizer
Zend OPcache
zlib

[Zend Modules]
Zend OPcache

To install additional modules, first check if the modules is provided by the default repositories;

apt-cache search php7.4-EXTENSION

Where EXTENSION is the PHP module you want to install. For example;

apt-cache search php7.4-mysql

If the module is available, then install it using the command;

apt install php7.4-EXTENSION

For example;

apt install php7.4-mysql

Install PHP 7.1/7.2/7.3 on Debian 11

Install SURY Repository

In order to install PHP 7.1/7.2/7.3 on Debian 11, you need to add SURY, the third party repositories that provides PHP packages for Debian systems.

To add SURY repository on Debian 11, run the commands below;

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

Next, update the system package cache;

apt update

Install PHP 7.1 on Debian 11

Once the repository is in place, install PHP 7.1 on Debian 11 by executing the command;

apt install php7.1

To install PHP 7.1 modules, run the command;

apt install php7.1-EXTENSION

Install PHP 7.2 on Debian 11

To install PHP 7.2 on Debian 11 by executing the command;

apt install php7.2

To install PHP 7.2 modules, run the command;

apt install php7.2-EXTENSION

Install PHP 7.3 on Debian 11

Once the repository is in place, install PHP 7.3 on Debian 11 by executing the command;

apt install php7.3

To install PHP 7.3 modules, run the command;

apt install php7.3-EXTENSION

Set default PHP version on Debian 11

If you have multiple versions of PHP installed on Debian 11, you can choose to make one of the versions as the default PHP version.

To list the available versions;

update-alternatives --config php

Sample command output;

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

  Selection    Path             Priority   Status
------------------------------------------------------------
* 0            /usr/bin/php7.4   74        auto mode
  1            /usr/bin/php7.1   71        manual mode
  2            /usr/bin/php7.2   72        manual mode
  3            /usr/bin/php7.3   73        manual mode
  4            /usr/bin/php7.4   74        manual mode

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

As you can see, we have PHP 7.4 as the default version of PHP.

To change the default version, simply enter the number that matched the appropriate version you want to make as the default and press ENTER.

For example, to make PHP 7.2 as the default version, simply type 2 and press ENTER.

You can simply execute the command below to change the version straight away;

update-alternatives --set php /usr/bin/php7.2

Checking the version;

php -v
PHP 7.2.34-23+0~20210701.63+debian11~1.gbpd7cd48 (cli) (built: Jul  1 2021 16:13:42) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.2.34-23+0~20210701.63+debian11~1.gbpd7cd48, Copyright (c) 1999-2018, by Zend Technologies

And that is it on how to you can install PHP 7.x on Debian 11.

Other Tutorials

Install PHP 8 on Debian 11

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