Install PHP 8.0.x On Ubuntu Server

PHP is a scripting language known as Hypertext Preprocessor and initially stands for Personal Home Page. PHP is especially suited to web development and uses for server-side scripting. 

This article will show how you can install the latest PHP version, PHP 8.0.x, on the latest Ubuntu 22.0.4 or older. Using the following method, you can install PHP 8.0.x in other Linux systems such as Debian 10, etc.

Must Read: How to Upgrade from PHP 7.4 to PHP 8.0? [Also Explained for WordPress] 

Install PHP 8.0.x On Ubuntu

Installing the latest PHP version is very easy if you are using a cloud server, VPS, or a local machine. Execute the following commands to install PHP 8.0 on the Ubuntu system.

1. Add ‘ppa:ondrej/php‘ Repository:

PHP 8.0.x is the latest stable version of PHP, and it is the major update of the language and comes with many new features and optimizations. However, before installing the PHP 8.0.x, you have to add the ppa:ondrej/php repository. 

So, adding that, use the following command:

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

Update the system after adding the repository.

sudo apt update

2. Install PHP 8.0.x on Ubuntu

Now execute the following command to install PHP 8.0 on your Ubuntu system.

sudo apt install php8.0

Output:

root@PRONAY-PC:~# sudo apt install php8.0
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  libapache2-mod-php8.0 php-common php8.0-cli php8.0-common php8.0-opcache php8.0-readline
Suggested packages:
  php-pear
The following NEW packages will be installed:
  libapache2-mod-php8.0 php-common php8.0 php8.0-cli php8.0-common php8.0-opcache php8.0-readline
0 upgraded, 7 newly installed, 0 to remove and 6 not upgraded.
Need to get 4434 kB of archives.
After this operation, 20.1 MB of additional disk space will be used.
Do you want to continue? [Y/n]

When you execute the installing command, the system will prompt you to “Do you want to continue?” you have to type “Y” and hit enter on the keyboard to install PHP 8.0.

3. Check whether PHP is installed or not.

You have to run the following command to check whether PHP is installed or not. If you are upgrading PHP 7.4 to PHP 8.0, it will still show you the older version (PHP 7.4) when we execute the following command.

sudo php -v

Output:

root@PRONAY-PC:~#sudo php -v
PHP 8.0.5 (cli) (built: May  3 2021 11:30:57) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.5, Copyright (c) Zend Technologies
    with Zend OPcache v8.0.5, Copyright (c), by Zend Technologies

As I have mentioned, if your system has the older version, PHP 7.4, then the output will show PHP 7.4 instead of PHP 8.0. So you have to disable the older version and enable the newer PHP 8.0. Check out the in-depth article for upgrading PHP 7.4 to PHP 8.0 for Ubuntu, Debian, and the WordPress website.

Must read:- How to Upgrade PHP 8.0.x to PHP 8.1.x?

Install PHP 8.0.x Modules or Extensions

PHP has over 180+ extensions for different-different works. It depends upon you how you are going to use it or what for, so as per your requirement, you can install one by one extension or by group or all extensions you can install just in one command.

1. Search PHP 8.0 Extensions

Using the following command, you can search or list the extensions of PHP 8.0.

sudo apt search php8.0-* 

2. Install PHP 8.0 Extensions

Executing the following command, you can install the frequently used PHP modules.

sudo apt install php8.0-{bz2,curl,intl,mysql,readline,xml}

Output:

root@PRONAY-PC:~# sudo apt install php8.0-{bz2,curl,intl,mysql,readline,xml}
Reading package lists... Done
Building dependency tree
Reading state information... Done
php8.0-readline is already the newest version (8.0.5-2+ubuntu20.04.1+deb.sury.org+1).
php8.0-readline set to manually installed.
The following NEW packages will be installed:
  php8.0-bz2 php8.0-curl php8.0-intl php8.0-mysql php8.0-xml
