Sunday, December 15, 2024
Google search engine
HomeGuest BlogsHow To Install PHP 8.2 on Ubuntu 22.04|20.04|18.04

How To Install PHP 8.2 on Ubuntu 22.04|20.04|18.04

.tdi_3.td-a-rec{text-align:center}.tdi_3 .td-element-style{z-index:-1}.tdi_3.td-a-rec-img{text-align:left}.tdi_3.td-a-rec-img img{margin:0 auto 0 0}@media(max-width:767px){.tdi_3.td-a-rec-img{text-align:center}}

PHP 8.2 is a version of the PHP programming language released near the end of year 2022. This is a major release of PHP that contains many new features, including readonly classes, null, false, and true as stand-alone types, deprecated dynamic properties, performance improvements and more. PHP 8.2 also includes bug fixes and performance improvements over previous versions like 8.1. We recommend you test your codebase with PHP 8.2 before upgrading in a production setup, just to ensure that everything works as expected.

In this article we shall cover steps that are used in the installation of PHP 8.2 on Ubuntu 22.04|20.04|18.04. The default version of PHP available on OS repositories is usually older than PHP official latest releases. PPA (Personal Package Archive) software repositories for PHP allows you to install newer releases of PHP on your Ubuntu system that are not available in the official repositories of a Linux distribution.

Debian installation article: How To Install PHP 8.2 on Debian

.tdi_2.td-a-rec{text-align:center}.tdi_2 .td-element-style{z-index:-1}.tdi_2.td-a-rec-img{text-align:left}.tdi_2.td-a-rec-img img{margin:0 auto 0 0}@media(max-width:767px){.tdi_2.td-a-rec-img{text-align:center}}

Step 1: Update Ubuntu System

Check for updates and install them before performing any other action.

sudo apt update && sudo apt -y upgrade

Clean APT cache after the upgrade.

sudo apt autoremove

Check if a reboot is required and act accordingly.

[ -f /var/run/reboot-required ] && sudo reboot -f

OS release details can be checked using the commands given here.

$ cat /etc/os-release
PRETTY_NAME="Ubuntu 22.04.1 LTS"
NAME="Ubuntu"
VERSION_ID="22.04"
VERSION="22.04.1 LTS (Jammy Jellyfish)"
VERSION_CODENAME=jammy
ID=ubuntu
ID_LIKE=debian
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
UBUNTU_CODENAME=jammy

Step 2: Add Surý PHP APT repository

To get PHP 8.2 packages installed on Ubuntu we’ll use Ondřej Surý’s PHP PPA which provides the latest stable versions of PHP for Ubuntu and Debian systems.

For Ubuntu 18.04 this repository doesn’t contain PHP 8.2 packages. Checkout our guide on building from source:

Install few dependency packages before adding the repo.

sudo apt update
sudo apt install -y lsb-release gnupg2 ca-certificates apt-transport-https software-properties-common

Run the following commands in your terminal to add Surý PPA to your system.

sudo add-apt-repository ppa:ondrej/php

Expected successful command execution.

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.
Press [ENTER] to continue or Ctrl-c to cancel.
Adding deb entry to /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
Hit:1 https://mirror.hetzner.com/ubuntu/packages jammy InRelease
Hit:2 https://mirror.hetzner.com/ubuntu/packages jammy-updates InRelease
Hit:3 https://mirror.hetzner.com/ubuntu/packages jammy-backports InRelease
Hit:4 https://mirror.hetzner.com/ubuntu/security jammy-security InRelease
Get:5 https://ppa.launchpadcontent.net/ondrej/php/ubuntu jammy InRelease [23.9 kB]
Get:6 https://ppa.launchpadcontent.net/ondrej/php/ubuntu jammy/main amd64 Packages [108 kB]
Get:7 https://ppa.launchpadcontent.net/ondrej/php/ubuntu jammy/main Translation-en [33.3 kB]
Fetched 166 kB in 1s (120 kB/s)
Reading package lists... Done

You can manually confirm if the repository is working by running the apt update command.

$ sudo apt update
Hit:1 https://mirror.hetzner.com/ubuntu/packages jammy InRelease
Hit:2 https://mirror.hetzner.com/ubuntu/packages jammy-updates InRelease
Hit:3 https://mirror.hetzner.com/ubuntu/packages jammy-backports InRelease
Hit:4 https://mirror.hetzner.com/ubuntu/security jammy-security InRelease
Hit:5 https://ppa.launchpadcontent.net/ondrej/php/ubuntu jammy InRelease
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done

Step 3: Install PHP 8.2 on Ubuntu

Once the PPA is added, use the apt command to install PHP 8.2 and any other associated PHP modules on Ubuntu server.

sudo apt install php8.2

Accept package installation as requested via prompt.

The following additional packages will be installed:
  apache2 apache2-bin apache2-data apache2-utils bzip2 file libapache2-mod-php8.2 libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap liblua5.3-0 mailcap mime-support php-common
  php8.2-cli php8.2-common php8.2-opcache php8.2-readline ssl-cert
Suggested packages:
  apache2-doc apache2-suexec-pristine | apache2-suexec-custom www-browser bzip2-doc php-pear
