Upgrading or installing PHP on Ubuntu — PHP 7.2 and Ubuntu 22.04
In addition to better security, the main reason users upgrade or install PHP 7.2 on Ubuntu 22.04 is the significant performance increase over previous versions when it is installed and using the OPcache module. This combination greatly decreases the time it takes for your web page to load. If you are developing a site locally or launching it on one of Liquid Web's Ubuntu VPS or Dedicated Servers, having installed the PHP 7.2 version or newer is the way to go.
Newer PHP versions have come out since then, and it is highly recommended to use the most recent versions because of software improvements and security updates. Still, there are some who use legacy software for one reason or another. However, older PHP versions can leave open security holes on a server if they are not replaced. Therefore, this guide will cover how to install PHP 7.2 on Ubuntu 22.04.
Prerequisites
Correct permissions
First, ensure you have proper root or sudo permissionsto be able to manage the system and applications on the server. If you only have access to a non-root user on the system, then you may have to get more permissions from the server admin.
Existing PHP versions
Check to see if PHP is already installed. PHP 7.0 is the primary option if you choose the default LAMP stack when installing the Ubuntu image on the server for the first time. It's possible that the server doesn't have any version of PHP installed if you used a different image or performed a basic install. Access the terminal and run the below command to check the PHP version:
user@test:~# php -v
If you see an older PHP version, then you will have to install the new version and switch the version Apache connects to subsequently. If the "Command php not found" message is given, then no PHP version is currently installed.
Creating a quick backup
If you do have an existing PHP version installed already on your system, you may want to make a backup of any configurations before doing the upgrade in case there are issues (like if your site code isn't compatible) and you need to downgrade again. You can check on the current configuration directory by running following command:
user@test:~# php --ini
Configuration File (php.ini) Path: /etc/php/7.0/cli
Loaded Configuration File: /etc/php/7.0/cli/php.ini
Scan for additional .ini files in: /etc/php/7.0/cli/conf.d
This command shows the configurations are all in the /etc/php/7.0/ directory. So, if you want to make a quick backup, you can run a command similar to the following one:
user@test:~# sudo cp -a /etc/php/7.0/ /etc/php/7.0.backup
Also, you can save a list of existing PHP modules by running:
user@test:~# sudo cp -a /etc/php/7.0/ /etc/php/7.0.backup
This step can help when comparing the existing modules to the modules in PHP 7.2, as demonstrated later in this article.
How to install PHP 7.2 on Ubuntu 22.04
Step #1. Adding the official PHP repository on Ubuntu
In order to run PHP 7.2, we must use an external repository and establish the dependencies required for installing PHP 7.2 in Ubuntu 22.04. The official repository for PHP on Ubuntu is from Ondřej Surý on launchpad. You may use the given command to include it in your system:
apt-get install python-software-properties
sudo add-apt-repository ppa:ondrej/php
Press the Enter key when the system prompts you to. This allows your system to use the repository as a source for new software:

Step #2. Performing the PHP installation on Ubuntu
Now let's proceed with the installation of PHP 7.2 on Ubuntu 22.04 using this command:
apt-get update
sudo apt-get install php7.2
The output is as follows:

It should show several different packages that it will install and how much total disk space they will use. When it prompts you, type Y and submit it. After a few moments the install of PHP on Ubuntu should be completed.
Step #3. Configuring the operating system (OS) to use the new PHP version
The above steps will install the PHP 7.2 on Ubuntu 22.02 but not make the operating system use it by default. To accomplish this, enter the following command into the terminal:
update-alternatives --set php /usr/bin/php7.2
It should display various packages that it will set up along with their estimated combined disc space requirements. When prompted, enter Y and submit the form. The installation of PHP 7.2 on Ubuntu should be finished shortly.
Step #4. Verifying the PHP version
You can now check the PHP version installed using the php -v command:
user@test:~$ php -v
PHP 7.2.34-38+ubuntu22.04.1+deb.sury.org+1 (cli) (built: Feb 14 2023 18:28:40) (NTS)
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.2.34-38+ubuntu22.04.1+deb.sury.org+1, Copyright (c), by Zend Technologies
It should now display PHP 7.2, but we must now set up Apache to use the more recent PHP 7.2 version that was just installed.
Configuring Apache to use the PHP 7.2 version
Ubuntu 22.04 uses a few different commands to help manage Apache modules, so it utilizes a specific PHP version depending on which module is loaded. You can view the list of available modules by running the following command:
user@test:~# ls /etc/apache2/mods-available/php*
/etc/apache2/mods-available/php7.0.conf
/etc/apache2/mods-available/php7.0.load
/etc/apache2/mods-available/php7.2.conf
/etc/apache2/mods-available/php7.2.load
You may have other modules as well. The default LAMP stack install would have PHP 7.0 and the new PHP 7.2 install we just made, but running the next command shows that PHP 7.0 is still active:
user@test:~# ls /etc/apache2/mods-enabled/php*
/etc/apache2/mods-enabled/php7.0.conf
/etc/apache2/mods-enabled/php7.0.load
To switch the active version so that it is the newer PHP 7.2 version, first, turn off the older PHP 7.0 version using the following command:
user@test:~# sudo a2dismod php7.0
Then enable PHP 7.2 using this command:
user@test:~# sudo a2enmod php7.2
The output will be similar to the following output:

Before restarting Apache, check the Apache configuration syntax by running this command:
user@test:~# apachectl -t
Syntax OK
If it says the syntax is OK, then restart Apache:
user@test:~# sudo service apache2 restart
Verifying Apache's PHP version
Now, to verify your work, rerun the previously used command below to confirm the installed and enabled PHP module. It should show the PHP 7.2 version:
user@test:~# ls /etc/apache2/mods-enabled/php*
/etc/apache2/mods-enabled/php7.2.conf
/etc/apache2/mods-enabled/php7.2.load
If you see different results or errors in any output, check the syntax of commands and make sure the sudo command was used if you are not running as the root user.
Then, to test it on your site, we recommend making a phpinfo file you can view on your domain. This shows the PHP version as well as information about current configurations, variables, and all the added modules. Create a new .php file in your site's home folder or in Apache’s document root (/var/www/html by default) and include this code:
<?php phpinfo(); ?>
Then, once you visit the page in your browser, the result will be as shown in the image below:

If your website was previously up and running with a prior version of PHP, check to see if there are any issues now. You can try to diagnose further if your code is incompatible or displaying errors. Also, you can investigate if you need to restart Apache right away. If necessary, you can downgrade the PHP version once more by using the a2dismod and a2enmod commands to turn off PHP 7.2 and then turn on PHP 7.0 or whatever previous version was installed on the server.
You may also check Apache’s PHP version in various other versions of Ubuntu like Lubuntu, Kubuntu, or Xubuntu using one of the following commands or by uploading an info.php page:
php -version
php -v
If you are building a new site and have a list of modules that are required, the next section can assist you to add additional modules. Some sites may now display an error stating that a module is missing and cannot operate.
Adding modules to PHP 7.2
Not every module from earlier PHP versions is automatically carried over to PHP 7.2. Type the command below first to install any necessary basic modules if you don't need to compare the list to an earlier PHP version. Instead of pressing the Enter key, hold down the Tab key twice to bring up a list of 7.2-specific modules that are available:
user@test:~# sudo apt-get install php7.2[tab][tab]
An example of the output is shown below:
php7.2 php7.2-enchant php7.2-mbstring php7.2-snmp
php7.2-bcmath php7.2-fpm php7.2-mysql php7.2-soap
php7.2-bz2 php7.2-gd php7.2-odbc php7.2-sqlite3
php7.2-cgi php7.2-gmp php7.2-opcache php7.2-sybase
php7.2-cli php7.2-imap php7.2-pgsql php7.2-tidy
php7.2-common php7.2-interbase php7.2-phpdbg php7.2-xml
php7.2-curl php7.2-intl php7.2-pspell php7.2-xmlrpc
php7.2-dba php7.2-json php7.2-readline php7.2-xsl
php7.2-dev php7.2-ldap php7.2-recode php7.2-zip
You might need to look up instructions on installing additional modules since this is not a comprehensive list. You can add numerous modules with the same install command by typing the modules you wish, as in show below:
user@test:~# sudo apt-get install php7.2-opcache php7.2-mbstring php-memcached
If you used the command earilier in this guide to save the list of modules from a prior version of PHP, you can save a list of the modules in PHP 7.2 and compare them to the older version. To do so, run the following command:
php -m > /tmp/7.2.modulelist.txt
Then, you can run the below command to compare it to the list that was created before:
user@test:~# diff /tmp/7.0.modulelist.txt /tmp/7.2.modulelist.txt
15,16d14
< mysqli
< mysqlnd
21d18
< pdo_mysql
28d24
< soap
29a26
> sodium
If you want a graphical way to compare the two lists, then run this command:
user@test:~# vimdiff /tmp/7.0.modulelist.txt /tmp/7.2.modulelist.txt
A page like this will appear in the terminal:

The various MySQL modules and soap show up on the list of 7.0 modules on the left but are missing from the list of 7.2 modules on the right. Also of note, the sodium module is on PHP 7.2 but wasn’t in PHP 7.0. So for this example, we can install the missing modules with the following command:
user@test:~# sudo apt-get install php7.2-mysql php7.2-soap
After those missing modules are successfully installed, we can save an updated list of modules and can compare them again using the following command:
user@test:~# php -m > /tmp/7.2.modulelist.updated.txt
diff /tmp/7.0.modulelist.txt /tmp/7.2.modulelist.updated.txt
29a30
> sodium
Only the sodium module differs in the two module lists you are comparing in this example, so your setup is now ready for development. Hopefully, this guide helped with your upgrade to PHP 7.2 — or your new install of PHP on Ubuntu. Please let our Linux Support Team know if you have any issues or need further help with your Ubuntu server on Liquid Web hosting.
Conclusion
This guide demonstrated how to set up and install PHP 7.2 on Ubuntu 22.04 on Linux, macOS, Windows, and other supported operating systems. The php -v command can be used to check PHP versions. You should be able to learn everything you need to know about your PHP installation on Ubuntu by using the methods listed above.
Related Articles:

About the Author: Sapta Upendran
Sapta is a passionate Linux system engineer, a voracious reader, a dexterous cook, and a wanderlust. She is also interested in technical and non-technical writing.
Our Sales and Support teams are available 24 hours by phone or e-mail to assist.
Latest Articles
CentOS Linux 7 EOL — everything you need to know
Read ArticleHow to install Node.js on Linux (AlmaLinux)
Read ArticleUpgrading or installing PHP on Ubuntu — PHP 7.2 and Ubuntu 22.04
Read ArticleWhy is your IP blocked?
Read ArticleLiquid Web Terraform Provider Deploying WordPress
Read Article