Upgrading or Installing PHP on Ubuntu 22.04
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
- Before running any installation commands, check to see if the PHP version you want is currently installed:
update-alternatives --list phpSave 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.
- 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/usr/bin/php8.3 -m > /tmp/php_8.3_modules.txtInstalling new versions of PHP
- 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- Update your system’s package list.
sudo apt update- 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]sudo apt install php8.3- 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]sudo update-alternatives --set php /usr/bin/php8.3- 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- If you adjusted the system version, check if the version switch was successful:
php -vPHP 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
- List the currently installed and enabled PHP modules on Ubuntu, with the command:
php -m- 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 php8.3 -mCheck for modules available for download
- 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]-apt-cache search php8.3-- Install a module by replacing both the [version] and the [module] in the apt-get command:
sudo apt-get install php[version]-[module]sudo apt install php8.3-curlExample for PHP 8.3:
sudo apt-get install php8.3-memcached php8.3-opcache php8.3-mbstringCompare modules between PHP versions
- 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.txtphp -m > /tmp/php83.modules.txt- Compare modules with earlier PHP versions:
diff /tmp/php_[old_version].modules.txt /tmp/php_[new_version].modules.txtdiff /tmp/php82.modules.txt /tmp/php83.modules.txtvimdiff /tmp/php[old_version].modules.txt /tmp/php[new_version].modules.txtvimdiff /tmp/php82.modules.txt /tmp/php83.modules.txtInstalling Modules
Ubuntu 22.04 uses commands to manage Apache modules, determining the PHP version Apache loads.
- 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]sudo apt install php8.3-mysql php8.3-xml php8.3-curl- Check available PHP modules for Apache:
ls /etc/apache2/mods-available/php*/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- 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*/etc/apache2/mods-enabled/php8.2.conf
/etc/apache2/mods-enabled/php8.2.load- Disable the older PHP module to switch to a newer PHP version.
sudo a2dismod php8.2- Enable the desired PHP module(s). The output will show which modules were enabled.
sudo a2enmod [version]sudo a2enmod php8.3- Verify Apache configuration with the following command. You want to see “Syntax OK“:
apachectl -t- Restart Apache to apply changes.
sudo service apache2 restart- Check the list of enabled PHP modules.
sudo ls /etc/apache2/mods-enabled/php*/etc/apache2/mods-enabled/php8.3.conf
/etc/apache2/mods-enabled/php8.3.load- Check the PHP version from the command line:
php -vYou 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
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.