Install PHP 7.1/7.2/7.3/7.4 on Ubuntu 22.04

|
Last Updated:
|
|

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 php
php:
  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 Packages

Install 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/null

Run System Update

Re-synchronize your system packages to the latest versions;

apt update

Install 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.4
Reading 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-EXTENSION

Where EXTENSION is the PHP module you want to install. For example;

apt-cache search php7.4-mysql

If the module is available, then install it using the command;

apt install php7.4-EXTENSION

For example;

apt install php7.4-mysql

Install 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.3

To install PHP 7.3 modules, run the command;

apt install php7.3-EXTENSION

Install PHP 7.2 on Ubuntu 22.04

To install PHP 7.2 on Ubuntu 22.04 by executing the command;

apt install php7.2

To install PHP 7.2 modules, run the command;

apt install php7.2-EXTENSION

Install 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.1

To install PHP 7.1 modules, run the command;

apt install php7.1-EXTENSION

Set 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 php

Sample 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.4

Checking the version;

php -v
PHP 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 Technologies

And that is it on how to you can install PHP 7.x on Ubuntu 22.04.

Other Tutorials

Install PHP 8 on Ubuntu 22.04

Install PHP 8 on Debian 11

Install MySQL 8 on Debian 11

Install MariaDB 10.6 on Debian 11

SUPPORT US VIA A VIRTUAL CUP OF COFFEE

We're passionate about sharing our knowledge and experiences with you through our blog. If you appreciate our efforts, consider buying us a virtual coffee. Your support keeps us motivated and enables us to continually improve, ensuring that we can provide you with the best content possible. Thank you for being a coffee-fueled champion of our work!

Photo of author
koromicha
I am the Co-founder of Kifarunix.com, Linux and the whole FOSS enthusiast, Linux System Admin and a Blue Teamer who loves to share technological tips and hacks with others as a way of sharing knowledge as: "In vain have you acquired knowledge if you have not imparted it to others".

2 thoughts on “Install PHP 7.1/7.2/7.3/7.4 on Ubuntu 22.04”

  1. 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

    Reply
    • With Ubuntu 22.04 officially released, the Ondrej PPA repos should work just fine. Please check the guide again. It has been updated. thanks

      Reply

Leave a Comment