Installing PHP 5.6 on Xenial (16.04)

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(); 
?>

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

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/

Tags:

Php

Drupal

Apt

Php7