How to Install PHP 8 on Ubuntu 21.04

To Ensure system updated.

apt update

If any update is available update the system and reboot.

apt upgrade -y && reboot

Add PHP PPA Repository

apt -y install software-properties-common

add-apt-repository ppa:ondrej/php

Output:

root@vps:~# add-apt-repository ppa:ondrej/php
PPA publishes dbgsym, you may need to include 'main/debug' component
Repository: 'deb http://ppa.launchpad.net/ondrej/php/ubuntu/ hirsute main'
Description:
Co-installable PHP versions: PHP 5.6, PHP 7.x and most requested extensions are included. Only Supported Versions of PHP (http://php.net/supported-versions.php) for Supported Ubuntu Releases (https://wiki.ubuntu.com/Releases) are provided. Don't ask for end-of-life PHP versions or Ubuntu release, they won't be provided.

Debian oldstable and stable packages are provided as well: https://deb.sury.org/#debian-dpa

You can get more information about the packages at https://deb.sury.org

IMPORTANT: The <foo>-backports is now required on older Ubuntu releases.

BUGS&FEATURES: This PPA now has a issue tracker:
https://deb.sury.org/#bug-reporting

CAVEATS:
1. If you are using php-gearman, you need to add ppa:ondrej/pkg-gearman
2. If you are using apache2, you are advised to add ppa:ondrej/apache2
3. If you are using nginx, you are advised to add ppa:ondrej/nginx-mainline
   or ppa:ondrej/nginx

PLEASE READ: If you like my work and want to give me a little motivation, please consider donating regularly: https://donate.sury.org/

WARNING: add-apt-repository is broken with non-UTF-8 locales, see
https://github.com/oerdnj/deb.sury.org/issues/56 for workaround:

# LC_ALL=C.UTF-8 add-apt-repository ppa:ondrej/php
More info: https://launchpad.net/~ondrej/+archive/ubuntu/php
Adding repository.
Press [ENTER] to continue or Ctrl-c to cancel.
Adding deb entry to /etc/apt/sources.list.d/ondrej-ubuntu-php-hirsute.list
Adding disabled deb-src entry to /etc/apt/sources.list.d/ondrej-ubuntu-php-hirsute.list
Adding key to /etc/apt/trusted.gpg.d/ondrej-ubuntu-php.gpg with fingerprint 14AA40EC0831756756D7F66C4F4EA0AAE5267A6C
Get:1 http://ppa.launchpad.net/ondrej/php/ubuntu hirsute InRelease [23.9 kB]
Hit:2 http://de.archive.ubuntu.com/ubuntu hirsute InRelease
Hit:3 http://de.archive.ubuntu.com/ubuntu hirsute-updates InRelease
Hit:4 http://de.archive.ubuntu.com/ubuntu hirsute-backports InRelease
Hit:5 http://de.archive.ubuntu.com/ubuntu hirsute-security InRelease
Get:6 http://ppa.launchpad.net/ondrej/php/ubuntu hirsute/main amd64 Packages [36.5 kB]
Get:7 http://ppa.launchpad.net/ondrej/php/ubuntu hirsute/main Translation-en [11.4 kB]
Fetched 71.7 kB in 0s (156 kB/s)
Reading package lists... Done

Install PHP 8

Ensure the added package is up to date.

apt update

If any update is available update the system and reboot.

apt upgrade -y && reboot

Install PHP 8 on Ubuntu

apt -y install php8.0

php -v

Output:

 root@vps:~# php -v
PHP 8.0.5 (cli) (built: May  3 2021 11:54:56) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.5, Copyright (c) Zend Technologies
    with Zend OPcache v8.0.5, Copyright (c), by Zend Technologies

To Install php Extensions.

NOTE: Replace the required extensions name in the command

apt install php8.0-<extension>

To list installed php modules/extensions.

php -m

This concludes our topic of installing PHP 8 on the Ubuntu 21.04 system.