How To Install PHP 8 On Ubuntu 20.04 LTS

How To Install PHP 8 On Ubuntu 20.04 LTS

It provides all the steps required to install PHP 8 on Ubuntu 20.04 LTS using PPA. It also provides the steps to install the apache extension to enable PHP for the Apache HTTP Server.

March 30, 2021

This tutorial provides all the steps required to install PHP 8 on Ubuntu 20.04 LTS using PPA. The steps should be similar on the other versions of Ubuntu and Linux based systems. In case you are planning to use PHP with the Apache HTTP Server and MySQL database server, you can install Apache 2 and MySQL 8 before starting the installation of PHP 8. It can be done by following the guides - How To Install Apache 2 On Ubuntu 20.04 LTS and How To Install MySQL 8 on Ubuntu 20.04 LTS. Also, PHP can be used with NGINX Web Server instead of the Apache HTTP Server. NGINX is widely used as a load balancer and reverse proxy for the Apache Web Server and other servers. You can follow How To Install And Configure Nginx on Ubuntu 20.04 LTS and Configure Virtual Host Or Server Block On Nginx.

Notes: The default version of PHP available for Ubuntu 20.04 LTS is PHP 7.4. You can also follow How To Install PHP 7 On Ubuntu 20.04 LTS.

Install PHP 8 from PPA

In this step, we will install PHP 8 using the PPA from Ondrej for Ubuntu 20.04 LTS release. Now, enable the PHP repository from Ondřej Surý using the commands as shown below.

# Refresh indexes
sudo apt update

sudo apt install software-properties-common

# Add PHP Repository
sudo add-apt-repository ppa:ondrej/php

# Press ENTER to enable the repository

We can install PHP 8 on Ubuntu 20.04 LTS using the commands shown below.

# Refresh indexes
sudo apt update

# Install PHP 8 on Ubuntu 20.04 LTS
sudo apt install php8.0

# The command installs apache2 and libapache2-mod-php8.0 in case it's not installed
# The command will also install libapache2-mod-php8.0 in case Apache is already installed

# Verify PHP
php --version

# Output
PHP 8.0.3 (cli) (built: Mar 5 2021 07:54:13) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.3, Copyright (c) Zend Technologies
with Zend OPcache v8.0.3, Copyright (c), by Zend Technologies

# Autoclean
sudo apt autoclean

# Autoremove
sudo apt autoremove
# OR
sudo apt --purge autoremove

It will ask to confirm the installation. Press Y and hit Enter to confirm the installation. The important paths and files of the PHP installation are /usr/lib/php/8.0, /usr/bin/php8.0, and /etc/php/8.0/cli/php.ini.

Install Apache2 Module

In case the Apache2 Module was not installed while installing PHP, we can use the below-mentioned commands to install the Apache2 Module for PHP. Also, refer to How To Install Apache 2 On Ubuntu 20.04 LTS.

# Install Apache2 Module if not installed by default
sudo apt install libapache2-mod-php8.0

PHP for NGINX

We can install PHP FPM and configure it for Nginx. This will eliminate the need for the Apache Web Server and Nginx can handle the PHP requests.

# FPM
sudo apt install php8.0-fpm

# Check Status
systemctl status php8.0-fpm

# Output
php8.0-fpm.service - The PHP 8.0 FastCGI Process Manager Loaded: loaded (/lib/systemd/system/php8.0-fpm.service; enabled; vendor preset: enabled) Active: active (running) since Tue 2021-03-30 00:50:50 UTC; 2min 21s ago Docs: man:php-fpm8.0(8) Process: 34409 ExecStartPost=/usr/lib/php/php-fpm-socket-helper install /run/php/php-fpm.sock /etc/php/8.0/fpm/pool.d/www.> Main PID: 34391 (php-fpm8.0) Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec" Tasks: 3 (limit: 2326) Memory: 11.8M CGroup: /system.slice/php8.0-fpm.service ├─34391 php-fpm: master process (/etc/php/8.0/fpm/php-fpm.conf) ├─34407 php-fpm: pool www └─34408 php-fpm: pool www Mar 30 00:50:50 ip-172-31-83-168 systemd[1]: Starting The PHP 8.0 FastCGI Process Manager... Mar 30 00:50:50 ip-172-31-83-168 systemd[1]: Started The PHP 8.0 FastCGI Process Manager.

You can also follow How To Install PHP For Nginx On Ubuntu 20.04 LTS for further details.

Install MySQL Extension

Use the below-mentioned commands to install the MySQL extension for PHP 8. Also, refer to How To Install MySQL 8 on Ubuntu 20.04 LTS.

# Install MySQL Extension
sudo apt install php8.0-mysql

# Enable PDO module
sudo phpenmod pdo_mysql

# Install MySQL Extension - Required for Wordpress Installation
sudo apt install php-mysql

Additional Extensions

You can also install the additional extensions in order to fully support a web application. Some of these packages might not be required in your scenario, hence it's totally optional to install these extensions.

# Install CGI and CLI if not installed by default
sudo apt install php8.0-common php8.0-cli php8.0-cgi

# Install CURL extensions
sudo apt install php8.0-curl

# Install PHP GD and Imagick
sudo apt install php8.0-gd php-imagick

# Multibyte String, Internationalization and Spell Check
sudo apt install php8.0-mbstring php8.0-intl php8.0-pspell

# Multibyte String, Internationalization and Spell Check - Required for Wordpress Installation
sudo apt install php-mbstring php-intl php-pspell

# Emails
sudo apt install php8.0-imap

# SQLite Database
sudo apt install php8.0-sqlite3

# Tidy and XML RPC
sudo apt install php8.0-tidy php8.0-xmlrpc

# Excel
sudo apt install php8.0-xsl

Install OPcache to enable caching at the bytecode level.

# Install OPcache extension
sudo apt install php8.0-opcache

Install the extensions to handle compressed files.

# Install Zip
sudo apt install php8.0-zip

These are the steps required to install PHP 8 on Ubuntu 20.04 LTS using the PPA from Ondřej Surý. We have also installed the additional packages that are mostly required for the website development.

Verify PHP using phpinfo()

We can also verify the PHP and the installed packages by checking the output of the phpinfo method. Now, add the info.php file at the root of your web server and execute it using your preferred browser. It should show the output similar to Fig 1.

# info.php
<?php
echo phpinfo();

Install PHP 8.0 on Ubuntu 20.04 LTS - phpinfo() output

Fig 1

Summary

This tutorial provided all the steps required to install PHP 8 on Ubuntu 20.04 LTS and also enable the extension for the Apache HTTP Server. We have also installed the popular extensions for PHP which are required to host web applications.

Write a Comment
Click the captcha image to get new code.
Discussion Forum by DISQUS