Help Docs Server Administration Linux Server Administration Upgrading or Installing PHP on Ubuntu 22.04

Upgrading or Installing PHP on Ubuntu 22.04

Upgrade PHP on Ubuntu 22.04 for better security & performance. This guide covers installation, Apache configuration, and managing PHP modules.

Introduction

Older apps might still rely on older PHP versions like 8.1 or 8.2, but upgrading to a newer PHP version will ensure better software and website performance, fewer compatibility issues with plugins, and continued security. Older PHP versions can leave your websites and server vulnerable to malicious attacks. This guide walks you through installing PHP 8.3 on Ubuntu 22.04, configuring Apache, and managing any modules your setup requires.

Upgrading to newer versions of PHP, like PHP 8.3 can unlock performance upgrades like OPcache. This stores precompiled scripts in memory so pages load more quickly. Whether you’re developing locally or running a live site on a Ubuntu Cloud VPS or dedicated server, using the most up-to-date versions of PHP will keep your applications running reliably.

Prerequisites

  • A server with Ubuntu 22.04.
  • Root or sudo access.

Check for currently installed PHP versions

  1. Before running any installation commands, check to see if the PHP version you want is currently installed:
update-alternatives --list php

Save a list of existing PHP modules

If an older PHP version is currently installed, your new application will require the reinstallation of modules. This step helps compare existing modules after the upgrade.

  1. Run the following command to capture a list of active modules for a version of PHP. Replace [version] with the PHP version you are currently using. 
/usr/bin/php[version] -m > /tmp/php_[version]_modules.txt

PHP 8.3 example
The following is an example where we export the names of our PHP 8.3 modules into a file located in /tmp/ named ‘php_8.3_modules.txt :
/usr/bin/php8.3 -m > /tmp/php_8.3_modules.txt

Installing new versions of PHP 

Use of a 3rd party repository
While Ubuntu 22.04 does not include the latest PHP versions by default, we are adding the official repository maintained by Ondřej Surý on Launchpad.

  1. Add the PHP repository. Run the following commands, pressing ‘Enter‘ when prompted to confirm adding the repository:
apt-get install python-software-propertiessudo add-apt-repository ppa:ondrej/php

  1. Update your system’s package list.
sudo apt update

  1. Replace [version] with the specific version number in the following command to install your desired PHP version. The system will display the installed packages and the required disk space. Type Y when prompted.
sudo apt install php[version]

PHP 8.3 example
For example, to install PHP 8.3, the command would be:
sudo apt install php8.3

  1. If desired, you can set your server’s default PHP version with the following command, replacing [version] with the installation path to the PHP version you want as the default:
sudo update-alternatives --set php /usr/bin/php[version]

PHP 8.3 example
In this example, we set PHP 8.3 as our system default by passing the installation path “/usr/bin/php8.3”
sudo update-alternatives --set php /usr/bin/php8.3

  1. The following command will show all PHP versions installed on your system, and can be used to check if the installation was successful:
update-alternatives --list php

  1. If you adjusted the system version, check if the version switch was successful:
php -v

Successful output for PHP 8.3
Example output if PHP 8.3 is active:
PHP 8.3.2 (cli) (built: Aug 20 2025 10:00:00) (NTS)

Adding modules to PHP

Not all modules from previous PHP versions are enabled by default. The following describes how to check for currently installed modules, add new modules, or remove any unneeded modules. 

List currently installed modules

  1. List the currently installed and enabled PHP modules on Ubuntu, with the command:
php -m

  1. If you have multiple versions of PHP installed, list all currently installed modules for a specific PHP version by calling its binary directly:
phpX.Y -m 

PHP 8.3 example
For example, to list modules for PHP 8.3, we would run:
php8.3 -m

Check for modules available for download

  1. List all PHP modules available for download by checking the apt-cache, replacing [version] with the PHP version you are using.
apt-cache search php[version]-

PHP 8.3 example
For example, to check which PHP 8.3 modules are available for installation, we could run:
apt-cache search php8.3-

  1. Install a module by replacing both the [version] and the [module] in the apt-get command:
sudo apt-get install php[version]-[module]

Installing curl on PHP 8.3 example
To install curl on PHP 8.3, we would run:
sudo apt install php8.3-curl

