Install PHP 8.0
Debian / Ubuntu

https://php.watch/articles/php-8.0-installation-update-guide-debian-ubuntu

 

How to install/update PHP 8.0 (Debian/Ubuntu)

Published On2020-10-31

PHP 8.0 brings several new features and improvements in performance, syntax, security, and stability. Installing PHP 8.0 on just about any sort of server/development setup is made easy with pre-compiled packages available in all currently supported Debian and Ubuntu versions.

Extensions and Dependency Changes in PHP 8.0

Despite the massive amount of changes, there are not many changes in PHP 8.0 in its dependencies and extension structure.

  1. JSON extension is now always available, and there is no compile flag to exclude it. This means there is no need to explicitly install php-json packages anymore.
  2. xmlrpc extension is moved to PECL with good reasons. Software repositories mentioned in this post does not include xmlrpc extension for PHP 8.0.

In addition, the GD extension has its name changed from php_gd2.dll to php_gd.dll in Windows that will be covered in a separate article.

INI changes in PHP 8.0

There are few INI file changes as well.

  1. Assertions throw exceptions by default – (assert.exception=1)
  2. Default error display is set to E_ALL – (error_reporting=-1)
  3. Startup errors are displayed by default – (display_startup_errors=1)

Due to new features and changes in PHP 8.0,

Installing PHP 8.0 on Ubuntu and Debian is made easy by the great efforts of Ondřej Surý. He maintains a PHP PPA that contains PHP 8.0 and several popular PECL extensions ready to install on any current Debian/Ubuntu system.


1. List existing PHP packages

If you are updating from an existing PHP version to 8.0, it's important to get a list of existing PHP packages. Albeit the small extension changes, installing the PHP 8.0 counterpart packages is the easiest way to make sure the upgrade is smooth and covers the same packages from the current PHP version.

dpkg -l | grep php | tee packages.txt

This command will list all the packages containing name php, and writes it to a file named packages.txt, which can be referred anytime.

2. Add ondrej/php PPA

Ubuntu

sudo add-apt-repository ppa:ondrej/php # Press enter when prompted.
sudo apt-get 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.0 and extensions

All PHP 8.0 packages follow php8.0-NAME pattern, and php8.0-common package includes a sensible set default of extensions (such as `php8.0-).

Install PHP 8.0 with CLI

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

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

You can 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.0-NAME pattern. Refer to the packages.txt file to see a list of existing packages if you are upgrading an existing system.

Note that you do not need to install php8.0-json as it is now included by default.

An example to install a few more useful extensions:

sudo apt install php8.0-{bz2,curl,intl,mysql,readline,xml}

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

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

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.0-fpm package provides integration with PHP 8.0 via FPM.

sudo apt install php8.0-fpm

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

sudo apt install libapache2-mod-php8.0

Note that the Apache2Handler is renamed to php_module from php7_module in PHP 8.0. The libapache2-mod-php8.0 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.0 installation

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

php -v
php -m
# php -v
PHP 8.0.0-dev (cli) (built: Oct 4 2020 14:04:36) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.0-dev, Copyright (c) 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 '^php7.4.*'

This assumes you are using PHP 7.4 as the previous version. Change php7.4 part of the command above with the appropriate PHP version.

Running PHP 8.0 with Other Versions

Instead of removing old PHP versions, it is also possible to run multiple PHP versions side-by-side.

The PHP 8.0 CLI will be installed at /usr/bin/php8.0 location by default. Similarly, other PHP binary files will be located in the same directory (/usr/bin/php7.4, /usr/bin/php7.3, 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.0 80 auto mode
1 /usr/bin/php7.4 74 manual mode
2 /usr/bin/php8.0 80 manual mode

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

To set the path without the interactive prompt:

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

What's new and changed in PHP 8.0

For a complete list of changes in PHP 8.0, see What's new and changed in PHP 8.0

Scroll to Top