How to install PHP 5.6 or newer on Raspbian

Since no repo has an ARM version of PHP >5.4, compiling and installing by hand it's all we have.
Published on April 18, 2016 in How-tos
Read time: 1 min
Tags:

Raspberry Pi is an awesome piece of hardware so you might need PHP on it for whatever kind of project.

Unfortunately the latest PHP version in official repo is (the no longer maintained) 5.4 and third-party repo do not support the ARM architecture. We have only one option left: download source, compile and install.

I’ve made a script for that and made it available as a gist.

#!/bin/bash
# credits: http://stackoverflow.com/questions/31280912/how-to-install-php-5-6-on-raspbian-wheezy
### VARIABLES ###
# type here the specific php 5.x version you want to install
PHP_VERSION="5.6.20"
### SCRIPT ###
# check if script is running as root
if [[ $EUID -ne 0 ]]; then
echo "Sorry, you have to be root."
echo "Try again using sudo"
exit 1
else
echo "Running as root..."
fi
# getting real number of cores
# CORES=$(cat /proc/cpuinfo | grep Hardware | wc -l)
# Check PHP version before start
# latest version in repo should be 5.4, which is no longer supported.
echo "Currently installed PHP version is..."
php -v
# working in temporary dir
mkdir -p /tmp/php_install
cd /tmp/php_install
# Get the PHP source
# You can find the latest version number on the PHP download page: http://php.net/downloads.php
# Change `nl1` to your nearest mirror. Find the mirror list here: http://php.net/mirrors.php.
wget http://nl1.php.net/distributions/php-$PHP_VERSION.tar.bz2
# Unpack
tar -xvjf php-$PHP_VERSION.tar.bz2
cd php-$PHP_VERSION
apt-get update
apt-get install libxml2-dev
./configure
# getting cpu architecture
RPI_HW=$(cat /proc/cpuinfo | grep Hardware | awk '{print $3}' | head -1)
if [ "$RPI_HW" = "BCM2709" ]; then
# if on RPi 2 or RPi 3 use 4 cores.
make -j4
else
# if on RPi 1 or unrecognized cpu (future Pis?) use 1 core
make
fi
make install
# Check PHP version
echo "now printing the newly installed PHP version..."
php -v

How to use the script

cd $HOME
wget -O install_php.sh https://gist.githubusercontent.com/pirafrank/2bce62d541af195aaea865d5102f1c09/raw/f8528fc6616663aa443b41059ae91d30794b3094/install_latest_php_raspberrypi.sh
chmod +x install_php.sh

Hope it helps!

Thanks for reading.



Comments

Got some words you want to share? Tell me!