How To Install PHP 8.1 On Ubuntu 22.04 LTS

In this guide we will learn how to install PHP on Ubuntu 22.04 Linux operating system. PHP is known scripting language, which was designed for building websites. PHP language can be used on many platforms such Linux, Windows, Mac OS, and Unix. On this tutorial, we will use the latest version of PHP (version 8.1). This version serves as an extension of PHP version 8.0. PHP version 8.1 presenet with many features which were not present in the previous one such as enums, read-only properties, never return type, fibers, etc.

Installing PHP 8.1 On Ubuntu 22.04 LTS

In this tutorial we will use the Ondrej PPA for installing PHP on Ubuntu 22.04 LTS system, this tutorial is also compatible with Ubuntu 20.04 LTS, and 18.04 LTS.

Prerequisite

As mentioned above, we will install the latest version of PHP (version 8.1.3, at least at the time this tutorial was written). Before we start to install PHP 8.1 on Ubuntu 22.o4 LTS operating system, there are several prerequisites that must be met so that the installation process can be running smoothly. We will need to prepare any requirements as mention below :

1. an Ubuntu 22.04 server system

ramans@infodiginet:~$ hostnamectl
Static hostname: infodiginet
Icon name: computer-vm
Chassis: vm
Machine ID: c05bdac4b09048309a26b6024adee484
Boot ID: ba10ca2fa4c348bc816932582a0222ad
Virtualization: vmware
Operating System: Ubuntu 22.04.1 LTS
Kernel: Linux 6.0.0-060000-generic
Architecture: x86-64

2. non-root user with sudo privileges

3. sufficient disk and good internet access

PHP 8.1 Installation Process

To install PHP on Ubuntu systems, we only need to add the required PPA and we can install any PHP version. The PHP 8.1 installation process will be explained in this tutorial are as follows :

  1. Update System
  2. Install dependency packages
  3. Add Ondrej PPA
  4. Install PHP
  5. Switch Default PHP version

Explanation of each step will be mentioned in the following sub-chapters.

1. Update System

The first step to be taken in PHP installation is to update local packages repository. This task will cut down the installation time and it also helps prevent zero-day exploits against outdated software, for this purpose we will submit the command line :

$ sudo apt update
$ sudo apt upgrade

 

2. Install Dependencies Packages

To install PHP, we need a dependencies packages on our system, for this purpose we will submit following command line :

$ sudo apt install software-properties-common apt-transport-https -y

Output :

ramans@infodiginet:~$ sudo apt install software-properties-common apt-transport-https -y
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
apt-transport-https is already the newest version (2.4.8).
The following additional packages will be installed:
python3-software-properties software-properties-gtk
The following packages will be upgraded:
python3-software-properties software-properties-common software-properties-gtk
3 upgraded, 0 newly installed, 0 to remove and 54 not upgraded.
Need to get 108 kB of archives.
After this operation, 0 B of additional disk space will be used.
Get:1 http://id.archive.ubuntu.com/ubuntu jammy-updates/main amd64 software-properties-common all 0.99.22.4 [14,1 kB]
Get:2 http://id.archive.ubuntu.com/ubuntu jammy-updates/main amd64 software-properties-gtk all 0.99.22.4 [65,2 kB] 
Get:3 http://id.archive.ubuntu.com/ubuntu jammy-updates/main amd64 python3-software-properties all 0.99.22.4 [28,8 kB] 
Fetched 108 kB in 25s (4.329 B/s) 
(Reading database ... 305092 files and directories currently installed.)
Preparing to unpack .../software-properties-common_0.99.22.4_all.deb ...
Unpacking software-properties-common (0.99.22.4) over (0.99.22.3) ...
Preparing to unpack .../software-properties-gtk_0.99.22.4_all.deb ...
Unpacking software-properties-gtk (0.99.22.4) over (0.99.22.3) ...
Preparing to unpack .../python3-software-properties_0.99.22.4_all.deb ...
Unpacking python3-software-properties (0.99.22.4) over (0.99.22.3) ...
Setting up python3-software-properties (0.99.22.4) ...
Setting up software-properties-common (0.99.22.4) ...
Setting up software-properties-gtk (0.99.22.4) ...
Processing triggers for dbus (1.12.20-2ubuntu4.1) ...
Processing triggers for shared-mime-info (2.1-2) ...
Processing triggers for mailcap (3.70+nmu1ubuntu1) ...
Processing triggers for desktop-file-utils (0.26-1ubuntu3) ...
Processing triggers for hicolor-icon-theme (0.17-2) ...
Processing triggers for gnome-menus (3.36.0-1ubuntu3) ...
Processing triggers for libglib2.0-0:amd64 (2.72.4-0ubuntu1) ...
Processing triggers for man-db (2.10.2-1) ...

 

