How to Install PHP 8.0 on Ubuntu 20.04 LTS
Introduction
In this guide we will see how to install PHP 8.0 on Ubuntu 20.04 LTS focal Fossa. However, PHP is an open source programming language. Additionally, I will try to cover all the related topics like installation of all the most common PHP 8.0 extensions. Moreover, PHP is a server side scripting language. Also, it is one of the most widely used languages for development of dynamic and interactive web pages. As we know that, the first step in programming is getting your system up and running with your favorite programming language installed and configured on it. Normally PHP is used with MySQL / MariaDB Database Server.
Moreover, in this tutorial we will install and setup PHP 8.0 programming environment. We will also test this installed programming environment.
Link to the Official Website of PHP |
Prerequisites for Installation of PHP 8.0 on Ubuntu 20.04 LTS
Following are the point wise basic requirements for installing PHP 8.0 on Ubuntu 20.04 LTS Focal Fossa
- First we will require a machine with pre-installed Ubuntu 20.04 Server / Desktop operating system. If you want to start fresh by installing Ubuntu 20.04 operating system on your machine then you can follow our tutorial on installing Ubuntu 20.04, the link is given below:
Link to the Official Website of Ubuntu 20.04 LTS Focal Fossa
- We will also require root / administrative User Name and Passwords of the operating system. This will enable us to install the PHP language and its associated applications, libraries and dependency software.
- We will need a stable and fast internet connection for downloading and installing all the applications to setup the PHP 8 programming environment.
How to install Ubuntu 20.04 LTS
Update Ubuntu 20.04 LTS Operating System
However, before installing PHP 8.0 we must update the Ubuntu 20.04 operating system. To update the Ubuntu 20.04 operating system open the terminal window and issue the following command as given below:
sudo apt update
sudo apt upgrade
Installation of PHP 8.0 on Ubuntu 20.04 LTS
However, the default PHP version in Ubuntu 20.04 repository is PHP 7.4. This means that if we will try to install PHP without specifying the version number in the command line then PHP version 7.4 will be installed by default. Therefore, it is better not to depend on the default version of the PHP. Here, in this tutorial we will install PHP version 8.0 on our Ubuntu 20.04 LTS operating system.
Additionally, we will also install software-properties-common package. Because, this package performs management of all the additional software. Next, to install this we have to issue the following command in the terminal window:
sudo apt install software-properties-common
Next we will get a prompt for confirmation of installation of the software-properties-common package. Here we have to enter ‘Y’ to proceed to install the application.
The Ondrej Repository
Now, we will have to install ppa:ondrej/php repository. Because, all the versions of PHP are maintained in this repository. Moreover, we can install any version of the PHP language of our choice from this repository. Next, to install this repository, issue the following command in the terminal window.
sudo add-apt-repository ppa:ondrej/php
Again run apt update so that the newly added repository can be added in the local package manager. Issue the following command in the terminal window:
sudo apt-get update
Next, we are ready to install the PHP version of our choice. Here we have chosen to install PHP 8.0. Now issue the following command in the terminal window to install PHP 8.0. on our Ubuntu 20.04 LTS operating system:
sudo apt install php8.0
Now to verify the installed version of PHP, issue the following command in the terminal window:
php -v
The output of the above command will be similar to the given below:
php -v PHP 8.0.9 (cli) (built: Jul 30 2021 13:03:39) ( NTS ) Copyright (c) The PHP Group Zend Engine v4.0.9, Copyright (c) Zend Technologies with Zend OPcache v8.0.9, Copyright (c), by Zend Technologies |
Install most command extensions (packages) of PHP 8.0
Now, we have successfully installed PHP version 8.0 on our Ubuntu machine. Now this is the time when we should install the most common PHP extensions or packages on our system. Here, we can install more than one PHP extensions or packages just by issuing one command in the terminal window. For example to install the most common packages / extensions of PHP version 8.0, issue the following command in the terminal window:
sudo apt install php8.0-gd php8.0-xml php8.0-soap php8.0-mbstring php8.0-mysql
However, the Apache Web Server is one of the most common web servers for deploying PHP based web applications. Therefore, to integrate PHP version 8.0 with the Apache Web Server we have to install the following Apache module on our machine:
sudo apt install libapache2-mod-php8.0
Now to enable the PHP version 8.0 modules, we have to issue the following command in the terminal windows:
sudo a2enmod php8.0
Then we have to reload the apache2 web server. To reload the Apache2 web server issue the following command in terminal window:
sudo systemctl reload apache2
Test the PHP 8.0 Installation
Now is the time to test our PHP version 8.0 installation. For this you have to navigate to the directory ‘/var/www/html’. Now you have to open your favorite text editor and create a file phpinfo.php. However, we are using nano editor here. Next copy and paste the lines given below in the phpinfo.php file. Now save the file and exit from the text editor.
<?php
phpinfo();
?>
Now open your favorite web browser and type the URL http://localhost/phpinfo.php in the address bar of the web browser and you will see the following output in the web browser as shown below.
Moreover, all the PHP configurations of the Apache Web Server are normally saved in /etc/php/8.0/apache2/php.ini file. From this file we can get the list of all the loaded PHP extensions.
List PHP Extensions
However, to get the list of all the loaded PHP extension, issue the following command in the terminal window:
php –m
The output of the above command will be similar to the output given below:
php -m [PHP Modules] calendar Core ctype date dom exif FFI fileinfo filter ftp gd gettext hash iconv json libxml mbstring mysqli mysqlnd openssl pcntl pcre PDO pdo_mysql Phar posix readline Reflection session shmop SimpleXML soap sockets sodium SPL standard sysvmsg sysvsem sysvshm tokenizer xml xmlreader xmlwriter xsl Zend OPcache zlib [Zend Modules] Zend OPcache |
Conclusion
Finally, we have successfully installed PHP version 8.0. Additionally, we have also installed the most common extensions of PHP version 8. We have also listed the loaded extensions and checked the installed PHP version. Now we can start using PHP version 8.0. Enjoy!