Installing PHP 5.6 on Xenial (16.04)

534,314

Solution 1

Remove all the stock php packages

List installed php packages with dpkg -l | grep php| awk '{print $2}' |tr "\n" " " then remove unneeded packages with sudo aptitude purge your_packages_here or if you want to directly remove them all use :

sudo apt-get purge `dpkg -l | grep php| awk '{print $2}' |tr "\n" " "`

Add the PPA

sudo add-apt-repository ppa:ondrej/php

If you get add-apt-repository: command not found run the following command first :

sudo apt-get install software-properties-common

Install your PHP Version

sudo apt-get update
sudo apt-get install php5.6

You can install php5.6 modules too for example

sudo apt-get install php5.6-mbstring php5.6-mcrypt php5.6-mysql php5.6-xml

Verify your version

If you have installed php5.6-cli

sudo php -v

If you don't have php5.6-cli, put this on a .php file

<?php 
      //outputs php configuration info
      phpinfo(); 
?>

Solution 2

Run the following commands:

$ sudo apt-get install python-software-properties
$ sudo add-apt-repository ppa:ondrej/php
$ sudo apt-get update
$ sudo apt-get install -y php5.6

Check your PHP Version

php -v

Solution 3

You can even consider using vagrant (or similar technology) to assist in using both versions at the same time. Vagrant is essentially a VM client designed to help spin VM's up and down quickly and easily with project based configuration; i.e. you can configure a VM specifically for a project and each VM can be different per project if need be.

This means that you can develop a project and test it on your local machine using the same production configuration as the project will be run under. Not only is this great for yourself but also means that a team of developers can always test under the same circumstances as the VM configuration is portable and cross-platform (it's just a text file).

Once vagrant and virtual box are installed, it's as easy as running:

vagrant init ubuntu/trusty64; vagrant up --provider virtualbox

and once the vm is up and running you can use:

vagrant ssh

and you can now manually install apache, php and mysql. You can also define a provisioning configuration, so that when you enter:

vagrant up

the VM builds itself as well as installs all the binaries you need and can even do a git clone of your code, setup networking so you can view the application in your browser, etc.

For more detailed information look here: https://www.vagrantup.com/docs/getting-started/

Solution 4

You can use XAMPP Linux.You can download any version of xampp linux from the below mentioned official xampp site.

https://www.apachefriends.org/download.html

Enter downloaded file location using terminal.

First give certain permission for the downloaded binary file. (Assume that i downloaded 64 bit binary package.)

sudo chmod a+x xampp-linux-x64-5.6.23-0-installer.run

Then install the xampp

sudo ./xampp-linux-x64-5.6.23-0-installer.run

If you are download 32 bit binary file do below mentioned like this

sudo chmod a+x xampp-linux-5.6.23-0-installer.run 
sudo ./xampp-linux-5.6.23-0-installer.run
Share:
534,314

Related videos on Youtube

Nikolai
Author by

Nikolai

Updated on September 18, 2022

Comments

  • Nikolai
    Nikolai over 1 year

    I need to use php5 (5.6) because I'm working on a Drupal 7 project which doesn't support php7. The problem is I recently updated to ubuntu 16.04 Xenial which seems to no longer support php5.

    I tried to install it from this repo, however when It try to install it with apt I always get similar errors.

    $ sudo apt-get install php5-cli
    Cependant les paquets suivants le remplacent :
      php7.0-cli:i386 php7.0-cli`
    

    Which means it's obsolete. How can I install PHP 5.6 on Xenial?

  • Graham Wheeler
    Graham Wheeler almost 8 years
    How to install PEAR? apt shows no php5-pear package, and trying to install php-pear goes and installs php7 again.
  • oerdnj
    oerdnj almost 8 years
    @GrahamWheeler php-pear pull just CLI PHP 7.0 (php7.0-cli) and that's harmless
  • Olaf Dietsche
    Olaf Dietsche almost 8 years
    Minor nitpick. You can avoid grep and tr by awk already, e.g. dpkg -l | awk '/php/ {printf "%s ", $2;}'
  • Weijing Jay Lin
    Weijing Jay Lin almost 8 years
    I got error: 'add-apt-repository: command not found'
  • Erel Segal-Halevi
    Erel Segal-Halevi over 7 years
    I cannot install packages. E.g, "sudo apt-get install php5.6-mysql" returns "E: Unable to locate package php5.6-mysql"
  • Eric FD
    Eric FD over 7 years
    Don't forget to service apache2 restart if using with Apache.
  • fmalina
    fmalina over 7 years
    Ondřej Surý saving our asses again.
  • Neek
    Neek over 7 years
    What do we do if some packages are not found? I installed php5.6 from the ondrej ppa a week or so ago, today I need the mysql connector, but sudo apt-get install php5.6-mysql results in errors like: Err:1 http://ppa.launchpad.net/ondrej/php/ubuntu xenial/main amd64 php5.6-opcache amd64 5.6.28-2+deb.sury.org~xenial+1 404 Not Found for what seems like all deps. Digging in ppa.launchpad.net/ondrej/php/ubuntu/pool/main/p/php5.6 I can see opcache for example, but 5.6.29 not 5.6.28 as reported in my error. I have run apt-get update, don't know what else to try.
  • Neek
    Neek over 7 years
    Oops, never mind - I had not added the mongodb-org repository key, so my apt-get update failed on mongodb-org without getting to update ondrej/php/ubuntu. I thought it was a noisy warning, not a critical failure that halted the update. Once the mongodb-org key was added, then apt-get update completed and my apt-get install php5.6-mysql fetched the correct version 5.6.29. All is well.
  • Quan To
    Quan To over 7 years
    It's not really practical. I can just install both php5.6-fpm and php7.0-fpm, then fastcgi_pass to the socket that the desired PHP version is running on (I'm using nginx, but I reckon apache2 must have something similar). It will be much more lightweight.
  • storm
    storm over 7 years
    I think that docker would be more suitable .. It's a good idea, though.
  • Eugene
    Eugene about 7 years
    What if I had installed php before though ppa:ondrej/php5-5.6? It seems it doesn't see new installs with sudo apt-get install php5.6-curl.
  • Steve Childs
    Steve Childs almost 7 years
    @oerdnj - that doesn't work, if you install php5.6 and then php-pear, which includes bits of php7.1, when you run pecl you get a load of php errors (invalid for-each arguments). :(
  • Steve Childs
    Steve Childs almost 7 years
    To reply to my own comment! If you do get loads of errors when running pecl, ensure you've added the php-xml package, e.g. "sudo apt-get install php5.6-xml"
  • Anuraag Vaidya
    Anuraag Vaidya over 5 years
    Do sudo add-apt-repository ppa:ondrej/apache2 if running apache2
  • Shanteshwar Inde
    Shanteshwar Inde over 4 years
    not working for ubuntu 18.04. showing forbidden.
  • rubo77
    rubo77 over 4 years
    add -y to all commands if you need to run this non-interactive
  • GTodorov
    GTodorov over 3 years
    This no longer works! The ppa is non-existent!