Ubuntu

Install the latest PHP on Ubuntu 22.04

PHP, which is a scripting language, was designed for building websites. This language can be used on many platforms such Linux, Windows, Mac OS, and Unix. The latest version of PHP which is version 8.1 was released on 25th November, 2021 and serves as an extension of PHP version 8.0. This version introduces many features which were not present in the previous one such as enums, read-only properties, never return type, fibers, etc.

Here we have summed up steps using which you can with great ease install the latest version of PHP in Ubuntu 22.04.

Install the latest PHP on Ubuntu 22.04

The guide below teaches you how to install the latest PHP on Ubuntu 22.04.

Step 1: Update the system

To avoid any conflicts during the installation procedure make sure your system is updated. This can be done using this command.

sudo apt update && sudo apt upgrade -y

Output

The system has been updated.

Step 2: Install Dependencies

To install PHP with success you have to install dependencies and to do so run the below-given command. These dependencies might already exist on your system, however, running this command confirms their presence.

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

Output

This makes sure the dependencies are installed.

Step 3: Import PPA Repository of PHP

The next step is to import PPA repository from Ondřej Surý who is a renowned PHP and Debian developer and maintains its packages as well as Ubuntu’s packages.

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

Output

Now update the system once again using the command provided in step 1.

Step 4: Install Apache module/PHP-FPM

There can be two ways to install PHP:

1. Using Apache Module
If you are an Apache HTTP server user and want to install php’s latest version with Apache, then execute the command given below:

sudo apt install php8.1 libapache2-mod-php8.1

Output

After this, you would require to start your Apache server again so that the new PHP module is loaded.

sudo systemctl restart apache2

To evaluate its status.

sudo systemctl status apache2

2. Using Nginx(PHP-FPM)
Run this to install PHP.

sudo apt install php8.1-fpm libapache2-mod-fcgid -y

After this make sure your PHP-FPM is working properly. Use this command to confirm this.

sudo systemctl status php8.1-fpm

Step 5: Verify installation

Lastly, verify the successful installation of PHP on your system using this command. For this blog, we have installed PHP using the Apache module.

php --version

Output

We have installed PHP with great ease.

Step 6: Install Extensions

There are numerous extensions of PHP available so if you desire to install any one of these then you can use the following command.

sudo apt install php8.0-

However, if you want the list of all the extensions then use the following command.

php -m

Output

Chose the extension from the list and install it using the above given command.

Conclusion

In order to install latest PHP on Ubuntu 22.04 you have to import PPA, then install Apache module or PHP-FPM using $ sudo apt install php8.1 libapache2-mod-php8.1, or $ sudo apt install php8.1-fpm libapache2-mod-fcgid commands respectively. Later the successful installation of PHP can be verified using the command $ php –version. Moreover, for the purpose of adding PHP extensions use this command $ php -m.

About the author

Naima Aftab

I am a software engineering professional with a profound interest in writing. I am pursuing technical writing as my full-time career and sharing my knowledge through my words.