Trending News


How To Install PHP 8 on Ubuntu 18.04 / 20.04 [ Easy Guide ]

How To Install PHP 8 on Ubuntu 18.04 / 20.04 [ Easy Guide ] 

Php 8 on ubuntu

One of the most popular server-side programming languages is PHP. This guide will show you how to install PHP 8.0 on Ubuntu 18.04 / 20.04. When creating dynamic and responsive websites, it is the language of choice.

Table of Contents

PHP empowers many major CMS Platforms like WordPress, Drupal, and Magento. Released on November 26, 2020. Php 8.0 is the latest edition at the time we wrote this guide.

It boasts of several following features and optimizations:
  • JIT Compiler
  • Union Types
  • Named Arguments
  • Null Safe Operator
  • Match Expression
  • Improvements in error handling and consistency

Ubuntu 20.04 repositories are the default version of PHP 7.4 at the time of writing. From the Ondrej/PHP repository, we will install PHP.

Make sure that your apps support it before upgrading to or installing PHP 8. So, let us get started with the guide on installing and using PHP 8.0 on the Ubuntu 18.04 / 20.04 LTS Focal system.


Enable PHP Repository

PHP 7.4 was the default PHP version in Ubuntu 20.04 repositories at the time of writing. We will use the Ondrej PPA repository to install the latest release of PHP. Many PHP versions and PHP extensions are available in this repository.

But first, update your Ubuntu system packages and install certain prerequisites.

					$ sudo apt update
$ sudo apt upgrade
$ sudo apt install  ca-certificates apt-transport-https software-properties-common
After that, add the Ondrej PPA.
					$ sudo add-apt-repository ppa:ondrej/php

To proceed with adding the repository, hit ENTER when prompted.

Install PHP 8.0 on Ubuntu with Apache

If you use Apache as your web server, you can install PHP as an Apache module or PHP-FPM.

Install PHP as an Apache Module

Install Php as an Apache module with the following commands:
					sudo apt update
sudo apt install php8.0 libapache2-mod-php8.0

Restart Apache after installing the packages to allow the PHP module to load:

					sudo systemctl restart apache2


Set up Apache using PHP-FPM

PHP’s Php-FPM is a FastCGI process manager. To install the required packages, use the following command:

					sudo apt update
sudo apt install php8.0-fpm libapache2-mod-fcgid
PHP-FPM is disabled by default in Apache. Run the following command to enable it:
					sudo a2enmod proxy_fcgi setenvif
sudo a2enconf php8.0-fpm
Restart Apache to apply the changes:
					systemctl restart apache2


Install PHP 8.0 on Ubuntu with Nginx

For the Nginx server, there is no official PHP module. In that case, we will deploy PHP applications via Nginx web servers using PHP-FPM.

As a result, you must first install PHP fpm on your system.
					sudo apt install php8.0-fpm 

Enable and start the service after the installation is complete.
					sudo systemctl enable php8.0-fpm  sudo systemctl start php8.0-fpm

Then, with the following code, update the Nginx server block configuration.
					server {

   # server block code ...

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php8.0-fpm.sock;


The above code tells Nginx to use PHP-fpm to handle all files with the “.php” extension.

To apply the changes, restart the Nginx web server.
					sudo systemctl restart nginx 


Install PHP 8 Extensions

PHP extensions are libraries that extend PHP’s functionality. These extensions are available as packages and installed in the following way:

					$ sudo apt install php8.0-[extension-name]


Examples include the SNMP, Memcached, and MySQL extensions installed in the following example.

					$ sudo apt install php8.0-snmp php-memcached php8.0-mysql


Verify Php 8 Installation

Run the following command to verify the version of PHP you have installed:
					$ php -v

You can also make a sample PHP file in /var/www/HTML like shown:
					$ sudo vim /var/www/html/info.php

Save the file after pasting the lines mentioned below:


Finally, open your browser and type in the IP address of the server as shown:

You should see the following webpage.
Php 8 on ubuntu Yehiweb

Switch from Default Php Version

You can switch between installed PHP versions on your system quickly. Run the following terminal command:

					sudo update-alternatives --config php  

PHP 8.0 and PHP 7.4 are installed on this system. Select your preferred PHP version.
					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  to keep the current choice[*], or type selection number: [ENTER CHOICE HERE]


We hope you can now install PHP 8.0 and easily integrate it with either the Apache or Nginx web servers. Your feedback is much appreciated.

Share on facebook
Share on twitter
Share on linkedin
Share on pinterest
Share on reddit
Share on vk
Share on tumblr
Share on skype
Share on stumbleupon
Share on whatsapp
Share on email

Related posts

Leave a Reply

Required fields are marked *