How to install PHP 8.1 on Ubuntu 21.04

In this tutorial, I am going to show you how to install the current stable release version of PHP 8.1 on Ubuntu 21.04.

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 Zend engine that parses PHP code and turns it into opcodes which are then interpreted.
  • Flexible – PHP is flexible because it allows to almost all databases. MySQL being 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 it is not so hard therefore suitable to almost all programmers starting out.
  • PHP supports major communication protocols i.e LDAP, IMAP, POP3.

Prerequisites for Installing PHP 8.1

  • Before you can use PHP you must have web server installed. Apache web server is the most widely used with PHP.
  • Database must be there for PHP to run. It works well on all databases such Oracle, MySQL, PostgreSQL.
  • PHP parser must be there. What is PHP parser if you can ask? PHP Parser is a library that takes a source code written in PHP, passes it through a lexical analyzer and creates its respective syntax tree.

PHP 8.1 is a major update release version. It contains many new features and optimizations e.g

  • It introduced Enumerations feature. Enumerations or Enum feature allow a developer to define a custom type that is limited to one of the discrete number of possible values. This is helpful while defining domain model.
  • Improvement of RFC readonly property version 2. This prevents modification of the property after initialization.
  • Introduction of RFC fibers call action. This seeks to eliminate synchronous and asychronous function call by allowing functions to become interruptible without affecting the entire fuction.
  • Introduction of a non return type. This indicates that a function either exits, throws an exception or doesn’t terminate.
  • Introduction of first class collable actions.
  • Introduction of fsync and fdatasync functions
  • Introduction of array_is_list functions
  • Introduction of explicit octal numeral notation

Related Articles

Table of contents

  1. Run system update
  2. Add Ondrej sury PPA repository
  3. Install PHP 8.1

Installing PHP 8.1 on Ubuntu 21.04

1. Run system updates

First, let’s run system updates on our system with the following command.

$ sudo apt update && apt upgrade -y
$ reboot -n

When updates are complete you can probably reboot your system for the changes to take effect, then proceed to the next step.

2. Add Ondrej sury PPA repository

To start installing and using the software package from PPA, you need to tell Ubuntu where to find the PPA. The contents of PPA are not checked or monitored.

To install this package, run the following command from your terminal.

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

Sample output will look like this:

PPA publishes dbgsym, you may need to include 'main/debug' component
Repository: 'deb hirsute main'
Co-installable PHP versions: PHP 5.6, PHP 7.x and most requested extensions are included. Only Supported Versions of PHP ( for Supported Ubuntu 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:

You can get more information about the packages at

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

BUGS&FEATURES: This PPA now has a issue tracker:

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:

WARNING: add-apt-repository is broken with non-UTF-8 locales, see for workaround:

# LC_ALL=C.UTF-8 add-apt-repository ppa:ondrej/php
More info:
Adding repository.
Press [ENTER] to continue or Ctrl-c to cancel.

Press enter to allow installation on your system.

Run system update again for the new changes to take effect and restart when necessary.

3. Install PHP 8.1 on Ubuntu 21.04

When this is complete, move ahead and install PHP 8.1 on Ubuntu 21.04. Use the following command on your terminal.

$ sudo apt install php8.1

Many dependencies will be installed during this process.

sample output will look like this;

Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following package was automatically installed and is no longer required:
Use 'sudo apt autoremove' to remove it.
The following additional packages will be installed:
  apache2 apache2-bin apache2-data apache2-utils bzip2 libapache2-mod-php8.1 libapr1 libaprutil1
  libaprutil1-dbd-sqlite3 libaprutil1-ldap libjansson4 liblua5.3-0 libsodium23 mailcap mime-support php-common
  php8.1-cli php8.1-common php8.1-opcache php8.1-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 libapache2-mod-php8.1 libapr1 libaprutil1
  libaprutil1-dbd-sqlite3 libaprutil1-ldap libjansson4 liblua5.3-0 libsodium23 mailcap mime-support php-common
  php8.1 php8.1-cli php8.1-common php8.1-opcache php8.1-readline ssl-cert
0 upgraded, 22 newly installed, 0 to remove and 0 not upgraded.
Need to get 6841 kB of archives.
After this operation, 30.1 MB of additional disk space will be used.
Do you want to continue? [Y/n] 

Press Y to allow the installation to continue.

You can now check the PHP version on your system

$ php --version
PHP 8.1.0 (cli) (built: Nov 25 2021 20:22:22) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.0, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.0, Copyright (c), by Zend Technologies

4. Conclusion

Congratulations, you have successfully installed PHP 8.1 on your Ubuntu 21.04. You can now continue learning more and in case you have a problem you consult us or PHP documentation.

About Kipkoech Sang

I am a technology enthusiast who loves to share gained knowledge through offering daily tips as a way of empowering others. I am fan of Linux and all other things open source.
View all posts by Kipkoech Sang →

Leave a Reply

Your email address will not be published.