Quick tip: install PHP 7 on Raspbian using the official testing branch

With latest version of Raspbian being based on Debian Jessie, this still comes with PHP 5.6 by default. Since PHP 5.6 reaching its end of life, and only security updates support starting tomorrow, it's time to start looking at the newer versions. PHP 7.0 was released last december in 2015 but is still not merged in the stable branch. Luckily there's a little workaround for this issue!

To install PHP 7 we must tap into the testing branch of Raspbian. Start with opening /etc/apt/source.listwith your favorite editor.

sudo nano /etc/apt/sources.list

And add this line at the end of the file.

deb http://mirrordirector.raspbian.org/raspbian/ stretch main contrib non-free rpi

Now what we don't want is that every package is updated or installed from the stretch (testing) branch. To do this we can set some preferences that we want all packages to be selected from Jessie by default. Open up the following file /etc/apt/preferences.

sudo nano /etc/apt/preferences

And add the following contents to it.

Package: *
Pin: release n=jessie
Pin-Priority: 600

Save the file and run an update.

sudo apt-get update

Now if you want to install something from the testing branch, you can do this using the following format.
For example, installing the PHP 7 cli package can be done like this.

sudo apt-get install -t stretch php7.0-cli

And that's pretty much it, enjoy! Since this most likely my last post of 2016, happy new year! 🎆