Install/Upgrade 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

  • GD extension supports AVIF image format from PHP 8.1 if the extension is compiled with AVIF image support. However, the installation guide below does not include AVIF image support.
  • Curl extension supports DNS over HTTPS (DoH) since PHP 8.1. This feature requires Curl version 7.62 or later, and will not be available on older Debian/Ubuntu versions that do not bundle a Curl version later than 7.62. For example, this feature will not be available on Ubuntu 18.04.

INI Directive changes in PHP 8.1

  • date_sunrisedate_sunset functions and related INI settings are deprecated
  • filter.default and filter.default_options INI settings are deprecated
  • auto_detect_line_endings INI directive is deprecated

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.


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


sudo apt install apt-transport-https lsb-release ca-certificates wget -y
sudo wget -O /etc/apt/trusted.gpg.d/php.gpg 
sudo sh -c 'echo "deb $(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 GDCurl, etc.) can be installed as software packages following their name (php8.1-gdphp8.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-cliphp8.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-pdophp8.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_eventNginxLitespeed, 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]

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

Shakeel Shahid

About Author

Leave a comment

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

You may also like


How to Install/Upgrade PHP 8.3 on MacOS with Homebrew

With a handful of new features such as typed class constants, granular Exceptions in the DateTime extension, the new json_validate function, functionality changes and improvements, and deprecations, PHP 8.3 is

How to install or upgrade to PHP 8.3 on Fedora, RHEL, CentOS, and more

PHP 8.3 is 2023’s major update to PHP, bringing a handful of new features such as typed class constants, granular Exceptions in DateTime extension, the