How To Install PHP 8.1 on Ubuntu 22.04

Smit Pipaliya
TechvBlogs
Published in
4 min readJun 27, 2022

Jun 18, 2022, Originally published at techvblogs.com ・4 min read

PHP is a popular server scripting language known for creating dynamic and interactive web pages. Getting up and running with your language of choice is the first step in learning to program.

PHP is a general-purpose scripting language suitable for web development. Most websites out there are programmed using PHP language because it is:

  • Fast — PHP is fast because it runs on its own memory space. Also, PHP has a Zend engine that parses PHP code and turns it into opcodes which are then interpreted.
  • Flexible — PHP is flexible because it allows for almost all databases. MySQL is the de-facto database to connect to because it is open source.
  • Free and open source
  • PHP is forgiving — Why do we say PHP is forgiving? It is forgiving meaning its learning is not so hard and therefore suitable for almost all programmers starting out.
  • PHP supports major communication protocols i.e LDAP, IMAP, POP3.

This tutorial will guide you through installing PHP 8.1 on Ubuntu and setting up a local programming environment via the command line. You will also install a dependency manager, Composer, and test your installation by running a script.

PHP 8.1 is the latest PHP version released in 2021. In this guide, you are going to learn how to install the latest PHP version which is currently 8.1 on your Ubuntu 22.04 system or server, and configure it with Apache and Nginx. You will also learn how to upgrade your PHP version to the latest.

Below is a summarised list of the new key features witnessed in the PHP 8.1 release:

  • Readonly properties — Class properties can be marked as read-only hence can only be written once
  • Support for DNS-over-HTTPS (DoH)
  • AVIF Image Format support — PHP 8.1’s image processing and GD extension add support for AVIF images.
  • Never Return Type — A new return type hint called never is added in PHP 8.1
  • Added support for Fibers — The low-level mechanism to manage parallelism
  • PHP 8.1 adds array_is_list as a built-in function
  • File Uploads with CURLStringFile — In PHP 8.1, the PHP Curl extension now supports HTTP(S) requests with file uploads
  • Enums — They are now added in PHP 8.1
  • New fdatasync() and fsync() Functions
  • New Sodium XChaCha20 functions
  • The addition of pure intersection types feature
  • Explicit Octal Numeral Notation
  • Define final Class Constants
  • MurmurHash3 hash algorithm support
  • Intersection Types
  • xxHash hash algorithms support

This tutorial will guide you through installing PHP 8.1 on Ubuntu and setting up a local programming environment via the command line.

Installing PHP 8.1 on Ubuntu 22.04

1. Run system updates

The first thing to do in a new system is to update our repositories in order to make them up to date. Run upgrade command also.

sudo apt update && apt upgrade -y

2. Add Ondrej sury PPA repository

To run PHP 8.1 on Ubuntu 22.04, we need to add Ondrej sury PPA into our system. This is the maintainer of the PHP repository at the moment. This PPA is not currently checked so installing from it will not be guaranteed 100% results.

To add this PPA use the following command on our terminal.

sudo add-apt-repository ppa:ondrej/php

After installation is complete we need to update the repositories again for the changes to take effect.

sudo apt update

3. Install PHP 8.1 on Ubuntu 22.04

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

sudo apt install php8.1 -y

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

php --version

4. Install PHP 8.1 Extensions

Besides PHP itself, you will likely want to install some additional PHP modules. You can use this command to install additional modules, replacing PACKAGE_NAME with the package you wish to install:

sudo apt-get install php8.1-PACKAGE_NAME

You can also install more than one package at a time. Here are a few suggestions of the most common modules you will most likely want to install:

sudo apt-get install -y php8.1-cli php8.1-common php8.1-mysql php8.1-zip php8.1-gd php8.1-mbstring php8.1-curl php8.1-xml php8.1-bcmath

This command will install the following modules:

  • php8.1-cli - command interpreter, useful for testing PHP scripts from a shell or performing general shell scripting tasks
  • php8.1-common - documentation, examples, and common modules for PHP
  • php8.1-mysql - for working with MySQL databases
  • php8.1-zip - for working with compressed files
  • php8.1-gd - for working with images
  • php8.1-mbstring - used to manage non-ASCII strings
  • php8.1-curl - lets you make HTTP requests in PHP
  • php8.1-xml - for working with XML data
  • php8.1-bcmath - used when working with precision floats

PHP configurations related to Apache are stored in /etc/php/8.1/apache2/php.ini. You can list all loaded PHP modules with the following command:

php -m

You have installed PHP and verified the version you have running. You also installed any required PHP modules and were able to list the modules that you have loaded.

You could start using PHP right now, but you will likely want to use various libraries to build PHP applications quickly. Before you test your PHP environment, first set up a dependency manager for your projects.

Thank you for reading this blog.

--

--

Smit Pipaliya
TechvBlogs

I am Project Manager at ServerAvatar Cloud Technology.