0 upgraded, 5 newly installed, 0 to remove and 6 not upgraded.
Need to get 387 kB of archives.
After this operation, 1634 kB of additional disk space will be used.
Do you want to continue? [Y/n]

When you execute the PHP extensions installation command, the terminal will prompt you to “Do you want to continue?” You have to type “Y” and hit enter on the keyboard.

If you want to host a WordPress website, install these necessary PHP modules, so use the following command or, as a reference, take help from this article.

sudo apt install php8.0-common php8.0-cli php8.0-bz2 php8.0-curl php8.0-gd php8.0-intl php8.0-readline php8.0-xml php8.0-zip php8.0-fpm php8.0-bcmath php8.0-mbstring php8.0-imagick php8.0-mysql php8.0-zip

Install PHP 8.0.x Modules for Apache

Apache is an open-source web server. Without it, PHP doesn’t have much value. So if you haven’t installed the Apache webserver on Ubuntu, then install.

1. Install PHP Mods for Apache HTTP Server

sudo apt install libapache2-mod-php8.0 

2. Set PHP 8.0 as Default

sudo a2enmod proxy_fcgi setenvif

3. Enable PHP 8.0 module

sudo a2enconf php8.0-fpm

4. Restart Apache Server

sudo service apache2 restart

After executing the following step-by-step commands, installing the PHP modules for the Apache server will be done.

Install PHP 8.0.x Modules for Nginx

Using the following commands, you can install PHP modules for Nginx. However, there are no official PHP modules for NGiNX, but you can install the alternative by performing the following steps.

Before installing the PHP modules, first, you should install the Nginx web server.

1. Install the PHP-FPM module for Nginx

sudo apt install php8.0-fpm 

2. Enable and start the service

sudo systemctl enable php8.0-fpm
sudo systemctl start php8.0-fpm 

3. Restart the NGiNX web server

sudo systemctl restart nginx

Switch Default PHP Version

If your system has already installed an older PHP version, you have to switch to PHP 8.0. There are two methods to switch between different versions of PHP.

Method one:

In this first method, you have to execute a single command, and you can change PHP 8.0 between various PHP versions.

sudo update-alternatives --config php  

Output:

root@PRONAY-PC:~# sudo update-alternatives --config php
There are 2 choices for the alternative php (providing /usr/bin/php).

  Selection    Path             Priority   Status
------------------------------------------------------------
* 0            /usr/bin/php8.0   80        auto mode
  1            /usr/bin/php7.4   74        manual mode
  2            /usr/bin/php8.0   80        manual mode

Press <enter> to keep the current choice[*], or type selection number: [ENTER CHOICE HERE]

Method two:

That is the second method, and you have to execute some following commands and switch the PHP versions as your requirement. [This method is recommended for WordPress users]

Note: - This method will only works with Apache Webserver

Disable Old PHP Version:-

sudo a2dismod php7.4

Enable New PHP Version:-

sudo a2enmod php8.0

Restart Apache Server:-

sudo systemctl restart apache2.service

FAQs About PHP 8.0

Is PHP 8 backward compatible?

PHP has no backward compatibility. It is often when released a new version of PHP is, removed some features. Check official PHP 8.0 compatibility changelogs.

What changed in PHP 8?

PHP 8.0 is a significant update of the PHP language. It contains many new features and performance optimizations, including named arguments, union types, and more. Check PHP official changelogs.

Can WordPress Use PHP 8?

PHP 8.0 has been released officially, and it is stable now. Even WordPress 5.7.0 uses PHP 8.0, so you can update your PHP.

Conclusion

Using this tutorial, you can install PHP 8.0.x on your Ubuntu Linux system. Learn about more, like updating PHP 7.4 to PHP 8.0, installing Redis, and installing the LAMP stack on the Linux system.


Pronay Sarkar

Hello!
I am Pronay Sarkar, I love to write about tech and stuff.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.