In this tutorial, you will learn how to install PHP 7.1/7.2/7.3/7.4 on Ubuntu 22.04. PHP is a fast, flexible, pragmatic, popular general-purpose scripting language that is especially suited to web development.
Installing PHP 7.x on Ubuntu 22.04
The default Ubuntu 22.04 repositories provide PHP 8.x;
apt-cache policy phpphp:
Installed: (none)
Candidate: 2:8.0+82~0build1
Version table:
2:8.0+82~0build1 500
500 http://us.archive.ubuntu.com/ubuntu jammy/main amd64 Packages
500 http://us.archive.ubuntu.com/ubuntu jammy/main i386 PackagesInstall SURY APT Repository
To install PHP, you need to install SURY, third party repositories that provides PHP packages.
add-apt-repository ppa:ondrej/php --yes &> /dev/nullRun System Update
Re-synchronize your system packages to the latest versions;
apt updateInstall PHP 7.4 on Ubuntu 22.04
Once the SURY repos are in place, you should now be able to install PHP 7.4 on Ubuntu 22.04 by running the command below;
apt install php7.4Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
libapache2-mod-php7.4 libffi7 libpcre2-8-0 php-common php7.4-cli
php7.4-common php7.4-json php7.4-opcache php7.4-readline
Suggested packages:
php-pear
The following NEW packages will be installed:
libapache2-mod-php7.4 libffi7 php7.4 php7.4-cli php7.4-common php7.4-json
php7.4-opcache php7.4-readline
The following packages will be upgraded:
libpcre2-8-0 php-common
2 upgraded, 8 newly installed, 0 to remove and 736 not upgraded.
Need to get 4,365 kB of archives.
After this operation, 18.0 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Install PHP 7.4 Modules on Ubuntu 22.04
By default, quite a number of PHP 7.4 modules are already enabled.
php7.4 -m[PHP Modules]
calendar
Core
ctype
date
exif
FFI
fileinfo
filter
ftp
gettext
hash
iconv
json
libxml
openssl
pcntl
pcre
PDO
Phar
posix
readline
Reflection
session
shmop
sockets
sodium
SPL
standard
sysvmsg
sysvsem
sysvshm
tokenizer
Zend OPcache
zlib
[Zend Modules]
Zend OPcache
To install additional modules, first check if the modules is provided by the default repositories;
apt-cache search php7.4-EXTENSIONWhere EXTENSION is the PHP module you want to install. For example;
apt-cache search php7.4-mysqlIf the module is available, then install it using the command;
apt install php7.4-EXTENSIONFor example;
apt install php7.4-mysqlInstall PHP 7.3 on Ubuntu 22.04
Once the repository is in place, install PHP 7.3 on Ubuntu 22.04 by executing the command;
apt install php7.3To install PHP 7.3 modules, run the command;
apt install php7.3-EXTENSIONInstall PHP 7.2 on Ubuntu 22.04
To install PHP 7.2 on Ubuntu 22.04 by executing the command;
apt install php7.2To install PHP 7.2 modules, run the command;
apt install php7.2-EXTENSIONInstall PHP 7.1 on Ubuntu 22.04
Once the repository is in place, install PHP 7.1 on Ubuntu 22.04 by executing the command;
apt install php7.1To install PHP 7.1 modules, run the command;
apt install php7.1-EXTENSIONSet default PHP version on Ubuntu 22.04
If you have multiple versions of PHP installed on Ubuntu 22.04, you can choose to make one of the versions as the default PHP version.
To list the available versions;
update-alternatives --config phpSample command output;
There are 4 choices for the alternative php (providing /usr/bin/php).
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/bin/php8.1 81 auto mode
1 /usr/bin/php7.2 72 manual mode
2 /usr/bin/php7.3 73 manual mode
3 /usr/bin/php7.4 74 manual mode
4 /usr/bin/php8.1 81 manual mode
Press to keep the current choice[*], or type selection number:
As you can see, we have PHP 8.1 as the default version of PHP.
To change the default version, simply enter the number that matched the appropriate version you want to make as the default and press ENTER.
For example, to make PHP 7.4 as the default version, simply type 3 and press ENTER.
You can simply execute the command below to change the version straight away;
update-alternatives --set php /usr/bin/php7.4Checking the version;
php -vPHP 7.4.27 (cli) (built: Dec 20 2021 21:28:15) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
with Zend OPcache v7.4.27, Copyright (c), by Zend TechnologiesAnd that is it on how to you can install PHP 7.x on Ubuntu 22.04.


not able to install php7.4 on ubuntu 22.04.
libapache2-mod-php7.4 can not be installed. It seems their are some dependency issues.
LG
With Ubuntu 22.04 officially released, the Ondrej PPA repos should work just fine. Please check the guide again. It has been updated. thanks