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 php8.0 php8.0-opcache php8.0-xml php8.0-mbstring php8.0-ldap php8.0-curl php8.0-imap php8.0-zip php8.0-mysqli php8.0-intl php8.0-gd php8.0-cli php8.0-imagick php8.0-fpm libapache2-mod-php8.0 -y
  • Run the following command to install PHP 8.1 on the server or the system.
sudo apt install php8.1 php8.1-opcache php8.1-xml php8.1-mbstring php8.1-ldap php8.1-curl php8.1-imap php8.1-zip php8.1-mysqli php8.1-intl php8.1-gd php8.1-cli php8.1-imagick php8.1-fpm libapache2-mod-php8.1 -y
  • 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 7.1 as the default PHP version and we need to change it to PHP 7.3.
sudo a2dismod php7.1
  • Enable the PHP version you want to use as the default.
sudo a2enmod php7.3
  • Set the PHP version, which you want to choose.
sudo update-alternatives --set php /usr/bin/php7.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.