In this article, We are going to perform How to Install PHP 7 on Ubuntu 18.04/16.04 LTS.
Table of Contents
Introduction
PHP is free and open source multi purpose scripting language which is used for Web Development.
Prerequisites
- Ubuntu 18.04/16.04 LTS
- SSH Access with sudo privileges
Install PHP 7 on Ubuntu 18.04/16.04 LTS
if you are using Apache web server, Install php 7.4 with Apache on Ubuntu 18.04/16.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
Add the PHP PPA repository in Ubuntu 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 7.4 on Ubuntu
Now Install php 7.4 on Ubuntu 18.04/16.04 LTS using below command
sudo apt -y install php7.4
Install PHP 7.3 on Ubuntu
Install php 7.3 on Ubuntu 18.04/16.04 LTS using below command
sudo apt -y install php7.3
Install PHP 7.2 on Ubuntu
Install php 7.2 on Ubuntu 18.04/16.04 LTS using below command
sudo apt -y install php7.2
Install PHP 7.1 on Ubuntu
Install php 7.1 on Ubuntu 18.04/16.04 LTS using below command
sudo apt -y install php7.1
Install PHP 7.0 on Ubuntu
Install php 7.0 on Ubuntu 18.04/16.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 7.4 Modules
Install php 7.4 modules on Ubuntu 18.04/16.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 18.04/16.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 18.04/16.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 18.04/16.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 18.04/16.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
Use below commands to Install php 7.4 with Nginx web server on Ubuntu 18.04/16.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 18.04/16.04 LTS
If you want uninstall php 7 in Ubuntu 18.04/16.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 7 on Ubuntu 18.04/16.04 LTS System, Check php version, switch between multiple php version, install php 7 with nginx web server.
Related Articles:
How to Install Laravel on Ubuntu 20.04/18.04 LTS
How to Install PHP on Ubuntu 20.04 LTS
How to Install Memcached on Ubuntu 18.04/16.04 LTS