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

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

Table of Contents

Rasmus Lerdorf, a Danish-Canadian programmer, invented PHP, a popular computer language for scripting and web development, in 1994. As long as the PHP runtime environment is installed, a PHP script can run on Linux, Windows, and macOS. November 25, 2021 is the official release date of PHP 8.1. Numerous new features, enhancements, and deprecations from the previous version (PHP 8.0) are included in this release. We go over how to install PHP 8.1 on Ubuntu 22.04, 20.04, and 18.04 in this article. There are two versions of the Ubuntu operating system: Desktop and Server.

Key Features of PHP 8.1

  • Readonly Properties: Class properties can be marked as readonly, meaning they can only be written once.
  • DNS-over-HTTPS (DoH) Support: Enhanced security for DNS queries.
  • AVIF Image Format Support: The PHP 8.1’s image processing and GD extension adds support for AVIF images.
  • Never Return Type: A new return type hint called never is added.
  • Fibers: A low-level mechanism to manage parallelism.
  • array_is_list: A built-in function to check if an array is a list.
  • File Uploads with CURLStringFile: PHP Curl extension now supports HTTP(S) requests with file uploads.
  • Enums: Enumerations are now added.
  • New Functionsfdatasync() and fsync() for file synchronization, as well as new Sodium XChaCha20 functions.
  • Pure Intersection Types: Enhanced type handling.
  • Explicit Octal Numeral Notation: Improved numeral notation.
  • Define Final Class Constants: Enhanced class constant definition.
  • MurmurHash3 Hash Algorithm Support: New hash algorithm support.
  • xxHash Hash Algorithms Support: Additional hash algorithms supported.

For detailed information, read through the official PHP 8.1 notes.

Step 1: Update Ubuntu System

Let’s begin our installation of PHP 8.1 on Ubuntu 22.04|20.04|18.04 by ensuring the system has been updated and upgraded..

sudo apt update && sudo apt -y upgrade

After a successful OS upgrade, I recommend you reboot the system:

sudo systemctl reboot

Step 2: Add Ondřej Surý PPA repository

Ubuntu 22.04: PHP 8.1 packages and its extensions are available in the OS upstream repositories. The Sury repository is not needed.

Ubuntu 20.04: PHP 8.1 binary packages are available in the Ondřej Surý PPA repository. This repo has to be added manually on the system.

Ubuntu 18.04: The PPA repository is not available for this OS. Consider installing it from source.

Add Ondřej Surý PPA repository:

sudo apt update
sudo apt install lsb-release ca-certificates apt-transport-https software-properties-common -y
sudo add-apt-repository ppa:ondrej/php

The only input from you is to hit <Enter key> and add the repository:

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-Key>

Pull the latest packages list from configured sources on your Ubuntu system:

$ sudo apt update

Step 3: Install PHP 8.1

We should now be able to install PHP 8.1 on Ubuntu 22.04|20.04|18.04 Linux machine. The commands to run are as shared below:

sudo apt install php8.1

Hit the y key to start installation.

Check for the current active version of PHP with the following command:

php --version

Step 4: Install PHP 8.1 Extensions

The command to install PHP 8.1 extensions on Ubuntu 22.04|20.04|18.04 is:

sudo apt install php8.1-<extension>

Where <extension> is to be replaced with the name of the PHP extension to be installed. For example, mysql, zip, xml, etc.

Some available extensions are as shown below:

sudo apt install php8.1-<TAB>

Example installing commonly used PHP extensions:

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

To list all PHP 8.1 loaded modules, run the following command:

php --modules

If you are using PHP with Nginx, ensure the php-fpm service is started and running:

systemctl status php*-fpm.service

The PHP-FPM default configuration file to set listening socket, user, and other information are located in:

ls -1 /etc/php/8.1/fpm/

Edit the configuration files if necessary:

sudo vim /etc/php/8.1/fpm/pool.d/www.conf
sudo vim /etc/php/8.1/fpm/php-fpm.conf

We were able to install PHP 8.1 on the Ubuntu 22.04|20.04|18.04 Desktop/Server operating system with this instruction. With enhanced security, increased speed, and a plethora of new capabilities that enable developers to construct apps with ease and passion, PHP 8.1 appears to be a promising release overall. We hope you found this information to be very helpful. Please leave a remark if you need any help.

PHP 8.1 Installation on Other Systems

For instructions on installing PHP 8.1 on other systems, such as CentOS, Debian, or Windows, please refer to the respective documentation or contact a system administrator.

Concussion

In conclusion, installing PHP 8.1 on Ubuntu 22.04|20.04|18.04 is a straightforward process that can be accomplished by following the steps outlined above. The new features and enhancements in PHP 8.1 make it a compelling choice for developers looking to build modern, efficient web applications. By ensuring your system is up to date and utilizing the appropriate repositories, you can seamlessly integrate PHP 8.1 into your development environment.

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 *