The following NEW packages will be installed:
  apache2 apache2-bin apache2-data apache2-utils bzip2 file libapache2-mod-php8.2 libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap liblua5.3-0 mailcap mime-support php-common php8.2
  php8.2-cli php8.2-common php8.2-opcache php8.2-readline ssl-cert
0 upgraded, 21 newly installed, 0 to remove and 2 not upgraded.
Need to get 6,990 kB of archives.
After this operation, 29.8 MB of additional disk space will be used.
Do you want to continue? [Y/n] y

To check the version of PHP currently installed on the system run the following command:

$ php -v
PHP 8.2.7 (cli) (built: Jun  8 2023 15:27:40) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.7, Copyright (c) Zend Technologies
    with Zend OPcache v8.2.7, Copyright (c), by Zend Technologies

You can also get the details of PHP version currently active and being used by your system with the command:

php --version

Step 4: Install PHP 8.2 modules

PHP modules are software libraries created to add functionality to PHP. These modules are usually written in C and you can dynamically load into PHP or compile as shared objects.

If you want to install PHP 8.2 modules on Ubuntu system, use apt package manager with the command syntax given here:

sudo apt install -y php8.2-<module-name>

Where:

  • <module-name> is replaced with the actual name of the module to be installed.

In this example we install PHP 8.2 fpm module from PPA added earlier.

$ sudo apt install php8.2-fpm
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Suggested packages:
  php-pear
The following NEW packages will be installed:
  php8.2-fpm
0 upgraded, 1 newly installed, 0 to remove and 2 not upgraded.
Need to get 1,886 kB of archives.
After this operation, 5,756 kB of additional disk space will be used.
Get:1 https://ppa.launchpadcontent.net/ondrej/php/ubuntu jammy/main amd64 php8.2-fpm amd64 8.2.1-2+ubuntu22.04.1+deb.sury.org+1 [1,886 kB]
Fetched 1,886 kB in 0s (5,211 kB/s)
Selecting previously unselected package php8.2-fpm.
(Reading database ... 56412 files and directories currently installed.)
Preparing to unpack .../php8.2-fpm_8.2.1-2+ubuntu22.04.1+deb.sury.org+1_amd64.deb ...
Unpacking php8.2-fpm (8.2.1-2+ubuntu22.04.1+deb.sury.org+1) ...
Setting up php8.2-fpm (8.2.1-2+ubuntu22.04.1+deb.sury.org+1) ...
....

Installation of commonly used modules.

sudo apt install php8.2-{bcmath,fpm,xml,mysql,zip,intl,ldap,gd,cli,bz2,curl,mbstring,pgsql,opcache,soap,cgi}

Installed modules can be listed with the command:

$ php -m
[PHP Modules]
bcmath
bz2
calendar
Core
ctype
curl
date
dom
exif
FFI
fileinfo
filter
ftp
gd
gettext
hash
iconv
intl
json
ldap
libxml
mbstring
mysqli
mysqlnd
openssl
pcntl
pcre
PDO
pdo_mysql
pdo_pgsql
pgsql
Phar
posix
readline
Reflection
session
shmop
SimpleXML
soap
sockets
sodium
SPL
standard
sysvmsg
sysvsem
sysvshm
tokenizer
xml
xmlreader
xmlwriter
xsl
Zend OPcache
zip
zlib

[Zend Modules]
Zend OPcache

Step 5: Using PHP with Nginx / Apache

We will consider both Nginx and Apache web server configurations to host your PHP application.

Hosting PHP application with Nginx web server

With Nginx, PHP code is typically executed by a separate process, such as PHP-FPM (FastCGI Process Manager). PHP-FPM is a daemon that listens for incoming PHP requests and runs them in a separate process. Nginx just forwards incoming requests to PHP-FPM where they are executed.

Install Nginx web server and FPM extension.

sudo apt install nginx php8.2-fpm

Open your Nginx server configuration and add the following block inside the http block to configure PHP requests to be forwarded for execution by PHP-FPM:

$ sudo vim /etc/nginx/nginx.conf
server {
    listen 80;
    server_name mysite.example.com;
    root /var/www/mysite;
    index index.php index.html;

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
    }
}

Validate Nginx configuration is ok.

$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Hosting PHP application with Apache web server

In Apache we can use a built-in module called mod_php to process PHP code directly within the Apache process.

But first you need to install Apache web server package, PHP, and Apache PHP extension:

sudo apt install apache2 libapache2-mod-php8.2 

Enable mod_php module:

sudo a2enmod php8.2

Restart Apache web server after the configuration.

sudo systemctl restart apache2

Wrapping up

In conclusion, the installation of PHP 8.2 on Ubuntu 22.04/20.04/18.04 Linux system is a straightforward process. We started with the addition of the Ondřej Surý’s PHP PPA to the system’s package sources, which enabled us to install PHP 8.2 and other modules. Adding third-party PPA to production server can introduce stability and security risks, and they should always be used with caution. If performing PHP upgrade, consider backing up important files before updating your system, just in case something goes wrong during the update process.

Recommended Web Development books to read:

.tdi_4.td-a-rec{text-align:center}.tdi_4 .td-element-style{z-index:-1}.tdi_4.td-a-rec-img{text-align:left}.tdi_4.td-a-rec-img img{margin:0 auto 0 0}@media(max-width:767px){.tdi_4.td-a-rec-img{text-align:center}}

RELATED ARTICLES

Most Popular

Recent Comments