How to install PHP 5.6 on Ubuntu

You may need to run a project that depends on an older version of PHP. This tutorial teaches you how to install PHP 5.6 on Ubuntu.
What is PHP
PHP is a scripting language that is widely used in web development. As an open-source technology, PHP reduces the cost of web development and enjoys the support of a large community of web developers.
Prerequisites
The software-common-properties package provides utilities that we will use to add the PHP PPA to the Ubuntu repository.
To install software-common-properties on Ubuntu type:
sudo apt install software-common-properties
Among many other scripts shipped with software-common-properties now we have the add-apt-repository script on Ubuntu. We will use it later to add a private PHP PPA to our Ubuntu repository.
How to install PHP 5.6 on Ubuntu
Compiling PHP from the source code is a hard and time-consuming task. This tutorial teaches you how to install PHP 5.6 through the Ondrej PPA.
Step 1: Add the PHP PPA to the Ubuntu’s repository
Ondrej Sury is a Debian developer who has been packaging PHP for Debian-based distributions for many years.
Add Ondrej’s PHP PPA to your Ubuntu repository:
sudo add-apt-repository ppa:ondrej/php
Step 2: Update Ubuntu
For changes to take place after adding Ondrej’s PHP PPA to Ubuntu type:
sudo apt update
Step 3: Install PHP 5.6 on Ubuntu
Now that you have successfully added Ondrej’s PHP PPA on Ubuntu to install PHP 5.6 type:
sudo apt install php5.6
To print the PHP version installed type:
php --version
Step 3.1 Install necessary PHP packages
A web project has many dependencies. To be able to run it you need to install its requirements. To install common PHP packages type:
sudo apt install php5.6-common php5.6-mbstring php5.6-mysql php5.6-gd php5.6-zip php5.6-curl php5.6-bcmath
The following is a short explanation of the above PHP packages.
- php5.6-common contains the .so files for zip, curl, json, fileinfo, and phar.
- php5.6-mbstring helps to deal with multibyte encodings.
- php5.6-mysql provides the MySQL module for PHP.
- php5.6-gd provides functionalities for image processing in PHP.
- php5.6-zip is a package that offers the tools for creating and dealing with zip archives.
- php5.6-curl is a library that helps to make HTTP requests in PHP.
- php5.6-bcmath is used for arbitrary precision mathematics in PHP.
How to run a PHP script
You can run a PHP script through the command line or the browser. Use the nano text editor to create a new PHP file.
nano test.php
Then write the following PHP code.
<?php
echo "Testing the environment.\n"
?>
Type CTRL O to write the changes to the disk. Then CTRL X to exit.
To run the PHP script type:
php test.php
To execute the PHP script in the browser first copy test.php to /var/www/html.
sudo cp test.php /var/www/html
Then type your machine’s IP address followed by the PHP script’s name as below.
http://192.168.100.27/test.php

Final thoughts
Through this tutorial, you learned how to install PHP 5.6 on Ubuntu. You also learned how to execute a simple PHP script through the command line and the browser.