How to install PHP Version 8.0 and 8.1 in Ubuntu 18.04 and 20.04

How to install PHP Version 8.0 and 8.1 in Ubuntu 18.04 and 20.04

PHP is a Hypertext PreProcessor. For web development, it is one of the world’s most popular server-side scripting languages. PHP is a flexible, fast, and pragmatic language. Scripting languages can handle any aspect of a website’s backend, including sending and receiving cookies, receiving data from forms, creating dynamic pages, and working with databases.

Advantages:

  • Open Source and Free of Cost
  • Platform Independence
  • Easy loading
  • User-friendly
  • Stable
  • Flexible
  • Database connection
  • Library support
  • No lengthy code required
  • Increased Job opportunity

Prerequisites

  • Server or Local system with Ubuntu 18.04/ Ubuntu 20.04
  • Root user or another user with Sudo privileges

PHP Modules

The list of PHP modules that are mostly used is as follows:-

opcache
xml
mbstring
ldap
curl
imap
zip
mysqli
intl
gd
cli
imagick
fpm

Update the server

Before PHP installation we need to update the Server’s packages and repositories. Run the following command to update the repositories.

sudo apt update -y

Enable the PPA repository

By default, PHP packages are not available on the server’s default repositories. So, we need to enable the third-party PPA repository. Run the following command to enable the repository.

sudo apt install software-properties-common -y
sudo add-apt-repository ppa:ondrej/php -y

After enabling the PPA repository, we need to update the server so that server can detect and add the repository to the server.

sudo apt update -y

Install the PHP package

  • Run the following command to install PHP 8.0 on the server or the system.
sudo apt install -y php8.0-{opcache,xml,mbstring,ldap,curl,imap,zip,mysqli,intl,gd,cli,fpm} libapache2-mod-php8.0
  • Run the following command to install PHP 8.1 on the server or the system.
sudo apt install -y php8.1-{opcache,xml,mbstring,ldap,curl,imap,zip,mysqli,intl,gd,cli,fpm} libapache2-mod-php8.1
  • Run the following command to check the default PHP version.
sudo php -v
  • Run the following command to check the PHP modules of the default PHP version.
sudo php -m

Switch between Multiple PHP versions

When more than one version of PHP is installed on the server or the local system. Follow the below steps to change the default PHP version.

  • Firstly, disable the current default PHP version. For example, our server has PHP 8.0 as the default PHP version and we need to change it to PHP 8.1.
sudo a2dismod php8.0
  • Enable the PHP version you want to use as the default.
sudo a2enmod php8.1
  • Set the PHP version, which you want to choose.
sudo update-alternatives --set php /usr/bin/php8.1
  • Run the following command to update the default PHP version globally.
sudo update-alternatives --config php

Conclusion

As a result of today’s tutorial, we have learned about PHP, its most popular modules, and how to install the PHP version on Ubuntu 18.04/20.04. We have also learned how we can easily switch between multiple PHP versions.

Let me know if you have any issues or questions in the comments section.