Install multiple modules at once
You can install multiple modules by listing them in a single command.

Example for PHP 8.3:

sudo apt-get install php8.3-memcached php8.3-opcache php8.3-mbstring

Compare modules between PHP versions

  1. If you upgraded PHP, create a module list for the upgraded version to compare with the old one. Replace [version] with your PHP version:
php -m > /tmp/php[version].modules.txt

PHP 8.3 example
php -m > /tmp/php83.modules.txt

  1. Compare modules with earlier PHP versions:
diff /tmp/php_[old_version].modules.txt /tmp/php_[new_version].modules.txt

Example comparing PHP 8.2 to 8.3
diff /tmp/php82.modules.txt /tmp/php83.modules.txt

Visual comparison
Run the following for a visual comparison:
vimdiff /tmp/php[old_version].modules.txt /tmp/php[new_version].modules.txt

Example for PHP 8.2 vs 8.3:
vimdiff /tmp/php82.modules.txt /tmp/php83.modules.txt

Installing Modules

Ubuntu 22.04 uses commands to manage Apache modules, determining the PHP version Apache loads. 

  1. Use the apt install command to install missing modules. Replace [version] with the PHP version, and [module] with the desired PHP module.
sudo apt install php[version]-[module]

PHP 8.3 example
sudo apt install php8.3-mysql php8.3-xml php8.3-curl

  1. Check available PHP modules for Apache:
ls /etc/apache2/mods-available/php*

Example output
/etc/apache2/mods-available/php8.2.conf
/etc/apache2/mods-available/php8.2.load
/etc/apache2/mods-available/php8.3.conf
/etc/apache2/mods-available/php8.3.load

  1. Check the currently active PHP module with the following command that shows all PHP versions installed and available for Apache.
ls /etc/apache2/mods-enabled/php*

Example output
/etc/apache2/mods-enabled/php8.2.conf
/etc/apache2/mods-enabled/php8.2.load

  1. Disable the older PHP module to switch to a newer PHP version.
sudo a2dismod php8.2

  1. Enable the desired PHP module(s). The output will show which modules were enabled.
sudo a2enmod [version]

PHP 8.3 example
sudo a2enmod php8.3

  1. Verify Apache configuration with the following command. You want to see “Syntax OK“:
apachectl -t

  1. Restart Apache to apply changes.
sudo service apache2 restart

  1. Check the list of enabled PHP modules.
sudo ls /etc/apache2/mods-enabled/php*

Example output for PHP 8.3
/etc/apache2/mods-enabled/php8.3.conf
/etc/apache2/mods-enabled/php8.3.load

  1. Check the PHP version from the command line:
php -v

Double check post-installation
Once you’ve installed any missing modules, repeat the process described in the “Compare modules between PHP versions” section to generate an updated list of installed modules.

You can also create a phpinfo page to check if your website is actively using a certain PHP version. Visit our phpinfo guide for more information.

Troubleshooting & FAQs

There is a good chance the older version is still enabled.

  1. Restart Apache using sudo systemctl restart apache2.
  2. Ensure you disabled the old module with a2dismod.
  3. Run sudo a2enmod php8.x again for the desired version.

Missing modules are the most common error after upgrading.

  1. Check logs in /var/log/apache2/error.log
  2. Some required PHP modules may be missing, so install them with:
    sudo apt install php[version]-mysql php[version]-xml php[version]-curl [other-modules]
  3. Compare your backed-up module list with the new installation to identify any gaps.

Missing modules are the most common error after upgrading.

  1. PHP CLI and Apache can run different PHP versions.
  2. Ensure you updated the Apache module with the version you are needing, and restart apache.
    Example: a2enmod php8.3.

Next steps

You have now installed and configured a newer version of PHP on Ubuntu 22.04. Use php -v to confirm the version running on your system. Ensure all the PHP modules your applications need are installed and working correctly by testing the functions of your website. Keep track of PHP versions and keep your server updated regularly to help maintain security and performance.

If you need Ubuntu hosting, Liquid Web provides dedicated servers, cloud servers, and VPS solutions. Their Linux support team can offer guidance and hands-on help with PHP and server setup.

Was this article helpful?