3.  Add the Ondrej PPA

In this step we will add Ondrej PPA which contains all versions of PHP packages for the Ubuntu systems. This task will be performed by submitting ommand line :

$ sudo add-apt-repository ppa:ondrej/php -y

Output :

ramans@infodiginet:~$ sudo add-apt-repository ppa:ondrej/php -y
PPA publishes dbgsym, you may need to include 'main/debug' component
Repository: 'deb https://ppa.launchpadcontent.net/ondrej/php/ubuntu/ jammy main'
Description:
Co-installable PHP versions: PHP 5.6, PHP 7.x and most requested extensions are included. Only Supported Versions of PHP (http://php.net/supported-versions.php) for Supported Ubuntu Releases (https://wiki.ubuntu.com/Releases) are provided. Don't ask for end-of-life PHP versions or Ubuntu release, they won't be provided.

Debian oldstable and stable packages are provided as well: https://deb.sury.org/#debian-dpa

You can get more information about the packages at https://deb.sury.org

IMPORTANT: The <foo>-backports is now required on older Ubuntu releases.

BUGS&FEATURES: This PPA now has a issue tracker:
https://deb.sury.org/#bug-reporting

CAVEATS:
1. If you are using php-gearman, you need to add ppa:ondrej/pkg-gearman
2. If you are using apache2, you are advised to add ppa:ondrej/apache2
3. If you are using nginx, you are advised to add ppa:ondrej/nginx-mainline
   or ppa:ondrej/nginx

PLEASE READ: If you like my work and want to give me a little motivation, please consider donating regularly: https://donate.sury.org/

WARNING: add-apt-repository is broken with non-UTF-8 locales, see
https://github.com/oerdnj/deb.sury.org/issues/56 for workaround:

# LC_ALL=C.UTF-8 add-apt-repository ppa:ondrej/php
More info: https://launchpad.net/~ondrej/+archive/ubuntu/php
Adding repository.
Found existing deb entry in /etc/apt/sources.list.d/ondrej-ubuntu-php-jammy.list
Adding deb entry to /etc/apt/sources.list.d/ondrej-ubuntu-php-jammy.list
Found existing deb-src entry in /etc/apt/sources.list.d/ondrej-ubuntu-php-jammy.list
Adding disabled deb-src entry to /etc/apt/sources.list.d/ondrej-ubuntu-php-jammy.list
Adding key to /etc/apt/trusted.gpg.d/ondrej-ubuntu-php.gpg with fingerprint 14AA40EC0831756756D7F66C4F4EA0AAE5267A6C
Get:2 https://apt.grafana.com stable InRelease [5.984 B] 
Err:2 https://apt.grafana.com stable InRelease 
The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 9E439B102CF3C0C6
Hit:3 http://id.archive.ubuntu.com/ubuntu jammy InRelease 
Ign:4 http://download.webmin.com/download/repository sarge InRelease 
Hit:5 http://apt.postgresql.org/pub/repos/apt jammy-pgdg InRelease 
Hit:6 http://id.archive.ubuntu.com/ubuntu jammy-updates InRelease 
Hit:7 http://download.webmin.com/download/repository sarge Release 
Ign:8 https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/6.0 InRelease 
Hit:9 http://id.archive.ubuntu.com/ubuntu jammy-backports InRelease 
Hit:10 https://repos.influxdata.com/ubuntu jammy InRelease 
Hit:11 https://ppa.launchpadcontent.net/deadsnakes/ppa/ubuntu jammy InRelease 
Hit:13 https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/6.0 Release 
Hit:1 https://downloads.apache.org/cassandra/debian 40x InRelease 
Get:15 http://security.ubuntu.com/ubuntu jammy-security InRelease [110 kB] 
Hit:16 https://ppa.launchpadcontent.net/kicad/kicad-6.0-releases/ubuntu jammy InRelease
Hit:17 https://ppa.launchpadcontent.net/ondrej/php/ubuntu jammy InRelease 
Fetched 110 kB in 3s (33,7 kB/s)
Reading package lists... Done

 

4. Installing PHP

In this step we will install PHP on Ubuntu 22.04 system, before it we will update our Ubuntu 22.04 system by submitting command line :

$ sudo apt update

4.1. Install PHP 8.1

In this step, we will install PHP on our system. In this tutorial we will install PHP version 8.1, so we will submit the following command line :

$ sudo apt -y install php8.1

Output :

ramans@infodiginet:~$ sudo apt -y install php8.1
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
libapache2-mod-php8.1 php8.1-cli php8.1-common php8.1-opcache php8.1-readline
Suggested packages:
php-pear
The following NEW packages will be installed:
libapache2-mod-php8.1 php8.1 php8.1-cli php8.1-common php8.1-opcache php8.1-readline
0 upgraded, 6 newly installed, 0 to remove and 58 not upgraded.
Need to get 4.741 kB of archives.
After this operation, 20,9 MB of additional disk space will be used.
Get:1 https://ppa.launchpadcontent.net/ondrej/php/ubuntu jammy/main amd64 php8.1-common amd64 8.1.14-1+ubuntu22.04.1+deb.sury.org+1 [711 kB]
Get:2 https://ppa.launchpadcontent.net/ondrej/php/ubuntu jammy/main amd64 php8.1-opcache amd64 8.1.14-1+ubuntu22.04.1+deb.sury.org+1 [370 kB] 
Get:3 https://ppa.launchpadcontent.net/ondrej/php/ubuntu jammy/main amd64 php8.1-readline amd64 8.1.14-1+ubuntu22.04.1+deb.sury.org+1 [13,5 kB]
Get:4 https://ppa.launchpadcontent.net/ondrej/php/ubuntu jammy/main amd64 php8.1-cli amd64 8.1.14-1+ubuntu22.04.1+deb.sury.org+1 [1.841 kB] 
Get:5 https://ppa.launchpadcontent.net/ondrej/php/ubuntu jammy/main amd64 libapache2-mod-php8.1 amd64 8.1.14-1+ubuntu22.04.1+deb.sury.org+1 [1.775 kB]
Get:6 https://ppa.launchpadcontent.net/ondrej/php/ubuntu jammy/main amd64 php8.1 all 8.1.14-1+ubuntu22.04.1+deb.sury.org+1 [30,3 kB] 
Fetched 4.741 kB in 48s (99,8 kB/s) 
Selecting previously unselected package php8.1-common.
(Reading database ... 305232 files and directories currently installed.)
. . .
Creating config file /etc/php/8.1/apache2/php.ini with new version
libapache2-mod-php8.1: php7.4 module already enabled, not enabling PHP 8.1
Setting up php8.1 (8.1.14-1+ubuntu22.04.1+deb.sury.org+1) ...
Processing triggers for man-db (2.10.2-1) ...
Processing triggers for php8.1-cli (8.1.14-1+ubuntu22.04.1+deb.sury.org+1) ...
Processing triggers for libapache2-mod-php8.1 (8.1.14-1+ubuntu22.04.1+deb.sury.org+1) ...

Then we will verify by querying its version, by submitting command line :

$ php --version

Output :

ramans@infodiginet:~$ php --version
PHP 8.1.14 (cli) (built: Jan 6 2023 15:23:18) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.14, Copyright (c) Zend Technologies
with Zend OPcache v8.1.14, Copyright (c), by Zend Technologies

4.2. Install PHP 7.4

PHP version 7.4 is still widely used and is the most popular among other PHP 7 versions. To install PHP 7.4, we use the command line:

$ sudo apt -y install php7.4

Output :

ramans@infodiginet:~$ sudo apt -y 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 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 php-common php7.4 php7.4-cli php7.4-common php7.4-json php7.4-opcache php7.4-readline
0 upgraded, 8 newly installed, 0 to remove and 58 not upgraded.
Need to get 4.110 kB of archives.
After this operation, 17,7 MB of additional disk space will be used.
Get:1 https://ppa.launchpadcontent.net/ondrej/php/ubuntu jammy/main amd64 php-common all 2:93+ubuntu22.04.1+deb.sury.org+2 [16,6 kB]
Get:2 https://ppa.launchpadcontent.net/ondrej/php/ubuntu jammy/main amd64 php7.4-common amd64 1:7.4.33-2+ubuntu22.04.1+deb.sury.org+1 [692 kB]
Get:3 https://ppa.launchpadcontent.net/ondrej/php/ubuntu jammy/main amd64 php7.4-json amd64 1:7.4.33-2+ubuntu22.04.1+deb.sury.org+1 [20,6 kB] 
Get:4 https://ppa.launchpadcontent.net/ondrej/php/ubuntu jammy/main amd64 php7.4-opcache amd64 1:7.4.33-2+ubuntu22.04.1+deb.sury.org+1 [220 kB]
Get:5 https://ppa.launchpadcontent.net/ondrej/php/ubuntu jammy/main amd64 php7.4-readline amd64 1:7.4.33-2+ubuntu22.04.1+deb.sury.org+1 [13,3 kB]
Get:6 https://ppa.launchpadcontent.net/ondrej/php/ubuntu jammy/main amd64 php7.4-cli amd64 1:7.4.33-2+ubuntu22.04.1+deb.sury.org+1 [1.579 kB]
Get:7 https://ppa.launchpadcontent.net/ondrej/php/ubuntu jammy/main amd64 libapache2-mod-php7.4 amd64 1:7.4.33-2+ubuntu22.04.1+deb.sury.org+1 [1.518 kB]
Get:8 https://ppa.launchpadcontent.net/ondrej/php/ubuntu jammy/main amd64 php7.4 all 1:7.4.33-2+ubuntu22.04.1+deb.sury.org+1 [51,1 kB]

Output :

Setting up php7.4 (1:7.4.33-2+ubuntu22.04.1+deb.sury.org+1) ...
Processing triggers for man-db (2.10.2-1) ...
Processing triggers for php7.4-cli (1:7.4.33-2+ubuntu22.04.1+deb.sury.org+1) ...
Processing triggers for libapache2-mod-php7.4 (1:7.4.33-2+ubuntu22.04.1+deb.sury.org+1) ...

Then we will verify it by submitting command line :

$ php --version

Output :

ramans@infodiginet:~$ php --version
PHP 7.4.33 (cli) (built: Jan 6 2023 16:10:54) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
with Zend OPcache v7.4.33, Copyright (c), by Zend Technologies

5. Switch Default PHP Version

In this step we will try to switch PHP version for CLI and Apache. We will show you how to switch among PHP version by using command line :

$ sudo update-alternatives --config php

Output :

ramans@infodiginet:~$ sudo update-alternatives --config php
There are 2 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.4 74 manual mode
2 /usr/bin/php8.1 81 manual mode

Press <enter> to keep the current choice[*], or type selection number: 1
update-alternatives: using /usr/bin/php7.4 to provide /usr/bin/php (php) in manual mode
ramans@infodiginet:~$ sudo update-alternatives --config php
There are 2 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.4 74 manual mode
2 /usr/bin/php8.1 81 manual mode

Conclusion

In this tutorial we have learned about the instructions to install PHP on Ubuntu 22.04. Using of Ondrej PPA allows us to quickly install PHP on Ubuntu systems install multiple PHP versions on a single system. We can switch between PHP version as default anytime with the update-alternative utility.

(Visited 229 times, 1 visits today)

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *