Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mig1098/ea4b739f507d702494c600e7ac8c97ce to your computer and use it in GitHub Desktop.
Save mig1098/ea4b739f507d702494c600e7ac8c97ce to your computer and use it in GitHub Desktop.
Install PHP from a source download to enable its interactive command-line shell interface on Linux.

PHP Interactive CLI Install

The following commands can be used to install PHP from source with an interactive command-line shell interface on Linux.

This interactive shell, (command: php -a) was the main reason I built from source to begin with. Now though I am able to play with the latest PHP versions as soon as they are available.

The commands below were written for and tested on the following system.

  • OS: Ubuntu 14.04.4 LTS (trusty)
  • Shell: GNU bash, version 4.3.11(1)-release (x86_64-pc-linux-gnu)

Requirements

The prerequisites for the PHP install are listed in the Download's INSTALL file. At a minimum you will need:

  • ANSI C complier
  • automake
  • autoconf
  • libtool
  • flex
  • bison

To get these required tools use the following command for your relevant system.

Debian / Ubuntu

apt-get install build-essential autoconf bison libtool flex

RHEL / CentOS

yum groupinstall "Development Tools"
yum install bison libtool flex

Dependent Libraries

Some PHP modules you might want will require certain libraries in order to build and install. If a missing dependency is found, it will be reported during the Configure or Install process.

To check ahead of time for the required dependencies or libraries needed to enabled certain modules, see the PHP Manual for that PHP module, class or extension.

For example:

To install all the libraries you might need you can use the following command for your relevant system. This however will include many libraries that may not be needed at all.

Debian / Ubuntu

apt-get build-dep php5

RHEL / CentOS

yum groupinstall "Development Libraries"

Download

Download from the official PHP Downloads page.

cd ~/Downloads
wget -O php-7.0.7.tar.gz http://php.net/get/php-7.0.7.tar.gz/from/this/mirror

Verfiy

Get the hash for the downloaded file.

sha256sum ./php-7.0.7.tar.gz

Compare with the hash shown on the official PHP Downloads page.

66282ff4a9f88fe9607d9574e15bf335885b964245591a1740adb3f79c514a67
66282ff4a9f88fe9607d9574e15bf335885b964245591a1740adb3f79c514a67  ./php-7.0.7.tar.gz

Extract

cd /usr/local/src
tar -xf ~/Downloads/php-7.0.7.tar.gz

Configure

The core PHP configure options are documented online. For a complete up-to-date list of configure options, run ./configure --help in your PHP source directory.

For an effective and useful interactive PHP cli (command-line interface) the most important configure options are:

  • --enable-cli
  • --disable-cgi
  • --with-readline

With these options you build a PHP command that works with the -a option to provide an interactive PHP command-line interface.

# Locate source.
cd /usr/local/src/php-7.0.7/

# Generate configuration options
autoconf

# Configure PHP build.
./configure \
  --prefix=/usr/local \
  --mandir=/usr/local/man \
  --with-config-file-path=/usr/local/etc \
  --with-config-file-scan-dir=/usr/local/etc/php.d \
  --disable-cgi \
  --enable-bcmath \
  --enable-cli \
  --enable-ftp \
  --enable-mbstring \
  --enable-pcntl \
  --enable-phar \
  --enable-shared \
  --enable-zip \
  --with-bz2 \
  --with-curl \
  --with-iconv \
  --with-mcrypt \
  --with-openssl \
  --with-pear \
  --with-readline \
  --with-zlib

Configure Errors

If during the configure process you run into errors, address the problem and run the command again.

As an example, say you get the following error from the ./configure command above:

configure: error: mcrypt.h not found. Please reinstall libmcrypt.
  1. Find the package containing libmcrypt development files.

    sudo apt-cache search libmcrypt
     libmcrypt-dev - De-/Encryption Library development files
     libmcrypt4 - De-/Encryption Library
     mcrypt - Replacement for old unix crypt(1)
    
  2. Install the required package.

    sudo apt-get install libmcrypt-dev
  3. Run your ./configure command again.

Repeat this process until all requirements are met and no errors are reported.

Build

Build from source then install the newly compiled PHP using make install.

# If you've changed configure options since last time,
# clean out everything from the previous build.
make clean

# Build from source.
make

# Test the new build.
make test

# Install the new build.
make install

Running the tests helps the PHP developers test their release on your system. Consider sending results of failed tests using the prompt provided on test completion. It's easy to do and helps us all with minimal effort.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment