Install/Upgrade PHP 8.1 on Ubuntu/Debian

Published On28 Nov 2021

Install or update to PHP 8.1 on Ubuntu/Debian
PHP 8.1 is now released with new features, improvements, and deprecated functionality. Installing PHP 8.1 on a new system, or upgrading an existing PHP setup to PHP 8.1 is made easy on Ubuntu and Debian-based systems with the help of pre-compiled binaries.

None of the current Ubuntu or Debian software repositories offer PHP 8.1 in their default software repositories. However, Ondřej Surý continues to make PHP versions available as a Debian/Ubuntu software repository, and PHP 8.1 packages are now available.

What's new and changed in PHP 8.1

PHP 8.1 brings several new features, improvements, and functionality that are marked deprecated. For a complete list of changes in PHP 8.1, see What's new and changed in PHP 8.1.

Extension and Dependency Changes in PHP 8.1

INI Directive changes in PHP 8.1


1. List existing PHP packages

When updating an existing PHP setup, it is easier to list the existing PHP extensions installed as software packages, to match the PHP 8.1 extensions list.

On systems that install PHP 8.1 afresh, this step is not required.

dpkg -l | grep php | tee packages.txt

This command lists all PHP packages installed, displays them on-screen, and saves to a file named packages.txt in the current working directory.

2. Add ondrej/php PPA

None of the default software repositories on Ubuntu or Debian to date include PHP 8.1. Ondřej Surý maintains a package archive that contains compiled binaries of all current PHP versions, for Ubuntu and Debian.

Once this repository is added, the initial installation and updates can be done with the standard apt commands.

Ubuntu

sudo add-apt-repository ppa:ondrej/php # Press enter when prompted.
sudo apt update

Debian

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

Steps above will add the PPA as a source of packages, that contains all PHP packages and their dependencies such as argon2 and libzip.

3. Install PHP 8.1 and extensions

All PHP 8.1 packages follow php8.1-NAME pattern. Additional extensions (such as GD, Curl, etc.) can be installed as software packages following their name (php8.1-gd, php8.1-curl, etc.).

  • php8.1 is a meta-package that collectively installs a few dependencies such as php8.1-cli and php8.1-readline as well as auxiliary packages such as apache2 if one of php8.1-cli, php8.1-fpm, or libapache2-mod-php8.1 is not explicitly requested.
  • php8.1-common is a meta-package that installs most of the widely used PHP extensions in one-go. It automatically installs package such as php8.1-pdo, php8.1-tokenizer among other useful extensions.

Install PHP 8.1 with CLI

sudo apt install php8.1-common php8.1-cli -y

This command will install several PHP extensions due to php8.1-common, and the CLI for PHP 8.1.

Confirm the installation by running:

php -v # Show PHP version.
php -m # Show PHP modules loaded.

Additional extensions

You can install additional extensions from the same php8.1-NAME pattern. Refer to the packages.txt file to see a list of existing packages if you are upgrading an existing system.

Note that since PHP 8.0, the JSON extension is bundled and is implicitly installed.

An example to install a few more useful extensions:

sudo apt install php8.1-{bz2,curl,intl,xml}

Development Tools

For development environments, code coverage tools or the Xdebug debugger can be installed as well.

sudo apt install php8.1-pcov # PCOV code coverage tool
sudo apt install php8.1-xdebug # Xdebug debugger

This is not recommended for production servers.

Install Server APIs

Depending on the web server you use, you will need to install additional packages to integrate with the web server.


For Apache using mpm_event, Nginx, Litespeed, etc., php8.1-fpm package provides integration with PHP 8.1 via FPM.

sudo apt install php8.1-fpm

For Apache using mod_php, install libapache2-mod-php8.1.

sudo apt install libapache2-mod-php8.1

Note that the Apache2Handler is renamed to php_module from php7_module in PHP 8.0. The libapache2-mod-php8.1 package automatically configures the Apache module location, but if you are updating from an existing PHP setup, you might need to update configuration files; in particularly <IfModule> blocks.

4. Test PHP 8.1 installation

To test the PHP installation and the extensions, run the following commands:

php -v
php -m
# php -v
PHP 8.1.0 (cli) (built: Nov 25 2021 20:21:44) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.0, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.0, Copyright (c), by Zend Technologies
# php -m
[PHP Modules]
Core
ctype
curl
...

Purge old PHP versions

If the new installation is working as expected, you can remove the old PHP packages from the system.

sudo apt purge '^php8.0.*'

This assumes you are using PHP 8.0 as the previous version. Change php8.0 part of the command above with the appropriate PHP version.

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.

The PHP 8.1 CLI will be installed at /usr/bin/php8.1 location by default. Similarly, other PHP binary files will be located in the same directory (/usr/bin/php8.0, /usr/bin/php7.4, etc). The default php name will be symlinked to the latest PHP version by default, but it is possible to change where the default php command links to.

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/php8.0   80        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

Recent Articles on PHP.Watch

All ArticlesFeed 
How to fix PHP Curl HTTPS Certificate Authority issues on Windows

How to fix PHP Curl HTTPS Certificate Authority issues on Windows

On Windows, HTTPS requests made with the Curl extension can fail because Curl has no root certificate list to validate the server certificates. This article discusses the secure and effective solutions, and highlights bad advice that can leave PHP applications insecure.
AEGIS Encryption with PHP Sodium Extension

AEGIS Encryption with PHP Sodium Extension

The Sodium extension in PHP 8.4 now supports AEGIS-128L and AEGIS256 Authenticated Encryption ciphers. They are significantly faster than AES-GCM and CHACHA20-POLY1305. This article benchmarks them and explains how to securely encrypt and decrypt data using AEGIS-128L and AEGIS256 on PHP.
How to Install/Upgrade PHP 8.3 on MacOS with Homebrew

How to Install/Upgrade PHP 8.3 on MacOS with Homebrew

Install PHP 8.3 and PHP extensions on MacOS with Homebrew.
Subscribe to PHP.Watch newsletter for monthly updates

You will receive an email on last Wednesday of every month and on major PHP releases with new articles related to PHP, upcoming changes, new features and what's changing in the language. No marketing emails, no selling of your contacts, no click-tracking, and one-click instant unsubscribe from any email you receive.