Compile PHP 7 for Raspberry Pi (with memcached)

The PHP version that comes with Raspbian is the 5.4.45 at the time I’m writing this. Recently I’ve installed OwnCloud 8.2.2, which recommends PHP 5.6 or newer, so, I decided to compile a newer version by myself. Indeed I tried with a couple of newer versions: 5.6.18 and 7.0.3, both with php-fpm support.

After compiling, configuring and running both versions as separated php-fpm sockets, I runned this benchmark script to compare them, and these were the results:



It’s awesome, isn’t it? 🙂

So, I’m going to explain how to download, compile and install php-7.0.3 with the memcached extension for the Raspberry Pi. The instructions for php-5.6.18 are analogous, except for the version of memcached extension that you have to compile (below in the memcached section, simply use this source code instead of the php7 branch:

First, download the versions you want from here (in my case were 7.0.3 and 5.6.18):

Now, the quick recipe:

Installing dependencies

These were the dependences needed in my case. You may need to install additional packages. Take care of possible errors at compilation time, they use to be quite descriptive about what libraries are needed. If so, “apt-cache search ” is your friend 🙂

A workaround to fix a compilation error with the imap support:

If you are in Debian Jessie (Raspbian…) you’ll probably see the “gcc is unable to create an executable file” error when you try to compile anything. Well, that’s because Jessie comes with gcc-4.9, which seems to be broken (at least in my case), son I had to install gcc-4.8:


Compile and install php

Notice the –prefix and –with-config-file-path options below, and just set the directory name with your selected version:

If all was correct, then run the make command (if you have a Raspberry Pi 2, you should add the -j4 param to use all cores for the compilation):

It will take several minutes. After the process, run:

It will install php-7 under the /opt/php-7.0.3 path, so there is no conflicts with other possible php versions you may have installed.



From the directory you’ve just compiled, install the php.ini-production and sapi/fpm/init.d.php-fpm files as following, and move the *.conf.default to their .conf versions at the installation path:

Then edit the  /opt/php-7.0.3/etc/php-fpm.d/www.conf to search and uncomment/set the following options (the last one will be very necessary to configure nginx later on):

And that’s it. You can test it by executing “/opt/php-7.0.3/bin/php –version”, and run the php-fpm by executing:

Now to configure nginx to use php7, you should set the fastcgi_pass param to point to the new sock file. For example:

Then, restart nginx and voilà.


To download, compile and install the memcached extension, just execute the following:

Then edit the /opt/php-7.0.3/etc/php.ini file, look for the “Dynamic Extensions” section, and add this line to it:

For nginx, you’ll have to restart the php7-fpm and the nginx server. But before that you can test it by the command line with this:

Among other things, there should be a line like “memcached support => enabled”.

Troubleshooting: If you still not having memcached enabled, go to the php7 source directory and execute again “sudo make install”.

If everything was fine, restart services and that’s all:


Tagged , , . Bookmark the permalink.

Leave a Reply

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