PHP 8.1 is a significant update of the PHP language that was “officially” released on November 25, 2021. This is a standard upgrade going forward from the existing PHP 8.0 release. The new PHP 8.1 brings enums, fibers, never return type, final class constants, intersection types, and read-only properties, among new features and changes.
In the following tutorial, you will learn how to import the Ondřej Surý PPA and install PHP 8.1 on your Ubuntu 22.04 LTS Jammy Jellyfish using the command line terminal.
Table of Contents
Update System
Before you begin, make sure your system is up-to-date to avoid any conflicts during the installation of PHP.
sudo apt update && sudo apt upgrade -y
Install Dependencies
The following dependencies will need to be installed to install PHP 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 PPA
To successfully install PHP 8.1, you will need to import the good renowned PPA from Ondřej Surý, the lead developer on PHP and Debian, and maintain Ubuntu and Debian packages.
Use the following command to automatically import the PPA.
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
Next, upgrade any packages that require it.
sudo apt upgrade
Install PHP 8.1 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 as an Apache module, enter the following command.
sudo apt install php8.1 libapache2-mod-php8.1
Once installation is complete, restart your Apache server for the new PHP module to be loaded.
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.1-fpm libapache2-mod-fcgid
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.1-fpm
Lastly, restart Apache.
sudo systemctl restart apache2
Verify that PHP-FPM is working:
systemctl status php8.1-fpm
Example output:
As a reminder to see what version of PHP 8.1 is installed on your system, use the following command.
php --version
Example output:
Install PHP 8.1 with Nginx Option
Nginx does not contain native PHP processing like other web servers like Apache. You will need to install PHP-FPM “fastCGI process manager” to handle the PHP files.
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.1 and PHP 8.1-FPM.
sudo apt install php8.1 php8.1-fpm php8.1-cli
Once installed, the PHP-FPM service is automatically started, and you can check the status to make sure it’s running ok.
systemctl status php8.1-fpm
Example output:
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 for all server blocks that process PHP files that need the location ~ .php$ added.
server {
# … some other code
location ~ .php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.1-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.1 is installed on your system, use the following command.
php --version
Example output:
Optional Extra PHP 8.1 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.1-cli php8.1-curl php8.1-mysqlnd php8.1-gd php8.1-opcache php8.1-zip php8.1-intl php8.1-common php8.1-bcmath php8.1-imap php8.1-imagick php8.1-xmlrpc php8.1-readline php8.1-memcached php8.1-redis php8.1-mbstring php8.1-apcu php8.1-xml php8.1-dom php8.1-redis php8.1-memcached php8.1-memcache
Note, 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:
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.1-dev
Additional developments tool, such as debugging tools, use the following command.
sudo apt install php8.1-xdebug php8.1-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
In the tutorial, you have learned how to install PHP 8.1 and configure how to use it with Apache and Nginx. PHP 8.1 is exciting. However, at the current moment, it is still coming out of beta and not considered stable, such as 8.0 or the old stable 7.4, so beware, you may find that many of your favorite software like WordPress or Plugins/Themes for CMS software may conflict until developers can update.
Do some research, prepare, and have PHP 7.4 or 8.0 installed and ready to replace if anything goes wrong when making the switch. The stable versions such as 8.0 are still actively developed, and packages are pushed simultaneously along with the 8.1 packages.