How to Install PHP 8.0 on Ubuntu 20.04 LTS

PHP 8.0 is a significant release from PHP 7 series with improvements and changes to the language, including many long-awaited additions by developers everywhere! This includes named arguments; union types. It also boasts improved JIT compilation tools which will help optimize your application code even further than before while increasing performance.

More information about the release and full changelog, which gets updated on every new minor version release, can be found here.

In the following tutorial, you will learn how to install PHP 8.0 on Ubuntu 20.04 LTS Focal Fossa by importing the Ondřej Surý repository, the maintainer for PHP on Debian, and installing, upgrading, or removing howto instructions.

What is PHP?

PHP “recursive acronym for PHP: Hypertext Preprocessor” is a widely-used open source general-purpose scripting language that is especially suited for web development and can be embedded into HTML. What distinguishes PHP from client-side JavaScript is that the code is executed on the server, generating HTML, then sent to the client. The client would receive the results of running that script but would not know the underlying code. You can even configure your webserver to process all your HTML files with PHP, and then there’s no way that users can tell what you have up your sleeve.

Update Ubuntu

Before proceeding, run a system update to ensure all your packages are up-to-date to avoid any conflicts during the installation.

sudo apt update && sudo apt upgrade

Install Required Packages

The following dependencies will need to be installed to install PHP 8.0 successfully. Most of these packages would already be present on your system, but running the command can help ensure they’re installed.

sudo apt install software-properties-common apt-transport-https -y

Import Ondřej Surý PHP Repository

To begin with, import the PHP repository by Ondrej, who has been a PHP maintainer for Debian for over a decade and is widely used amongst Ubuntu servers and users.

Import the PPA using the following command.

sudo add-apt-repository ppa:ondrej/php -y

Once done, it is good to refresh your APT repositories as the PPA may bring additional upgrades to existing dependencies.

sudo apt update

After importing the PPA and running an update, you should see a few packages that need updating; run an upgrade now.

sudo apt upgrade

Install PHP 8.0 with Apache Option

If you run an Apache HTTP server, you can run PHP as an Apache module or PHP-FPM.

Install Apache Module

To install PHP 8.0 as an Apache module, enter the following command.

sudo apt install php8.0 php8.0-common libapache2-mod-php8.0 php8.0-cli

Once installation is complete, restart your Apache server to load the new PHP module.

sudo systemctl restart apache2

Install Apache with PHP-FPM

PHP-FPM (an acronym of FastCGI Process Manager) is a hugely popular alternative PHP (Hypertext Processor) FastCGI implementation.

To install PHP-FPM with the following commands.

sudo apt install php8.0-fpm php8.0-common libapache2-mod-fcgid php8.0-cli

Note, by default, PHP-FPM is not enabled for Apache. You must enable it by the following command.

sudo a2enmod proxy_fcgi setenvif && sudo a2enconf php8.0-fpm

Lastly, restart Apache.

sudo systemctl restart apache2

Verify that PHP-FPM is working:

systemctl status php8.0-fpm

If everything is ok, proceed to enable FPM in the web service you are using.

As a reminder to see what version of PHP 8.0 is installed on your system, use the following command.

php --version

Install PHP 8.0 with Nginx Option

Nginx does not contain native PHP processing like other web servers like Apache. To handle the PHP files, you will need to install PHP-FPM “fastCGI process manager.”

First, check for updates on your system and install PHP-FPM, natively installing the PHP packages required.

In your terminal, use the following command to install PHP 8.0 and PHP 8.0-FPM.

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

Once installed, the PHP-FPM service is automatically started, and you can check the status to make sure it’s running ok.

sudo systemctl status php8.0--fpm

If everything is ok, proceed to enable FPM in the web service you are using.

You will need to edit your Nginx server block and add the example below for Nginx to process the PHP files.

Below is an example of all server blocks that process PHP files that need the location ~ .php$ added.

server {
 location ~ .php$ {
   include snippets/fastcgi-php.conf;
   fastcgi_pass unix:/run/php/php8.0-fpm.sock;
 }

Test Nginx to make sure you have no errors with the adjustments made with the code above; enter the following.

sudo nginx -t

Example output:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Restart the Nginx service for installation to be complete.

sudo systemctl restart nginx

As a reminder to see what version of PHP 8.0 is installed on your system, use the following command.

php --version

Optional Extra PHP 8.0 Extensions

While most would opt to install PHP themselves and know what packages to install, below are examples of commands that can be combined or modified.

First, you can pick which modules from the following command to install extensions that you require that will automatically enable them with your PHP installation.

sudo apt install php8.0-cli php8.0-curl php8.0-mysqlnd php8.0-gd php8.0-opcache php8.0-zip php8.0-intl php8.0-common php8.0-bcmath php8.0-imap php8.0-imagick php8.0-xmlrpc php8.0-readline php8.0-memcached php8.0-redis php8.0-mbstring php8.0-apcu php8.0-xml php8.0-dom php8.0-redis php8.0-memcached php8.0-memcache

Remove the options you do not want. This is optional. It is highly recommended to only install and keep what modules you require from a performance and security standard.

To view modules loaded at any time, you can use the following command.

php -m

Example output:

[PHP Modules]
apcu
bcmath
calendar
Core
ctype
curl
date
dom
exif
FFI
fileinfo
filter
ftp
gd
gettext
hash
iconv
igbinary
imagick
imap
intl
json
libxml
mbstring
memcache
memcached
msgpack
mysqli
mysqlnd
openssl
pcntl
pcov
pcre
PDO
pdo_mysql
Phar
posix
readline
redis
Reflection
session
shmop
SimpleXML
sockets
sodium
SPL
standard
sysvmsg
sysvsem
sysvshm
tokenizer
xdebug
xml
xmlreader
xmlrpc
xmlwriter
xsl
Zend OPcache
zip
zlib

[Zend Modules]
Xdebug
Zend OPcache

Depending on how many modules you have installed, this can be pretty large, and it is always recommended to keep an eye on this and remove any you do not need.

Lastly, use the following command for anyone interested in installing the development branch.

sudo apt install php8.0-dev

Additional developments tool, such as debugging tools, use the following command.

sudo apt install php8.0-xdebug php8.0-pcov

This will install lots of dependencies, and unless you are developing with PHP or have some special requirement to install it, do not use this version.

Comments and Conclusion

All in all, PHP 8.0 is slowly becoming the new standard as more developers update their code to be compatible with it and take advantage of its advantages. WordPress has even pledged support for the next version of PHP, 8.1, which promises even more speed improvements and security enhancements over 8.0. So if you’re looking to make your website faster and more secure, start making the switch to PHP 8 now – it’s well worth it.



Follow LinuxCapable.com!

Like to get automatic updates? Follow us on one of our social media accounts!