How to Upgrade PHP Versions on Ubuntu

Updated: January 19, 2024 By: Khue Post a comment

PHP is a general-purpose scripting language that is especially suited for web development. It can be embedded into HTML and run on a web server. PHP has many features and extensions that make it powerful and flexible for creating dynamic web pages.

You may want to upgrade PHP on Ubuntu for various reasons, such as:

  • To get the latest features and improvements of PHP, such as new syntax, functions, classes, and constants.
  • To fix bugs and security issues that may exist in older versions of PHP.
  • To improve the performance and compatibility of your PHP applications with other software and libraries.

Prerequisites

To upgrade PHP on Ubuntu, you will need:

  • An Ubuntu server with root access or a user with sudo privileges. 
  • A terminal or an SSH client to access your server.
  • A backup of your data and configuration files in case something goes wrong during the upgrade process.

The Steps

Step 1: Check the current version of PHP

Before upgrading PHP, you should check the current version of PHP that is installed on your server. You can do this by running the following command in your terminal:

php -v

Output (may vary):

PHP 7.4.3 (cli) (built: Feb 23 2020 07:24:02) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.3, Copyright (c), by Zend Technologies

Step 2: Add the Ondřej Surý’s PHP PPA

We will need to add a third-party repository that provides the latest versions of PHP for Ubuntu. One of the most popular and reliable repositories is Ondřej Surý’s PHP PPA. This repository contains packages for PHP 8.1, PHP 8.0, and PHP 7.4.

To add this repository to your system, perform the command below:

sudo add-apt-repository ppa:ondrej/php

You will be asked to confirm the addition by pressing ENTER.

Step 3: Update the package lists

After adding the repository, you need to update the package lists on your system to get the latest information about the available packages from the repository. You can get the job done by executing the following command:

sudo apt update

This will fetch the package lists from all the repositories that are enabled on your system, including Ondřej Surý’s PHP PPA.

Step 4: Remove the old version of PHP (optional)

If you want to remove the old version of PHP from your system before installing the new one, you can do this by running the following command in your terminal:

sudo apt remove php7.4

Don’t forget to replace php7.4 with the version of PHP that you want to remove.

This will uninstall the old version of PHP and its dependencies from your system.

Note: This step is optional and not required for upgrading PHP on Ubuntu. However, it may help you save some disk space and avoid potential conflicts between different versions of PHP.

Step 5: Install the new version of PHP

Now that you have added the repository and updated the package lists, you can install the new version of PHP on your system. You can choose from PHP 8.1, PHP 8.0, or PHP 7.4, depending on your preference and compatibility with your applications. In this tutorial, I’ll go with PHP 8.1.

To install the new version of PHP on your server, perform the command below:

sudo apt install php8.1

This will install the new version of PHP and its dependencies on your system.

Step 6: Install additional PHP modules

Depending on your needs and applications, you may want to install some additional PHP modules that provide extra functionality and features for PHP. For example, you may want to install the mysqli module that allows you to interact with MySQL databases or the curl module that allows you to make HTTP requests.

To install additional PHP modules, run the command below:

sudo apt install php8.1-mysqli php8.1-curl php8.1-xml php8.1-mbstring

Replace php8.1 with the version of PHP that you have installed in step 5, and add or remove the modules that you want to install or uninstall.

Step 7: Restart the web server


After installing the new version of PHP and the additional modules, you need to restart the web server that is running on your system to apply the changes and load the new version of PHP. The most common web servers for Ubuntu are Apache and Nginx.

To restart the Apache web server, execute the following command:

sudo systemctl restart apache2

To restart the Nginx web server, run this one:

sudo systemctl restart nginx

Conclusion

In this tutorial, we have learned how to upgrade PHP on Ubuntu with a step-by-step guide. We have also learned what PHP is, why we may want to upgrade it, and what are the prerequisites for this task.

By upgrading PHP on Ubuntu, we can enjoy the latest features and improvements of PHP, fix bugs and security issues, and improve the performance and compatibility of our PHP applications.