HomeHOWTOSHow 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

Today we will learn how to install PHP 8.2 on Ubuntu 22.04|20.04|18.04. PHP is an abbreviation of Hypertext Pre-processor. It is an open-source general scripting language used as a backend programming language for Web applications. In addition to that, it can be used alongside HTML when creating web applications. What separates PHP from other languages such as JavaScript is that for PHP the codes are executed on the server, and HTML scripts are generated and sent to the client.

PHP 8.2 was released in November 2022. This version has many nifty features such as read-only classes, null, false, and true as stand-alone types, deprecated dynamic properties, performance improvements and much more. It also has several bug fixes and performance enhancements over the previous releases.

The features of PHP 8.2 are:

  • Fibres – a new feature that allows for lightweight user-land threads that can be scheduled cooperatively by the developer.
  • First-class callables for object methods – allowing object methods to be passed around as callables, similar to closures.
  • Improved error messages and debugging information – including better error messages for type errors and warnings for invalid parameter types.
  • More consistent and improved handling of named arguments – including disallowing passing the same named argument multiple times and enforcing the order of named arguments.
  • Improved syntax for enums – including specifying a custom value for each enum constant.

Today we will learn How to Install PHP 8.2 on Ubuntu 22.04|20.04|18.04.

Step 1: Add the PHP 8.2 Surý repository on Ubuntu 22.04|20.04|18.04

The latest PHP packages can be easily installed on Ubuntu 22.04|20.04|18.04 from the  Ondřej Surý’s PHP repository.

First, update the APT index:

sudo apt update

Install the required packages:

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

Now add the repository to Ubuntu 22.04|20.04|18.04:

sudo add-apt-repository ppa:ondrej/php

Proceed as shown:

 Co-installable PHP versions: PHP 5.6, PHP 7.x, PHP 8.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
Press [ENTER] to continue or Ctrl-c to cancel adding it. Press Enter to Proceed

Step 2: Install PHP 8.2 on Ubuntu 22.04|20.04|18.04

Once the PPA has been added, we can easily install PHP 8.2 on Ubuntu 22.04|20.04|18.04 using the command:

sudo apt install php8.2

Dependency tree:

Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following package was automatically installed and is no longer required:
  gir1.2-goa-1.0
Use 'sudo apt autoremove' to remove it.
The following additional packages will be installed:
  apache2 apache2-bin apache2-data apache2-utils libapache2-mod-php8.2 libapr1 libaprutil1
  libaprutil1-dbd-sqlite3 libaprutil1-ldap liblua5.2-0 libpcre2-8-0 php-common php8.2-cli
  php8.2-common php8.2-opcache php8.2-readline
Suggested packages:
  apache2-doc apache2-suexec-pristine | apache2-suexec-custom php-pear
The following NEW packages will be installed:
  apache2 apache2-bin apache2-data apache2-utils libapache2-mod-php8.2 libapr1 libaprutil1
  libaprutil1-dbd-sqlite3 libaprutil1-ldap liblua5.2-0 php-common php8.2 php8.2-cli
  php8.2-common php8.2-opcache php8.2-readline
The following packages will be upgraded:
  libpcre2-8-0
1 upgraded, 16 newly installed, 0 to remove and 365 not upgraded.
Need to get 6,446 kB of archives.
After this operation, 29.2 MB of additional disk space will be used.
Do you want to continue? [Y/n] y

Once installed, check the PHP version:

$ php -v
PHP 8.2.4 (cli) (built: Mar 16 2023 14:34:24) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.4, Copyright (c) Zend Technologies
    with Zend OPcache v8.2.4, Copyright (c), by Zend Technologies

Step 3: Install PHP 8.2 Extensions on Ubuntu 22.04|20.04|18.04

For more functionality, we need to install libraries for PHP. The modules, written in C language can be dynamically loaded into PHP or compiled and shared.

The modules can be installed with an APT command with the below syntax:

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

You need to replace module-name with the exact module to be installed. For example:

sudo apt install php8.2-cli

You can make the command neater when installing several modules at once. For example:

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

To check the installed PHP extensions, use 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
random
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

Verdict

Now you are set to use the installed PHP as desired. If you found any troubles with this setup, let us know in the comments.

Read more about PHP.

See more:

Install and Use Docker Compose on Linux Systems

Install LAMP Stack on Ubuntu 20.04

How To Install Google Chrome on Ubuntu 22.04|20.04

Klinsmann Öteyo
Klinsmann Öteyo
Self proclaimed Geek
- Advertisment -

Recent posts

LEAVE A REPLY

Please enter your comment!
Please enter your name here