How to Install PHP 5.6 on Ubuntu 16.04 & Debian 9

0

As you already know that both Ubuntu 16.04 and Debian 9 has been shipped with PHP 7 and applications which are designed to work with PHP 5 like vnStat PHP Frontend, PHP Server Monitor, etc., may not work due to compatibility issues.

In such cases, you would need to install PHP 5.6 on Ubuntu 16.04 / Debian 9 to meet the requirements of the application that you want to install it on the system.

Here, in this howto, we will go through the steps to install PHP 5.6 on Ubuntu 16.04 / Debian 9.

Setup Ondřej Repository

Ondřej Surý maintains PHP 5.6 and PHP 7.0 repository for Ubuntu and Debian. We will now use the Ondej repository for our PHP 5.6 installation.

### Ubuntu 16.04 ###

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

### Debian 9 ###

sudo apt-get update
sudo apt-get install -y apt-transport-https curl
curl https://packages.sury.org/php/apt.gpg | sudo apt-key add -
echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/php5.list

Install PHP 5.6 on Ubuntu 16.04 / Debian 9

Update the repository cache using the following command.

sudo apt-get update

Now, install PHP 5.6 / PHP 7.0 using the apt command.

sudo apt-get install -y php5.6

Verify the PHP version using the following command.

php -v

Output:

PHP 5.6.33-1+0~20180105151408.9+stretch~1.gbp0deeda (cli) 
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
    with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies

Screenshot:

Install PHP 5.6 on Ubuntu 16.04 - Ubuntu 16.04
Install PHP 5.6 on Ubuntu 16.04 – Ubuntu 16.04
Install PHP 5.6 on Ubuntu 16.04 - Debian 9
Install PHP 5.6 on Ubuntu 16.04 – Debian 9

Extras

There are chances that your system may already have PHP 7 due to which the above command may show the PHP version as 7.0 or 7.2.

You can use the below command to set the default PHP version as 5.6 and can switch between PHP 5.6 and PHP 7, anytime.

sudo update-alternatives --config php

Output:

There are 2 choices for the alternative php (providing /usr/bin/php).

  Selection    Path             Priority   Status
------------------------------------------------------------
* 0            /usr/bin/php7.0   70        auto mode
  1            /usr/bin/php5.6   56        manual mode
  2            /usr/bin/php7.0   70        manual mode

Press  to keep the current choice[*], or type selection number: 1
update-alternatives: using /usr/bin/php5.6 to provide /usr/bin/php (php) in manual mode

Now, PHP v5.6 has been set as default for PHP.

That’s All.

You might also like