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.
- 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
$ sudo add-apt-repository ppa:ondrej/php
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
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
sudo a2enmod proxy_fcgi setenvif
sudo a2enconf php8.0-fpm
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.
sudo apt install php8.0-fpm
sudo systemctl enable php8.0-fpm sudo systemctl start php8.0-fpm
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.
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
$ php -v
/var/www/HTML
like shown:
$ sudo vim /var/www/html/info.php
http://server-ip/info.php

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
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]
Conclusion
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.
Bilal Shafqat
Related posts
New Articles
Top 7 Tools for Remote Sales
No matter the fact it has never been easier to travel to any point of the world to have a…