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 -y php7.0-{opcache,xml,mbstring,ldap,curl,imap,zip,mysqli,intl,gd,cli,fpm} libapache2-mod-php7.0
  • To install PHP 7.1 run the following command.
sudo apt install -y php7.1-{opcache,xml,mbstring,ldap,curl,imap,zip,mysqli,intl,gd,cli,fpm} libapache2-mod-php7.1
  • To install PHP 7.2 run the following command.
sudo apt install -y php7.2-{opcache,xml,mbstring,ldap,curl,imap,zip,mysqli,intl,gd,cli,fpm} libapache2-mod-php7.2
  • To install PHP 7.3 run the following command.
sudo apt install -y php7.3-{opcache,xml,mbstring,ldap,curl,imap,zip,mysqli,intl,gd,cli,fpm} libapache2-mod-php7.3
  • To install PHP 7.4 run the following command.
sudo apt install -y php7.4-{opcache,xml,mbstring,ldap,curl,imap,zip,mysqli,intl,gd,cli,fpm} libapache2-mod-php7.4
  • 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.