Install the Latest PHP on Ubuntu 22.04 LTS

PHP is a helpful programming language for creating dynamic and interactive web applications. You must install PHP packages on Ubuntu to run web applications built on PHP. It is open source, flexible, fast, and supports major communication protocols. The latest version of PHP is 8.2, with exciting features like Enums, read-only property, fibers, intersection types, and many more compared to the previous versions.

The latest PHP version installation instructions for Ubuntu 22.04 are provided in this article. We will follow these methods:

Method 1: Installing Latest PHP on Ubuntu 22.04 Using Default Repository

PHP is a popular server-side language for building dynamic web pages. In this section, we will install PHP8.1 on Ubuntu 22.04 using the default repository. For that, the following steps are practiced.

Step 1: Update the Core Libraries

To have an updated version of the installed repositories, run the below command:

$ sudo apt update

Installed core repositories are updated now.

Now, let’s proceed to the next step.

Step 2: Install PHP8.1 on Ubuntu 22.04

PHP can be installed on Ubuntu 22.04 by executing the following command:

$ sudo apt install php

Step 3: Verify PHP Version

To verify the installation and check its installed version, we will run the below command:

$ php --version

The PHP version 8.1.2 is successfully installed on the system.

Remove the Installed PHP

To remove the PHP version installed from the above method, use the command stated below:

$ sudo apt autoremove php

Method 2: Installing Latest PHP on Ubuntu 22.04 Using PPA

In this section, we will install PHP8.2 on Ubuntu 22.04 using a PPA repository, “ONDREJ”, step-by-step.

Step 1: Update the Core Libraries

It’s better to update the installed packages to have the updated versions before the installation; for that, run the following command:

$ sudo apt update

Let’s start installing the latest PHP version on Ubuntu.

Step 2: Install Required Dependencies

First, install the required dependencies, which helps to proceed with the latest PHP installation smoothly:

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

Required dependencies have been installed, and we can add the PPA repository now.

Step 3: Add PPA Repository

The PPA repository named ondrej PPA offers the latest version of PHP. To add it on Ubuntu 22.04, use the command:

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

Now, the required dependencies are installed, and the PPA repository is added. It is recommended to update the core libraries before proceeding with the installation:

$ sudo apt update -y

Installed packages are updated now.

Step 4: Install Latest PHP

When writing this article, PHP8.2 is the latest version. To install PHP8.2 in Ubuntu 22.04 through ondrej, execute the command:

$ sudo apt install php8.2 -y

Step 5: Verify PHP Version

Installed PHP version can be verified using the command:

$ php -version

Output shows that PHP 8.2.0 is installed.

Step 6 (Optional): Install PHP 8.2 Extensions

Several extensions are available that increase the functionalities; install any extension and add those features to the installed PHP. The general syntax to install the PHP8.2 extension is provided below:

$ sudo apt install php8.2-[extension]

Using the above syntax, the following command installs two extensions named mbstring and mysql.

$ sudo apt install php8.2-mbstring php8.2-mysql -y

Both extensions are installed, and we can use their features now.

Step 7(Optional): PHP Loaded Modules

PHP modules increase the code reusability and use the pre-built code by installing the module. To check the loaded modules in PHP8.2, use the command:

php -m

The output will show the name of the modules loaded in PHP.

Remove PHP 8.2

We can free up the space by uninstalling the PHP from our system using the command:

$ sudo apt remove php8.2 -y

The above command has removed PHP8.2 installed from the PPA repository.

Note: To remove Ondrej PPA, we have to run the below command:

$ sudo add-apt-repository --remove ppa:ondrej/php

That’s it from this guide!

Conclusion

Current latest version of PHP is 8.2, which is available by adding the PPA repository and then using the command “sudo apt install php8.2”. However, the latest available version in the default repository of Ubuntu is PHP-8.1 which can be installed with “sudo apt install php8.1”. This post has provided both methods to install the latest PHP on Ubuntu 22.04.