How to install PHP Version 7.0 to 7.4 in Ubuntu 18.04 and 20.04

How to install PHP Version 7.0 to 7.4 in Ubuntu 18.04 and 20.04

PHP stands for Hypertext Pre Processor. It is one of the world’s most popular server-side scripting languages for web development. PHP is flexible, fast, and pragmatic. It can power everything from your blog to some of the world’s most popular websites.

As a server-side scripting language, PHP can do anything related to a website’s backend, such as receiving data from forms, creating dynamic pages, working with databases, sending and receiving cookies, etc.

Advantages:

  • Platform Independent
  • Open source and dynamic Library support
  • Organized
  • Easy to understand and code
  • Gives Web Developers More Control
  • Maintenance
  • Stability
  • Database Connectivity
  • Performance
  • Reliability
  • Scalability
  • Compatibility
  • Easy integration and consistency

Prerequisites

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

PHP Modules

These are the basic components of PHP. Modules are very important since they connect PHP with other applications. For example, mysqli connects PHP with MySQL. 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

Update all the repositories of the server with the following command.

sudo apt update -y

Enable the PPA Repository

PHP versions are not available on the default repositories. So, we need to use a third-party PPA repository. To enable the repository, run the following command.

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

Now update the newly added repository with the following command.

sudo apt update -y

Install the PHP package

  • To install the PHP 7.0 run the following command.
sudo apt install php7.0 php7.0-opcache php7.0-xml php7.0-mbstring php7.0-ldap php7.0-curl php7.0-imap php7.0-zip php7.0-mysqli php7.0-intl php7.0-gd php7.0-cli php7.0-imagick php7.0-fpm libapache2-mod-php7.0 -y
  • To install PHP 7.1 run the following command.
sudo apt install php7.1 php7.1-opcache php7.1-xml php7.1-mbstring php7.1-ldap php7.1-curl php7.1-imap php7.1-zip php7.1-mysqli php7.1-intl php7.1-gd php7.1-cli php7.1-imagick php7.1-fpm libapache2-mod-php7.1 -y
  • To install PHP 7.2 run the following command.
apt install php7.2 php7.2-opcache php7.2-xml php7.2-mbstring php7.2-ldap php7.2-curl php7.2-imap php7.2-zip php7.2-mysqli php7.2-intl php7.2-gd php7.2-cli php7.2-imagick php7.2-fpm libapache2-mod-php7.2 -y
  • To install PHP 7.3 run the following command.
apt install php7.3 php7.3-opcache php7.3-xml php7.3-mbstring php7.3-ldap php7.3-curl php7.3-imap php7.3-zip php7.3-mysqli php7.3-intl php7.3-gd php7.3-cli php7.3-imagick php7.3-fpm libapache2-mod-php7.3 -y
  • To install PHP 7.4 run the following command.
apt install php7.4 php7.4-opcache php7.4-xml php7.4-mbstring php7.4-ldap php7.4-curl php7.4-imap php7.4-zip php7.4-mysqli php7.4-intl php7.4-gd php7.4-cli php7.4-imagick php7.4-fpm libapache2-mod-php7.4 -y
  • To check the default PHP version, run the following command.
php -v
  • To check all the installed PHP modules of the default PHP version, run the following command.
php -m

Switch between Multiple PHP versions

If the server or the local system have multiple PHP version installed on it and we need to change the default PHP version, then follow the below steps.

  • First, disable the current default PHP version. As an example, our server has PHP 7.4 as the default PHP version and we need to change it to PHP 7.1.
sudo a2dismod php7.4
  • Now enable the PHP version which you want to set as the default PHP version.
sudo a2enmod php7.1
  • Set the PHP version.
sudo update-alternatives --set php /usr/bin/php7.1
  • Now update the default PHP version globally with the following command.
sudo update-alternatives --config php

Conclusion

In today’s tutorial, we have learned about PHP and its mostly used modules, and How we can install the PHP version on the Ubuntu 18.04 /20.04. We have also learned how we can easily switch between multiple PHP versions.

If you guys have any issues or queries, let me know in the comments section.