Raspberry Pi3 and PHP 7

When looking for a small computer to use as an embedded device in the age of IoT (Internet of Things), it’s impossible to overlook the RaspberryPi. The third generation (Pi 3) is now out and I was able to obtain one through Amazon.com. The Pi 3 and it’s latest software does not disappoint. I have to say that booting into the new Raspian OS (based on Debian) is a lot faster than the original Raspberry Pi 1 I obtained two years ago. It’s now got a quad core processor running a good 500Mhz faster with nearly the same power foot print as the original. The operating system software has matured and now supports OpenGL optimizations within the desktop window manager. Where the original Pi 1 could barely play YouTube videos within the browser; the Pi 3 is able to run video within the browser or at full screen without a hiccup. The Pi 1 will definitely benefit from the faster software optimizations but running a full blown window manager was a chore on the Pi 1 and left many owners booting into console only mode to trim out as much overhead as possible. Pi 1’s limited two USB ports made the choice of sacrificing WiFi, a mouse, or a keyboard disappointing; not so with the Pi 3’s four USB ports.

In contrast to Pi 1 and Pi 2, the new Pi 3 with builtin WiFi and Bluetooth makes the system highly connected; while the quad processor makes running the default desktop, full office suite, and programming applications very snappy. The Raspberry Pi has truly matured to a fully operational desktop class computer with the same price tag as the original. Gone are the days of building your own tower with dozens of components and buzzing fans and heat sinks. For most current computer applications and garage projects, the Pi 3 fits nicely in the palm on you hand and is a very capable embedded device.

Installing PHP 7

While the Raspberry Pi has heaps of literature and technology revolving around JavaScript, Python, and other programming languages; PHP has consumed a lot of my time these days. A year ago, I would have snubbed the idea of using PHP in lieu of JavaScript as my go to language but I’m now seeing a lot of influences, if not mergers between the two languages: PHP has obtained a beautiful dependency/package manager (composer/packagist) that is not unlike what’s available for JavaScript via NodeJS. Programming libraries and projects such as ReactPHP mirror their JavaScript equivalent and now we’ve seen an incredible speed boost via the release of PHP 7. According to APIPlug.com, PHP 7 has been found to be a lot faster than its predecessor (I’m still waiting for their PHP vs NodeJS speed comparison). What better way to compliment our new Raspberry Pi 3’s quad core capabilities than with the cutting edge performance found in PHP 7? So let’s get started installing the cutting edge PHP 7 on the new Raspberry Pi 3.

To install PHP 7, we’ll need to gain access to the testing branch of Raspberry Pi’s Raspbian OS (aka under the codename: “stretch”). Start by editing the sources.list file used by apt-get by typing the following on the command line:

sudo nano /etc/apt/sources.list

The nano editor will open and you should be able to append the following line to the file, followed by pressing ctrl+x to exit and ‘y’ to save changes and press enter to confirm.

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

However, this will cause all updates and installs to come from “stretch” which isn’t considered stable. To avoid this, create a preferences file that will tell apt-get to obtain all packages from the current “jessie” release of Raspian first and fallback to “stretch” to accessing the missing PHP 7 components:

sudo nano /etc/apt/preferences

Again, the nano editor will open and you can paste the following content to prioritize where packages come from. Pressing ctrl+x exit and ‘y’ to save changes and enter to confirm.

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

Next, run update to download the latest package lists and get information on the newest versions of packages.

sudo apt-get update

Lastly, we can install PHP 7 with the following command (press ‘Y’ when prompted).

sudo apt-get install -t stretch php7.0

You can now check that PHP is install and it’s version by running:

php -v

You have the latest PHP 7 installed and running on Raspberry Pi!


11 thoughts on “Raspberry Pi3 and PHP 7

    1. Note: this method does not install the following modules:
      (I am sure there is a few more)

      As these are required for some applications (like phpmyadmin) it’s recommended you include them.

      make you bash in:
      sudo bash

      apt-get install php7.0-mbstring php7.0-mysql php7.0-curl -y

  1. Perfect!
    It works for me.
    I tried a lot of web page explained “How to install PHP on Raspberry PI” and they don’t work….

    This one is the best!

  2. In case of bad dependencies you better should try:

    apt-get install -t stretch php7.0-mbstring php7.0-mysql php7.0-curl -y


  3. following this I have broken packages when installing php7-mcrypt and php7-gd

    php7.0-gd : Depends: libgd3 (>= 2.1.1) but 2.1.0-5+deb8u11 is to be installed
    php7.0-mcrypt : Depends: libltdl7 (>= 2.4.6) but 2.4.2-1.11 is to be installed
    E: Unable to correct problems, you have held broken packages.

Leave a Reply

Your email address will not be published. Required fields are marked *