How to Install PHP on Ubuntu 20.04 LTS

In this article, We are going to perform How to Install PHP on Ubuntu 20.04 LTS

Introduction

PHP is free and open source multi purpose scripting language which is used for Web Development.

Prerequisites

  • Ubuntu 20.04 LTS
  • SSH Access with sudo privileges

Install PHP on Ubuntu 20.04 LTS

if you are using Apache web server, Install latest php with Apache on Ubuntu 20.04 LTS using below commands

sudo apt update
sudo apt install php libapache2-mod-php

Restart an Apache service to take effect

sudo systemctl restart apache2

OR

Add PHP PPA Repository

Update the System Packages

 sudo apt update 

Add the PHP PPA repository in Ubuntu 20.04 LTS using below commands,

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

Update the system packages

sudo apt update

Install PHP 8.0 on Ubuntu 20.04 LTS

Install php 8.0 on Ubuntu 20.04 using below command

sudo apt -y install php8.0

Install PHP 7.4 on Ubuntu 20.04 LTS

Now Install php 7.4 on Ubuntu 20.04 LTS using below command

sudo apt -y install php7.4

Install PHP 7.3 on Ubuntu 20.04 LTS

Install php 7.3 on Ubuntu 20.04 LTS using below command

sudo apt -y install php7.3

Install PHP 7.2 on Ubuntu 20.04 LTS

Install php 7.2 on Ubuntu 20.04 LTS using below command

sudo apt -y install php7.2

Install PHP 7.1 on Ubuntu 20.04 LTS

Install php 7.1 on Ubuntu 20.04 LTS using below command

sudo apt -y install php7.1

Install PHP 7.0 on Ubuntu 20.04 LTS

Install php 7.0 on Ubuntu 20.04 LTS using below command

sudo apt -y install php7.0

Check PHP Version

To check current PHP version using command line

php -v

Output:

PHP 7.4.9 (cli) (built: Aug 7 2020 14:30:01) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
with Zend OPcache v7.4.9, Copyright (c), by Zend Technologies

Installing PHP Modules/Extensions

Next Install php modules as per your php versions, search php modules are available using below command

 sudo apt-cache search php7*

Install php 8.0 Modules

sudo apt search php8.0-*

Install php 8.0 modules on Ubuntu 20.04 LTS using below command

sudo apt install php8.0-gd php8.0-xml php8.0-soap php8.0-mbstring php8.0-mysql

Install php 7.4 Modules

Install php 7.4 modules on Ubuntu 20.04 LTS using below command

sudo apt install php7.4-common php7.4-mysql php7.4-curl php7.4-json php7.4-cgi php7.4-opcache php7.4-mbstring

Install php 7.3 Modules

Install php 7.3 modules on Ubuntu 20.04 LTS using below command

sudo apt install php7.3-common php7.3-mysql php7.3-xml php7.3-xmlrpc php7.3-curl php7.3-gd php7.3-cli php7.3-dev php7.3-mbstring php7.3-opcache php7.3-zip

Install php 7.2 Modules

Install php 7.2 modules on Ubuntu 20.04 LTS using below command

sudo apt install php7.2-common php7.2-mysql php7.2-curl php7.2-json php7.2-cgi php7.2-opcache php7.2-mbstring

Install php 7.1 Modules

Install php 7.1 modules on Ubuntu 20.04 LTS using below command

sudo apt-get install php7.1-cli php7.1-common php7.1-json php7.1-opcache php7.1-mysql php7.1-mbstring php7.1-mcrypt php7.1-zip

Install php 7.0 Modules

Install php 7.0 modules on Ubuntu 20.04 LTS using below command

sudo apt-get install php7.0-cli php7.0-common php7.0-json php7.0-opcache php7.0-mysql php7.0-mbstring php7.0-mcrypt php7.0-zip

Switch PHP Version in Ubuntu

If you want to switch between multiple php version in Ubuntu and set default version of php using command line

sudo update-alternatives --config php

Select choice and type selection number as per your requirement

There are 5 choices for the alternative php (providing /usr/bin/php).
Selection Path Priority Status
0 /usr/bin/php7.4 74 auto mode
1 /usr/bin/php7.0 70 manual mode
2 /usr/bin/php7.1 71 manual mode
3 /usr/bin/php7.2 72 manual mode
4 /usr/bin/php7.3 73 manual mode
5 /usr/bin/php7.4 74 manual mode
Press to keep the current choice[*], or type selection number: 1

Install PHP 7.4 with Nginx on Ubuntu 20.04 LTS

Use below commands to Install php 7.4 with Nginx web server on Ubuntu 20.04 LTS

sudo apt update
sudo apt install nginx php-fpm

once installed restart the nginx server

sudo systemctl restart nginx

To restart the php7.4-fpm

sudo systemctl restart php7.4-fpm

To check the status of php7.4-fpm

sudo systemctl status php7.4-fpm

How to Completely Remove php 7 from Ubuntu 20.04 LTS

If you want uninstall php 7 in Ubuntu 20.04 LTS, Enter below command, it will remove all php 7.* versions

sudo apt-get purge php7.*

clean the deb packages

sudo apt-get autoclean

Remove the unwanted packages that is not needed

sudo apt-get autoremove

Conclusion:

In this article, We have performed ,How to Install PHP on Ubuntu 20.04 LTS System, check php version, switch between multiple php version and set default php version, install php 7.4 with nginx web server and uninstall php 7 from Ubuntu 20.04 LTS

Related Articles:

FOSS TechNix

FOSS TechNix (Free,Open Source Software's and Technology Nix*) founded in 2019 is a community platform where you can find How-to Guides, articles for DevOps Tools,Linux and Databases.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link
Powered by Social Snap