How To Install PHP 8.1/7.4/5.6 on Ubuntu 22.04/18.04
PHP is a recursive acronym for Hypertext Preprocessor. It is an open-source, well known broadly useful scripting language that is generally used for creating websites and web-based applications. It is used to make web-based business sites, blogs, and API applications. We will use Ondrej PPA for installing PHP on the Ubuntu server. Which contains various versions of PHP packages.
Useful Articles:
- HOW TO INSTALL PHP 7.4, 7.3, 7.2 & 7.1 ON UBUNTU 19.10
- HOW TO INSTALL PHP 7.4, 7.3, 7.2, 7.1 ON CENTOS/RHEL 8
- HOW TO INSTALL PHP 7.4, 7.3, 7.2, 7.1 ON CENTOS/RHEL 7/6
- ENABLE PHP ERRORS TO DISPLAY ON WEB BROWSER
- HOW TO DOWNGRADE PHP 7.1 TO 5.6 ON UBUNTU
- HOW TO UPGRADE PHP 5.6 TO 7.1 ON UBUNTU
- MONGODB PHP MODULE (EXTENSION) ON CENTOS 5/6/7
- INSTALL LAMP (APACHE 2.4, MYSQL 5.6, AND PHP 7.0) ON UBUNTU USING PPA
- INSTALL LAMP (APACHE 2.4, MYSQL 5.6, AND PHP 7.0) ON CENTOS/RHEL 7
This article describes step-by-step how to install PHP versions 8.1, 7.4 and 5.6 on your Ubuntu.
Step 1: Update Current Packages
First, let’s update the Apt cache and upgrade the current packages of a system using the following command:
$ sudo apt update && sudo apt upgrade -y
Step 2: Add the required PPA Repository
PHP installation on Ubuntu systems is really direct. You simply have to add the necessary PPA and you can install any PHP version on the Ubuntu system.
Before adding the necessary PPA first install a few required dependencies using the below-mentioned command:
$ sudo apt install software-properties-common ca-certificates lsb-release apt-transport-https
Add the Ondrej PPA to your system, which contains various versions of PHP packages for the Ubuntu systems.
$ sudo add-apt-repository ppa:ondrej/php
Now, update the repository:
$ sudo apt update
Step 3: Install PHP
The Ondrej repository contains various versions of PHP. The most recent stable version of PHP is 8.0. You can install any of the necessary PHP versions to your system.
Install PHP 8.1:
$ sudo apt install php8.1
Install PHP 7.4:
$ sudo apt install php7.4
Install PHP 5.6:
$ sudo apt install php5.6
Applications may require other PHP extensions likewise, that can also be added using the below-mentioned syntax. You need to replace [extension] with the PHP extension you want to install. I am going to install “php-mbstring, php-mysql, php-xml, and php-curl”
Syntax:
$ sudo apt install php8.1-[extension]
PHP Extensions:
$ sudo apt install php8.1-mysql php8.1-mbstring php8.1-xml php8.1-curl
Step 4: Check Active PHP Version
In order to check the version number of your installed PHP, run the following command.
$ php -v Output:
PHP 8.1.6 (cli) (built: May 17 2022 16:46:54) (NTS) Copyright (c) The PHP Group Zend Engine v4.1.6, Copyright (c) Zend Technologies with Zend OPcache v8.1.6, Copyright (c), by Zend Technologies
Step 5: Switching between installed PHP versions
You can use the update-alternatives command to set the default PHP version, run the command below.
$ sudo update-alternatives --config php
There are 4 choices for the alternative php (providing /usr/bin/php). Selection Path Priority Status ------------------------------------------------------------ * 0 /usr/bin/php8.1 81 auto mode 1 /usr/bin/php5.6 56 manual mode 2 /usr/bin/php7.4 74 manual mode 3 /usr/bin/php8.0 80 manual mode 4 /usr/bin/php8.1 81 manual mode Press to keep the current choice[*], or type selection number: 2
In the output of the above command, the currently enabled version of PHP is indicated by a * symbol. You can change it as per your prerequisite.
About PHP Configuration Files:
After switching from one version to another, you can find your PHP configuration file under a directory with the version number under /etc/php directory.
For PHP 8.1:
PHP CLI: /etc/php/8.1/cli/php.ini Apache: /etc/php/8.1/apache2/php.ini PHP FPM: /etc/php/8.1/fpm/php.ini For PHP 7.4:
PHP CLI: /etc/php/7.4/cli/php.ini Apache: /etc/php/7.4/apache2/php.ini PHP FPM: /etc/php/7.4/fpm/php.ini For PHP 5.6:
PHP CLI: /etc/php/5.6/cli/php.ini Apache: /etc/php/5.6/apache2/php.ini PHP FPM: /etc/php/5.6/fpm/php.ini
You can also find your PHP configuration file, by running the command below.
$ sudo php -i | grep "Loaded Configuration File" Enjoy it!