Skip to content

Instantly share code, notes, and snippets.

@sergeyklay
Last active June 25, 2024 04:37
Show Gist options
  • Save sergeyklay/a29224497375562ea26ea16b94a8cd37 to your computer and use it in GitHub Desktop.
Save sergeyklay/a29224497375562ea26ea16b94a8cd37 to your computer and use it in GitHub Desktop.
Multiple PHP versions using phpenv and php-build

Multiple PHP versions using phpenv and php-build

Install dependecies

Debian/Ubuntu users

sudo apt install \
  autoconf \
  bison \
  build-essential \
  libbz2-dev \
  libc-client-dev \
  libcurl4-gnutls-dev \
  libedit-dev \
  libgmp-dev \
  libjpeg-dev \
  libkrb5-dev \
  libmagickcore-dev \
  libmagickwand-dev \
  libmcrypt-dev \
  libmemcached-dev \
  libpng-dev \
  libreadline-dev \
  libonig-dev \
  libpq-dev \
  libssl-dev \
  libtidy-dev \
  libwebp-dev \
  libxml2-dev \
  libxpm-dev \
  libxslt-dev \
  libyaml-dev \
  libzip-dev \
  linux-libc-dev \
  pkg-config \
  re2c

macOS users

brew install \
  autoconf \
  automake \
  bison \
  bzip2 \
  curl \
  gettext \
  gmp \
  icu4c \
  jpeg \
  krb5 \
  imagemagick \
  libedit \
  libiconv \
  libmcrypt \
  libmemcached \
  libpng \
  libxml2 \
  libxslt \
  libzip \
  libtool \
  make \
  openssl \
  re2c \
  readline \
  tidy-html5 \
  zlib

Installation

Install phpenv

git clone git@github.com:phpenv/phpenv.git ~/.phpenv

Then follow the instruction from phpenv repo.

Install php-build

git clone git@github.com:php-build/php-build.git ~/.phpenv/plugins/php-build

Install config-add plugin

git clone git@github.com:sergeyklay/phpenv-config-add.git ~/.phpenv/plugins/phpenv-config-add

Install pear-setup plugin

git clone git@github.com:sergeyklay/phpenv-pear-setup.git ~/.phpenv/plugins/phpenv-pear-setup

Usage

export PHP_BUILD_ZTS_ENABLE=on
export PHP_BUILD_XDEBUG_ENABLE=off
export PHP_BUILD_EXTRA_MAKE_ARGUMENTS=-j"$(getconf _NPROCESSORS_ONLN)"
export PHP_BUILD_TMPDIR=~/src/php
mkdir -p $PHP_BUILD_TMPDIR

php-build --ini development 7.4.0 $(phpenv root)/versions/7.4.0-zts-debug/

Example of script to print install instructions for multiple PHP versions:

for v in 7.0.33 7.1.30 7.2.20 7.3.7 7.4.0; do
  for z in on off; do
    for d in --enable-debug --disable-debug; do
      [ "$z" = "on" ]             && zts=zts   || zts=nts
      [ "$d" = "--enable-debug" ] && dbg=debug || dbg=release
      echo \
        PHP_BUILD_ZTS_ENABLE=$z \
        PHP_BUILD_XDEBUG_ENABLE=off \
        PHP_BUILD_EXTRA_MAKE_ARGUMENTS=-j"$(getconf _NPROCESSORS_ONLN)" \
        PHP_BUILD_CONFIGURE_OPTS="\"--disable-fpm --disable-phpdbg $d\"" \
        php-build --ini development $v $(phpenv root)/versions/$v-$zts-$dbg/
    done
  done
done

This will print lines like this:

PHP_BUILD_ZTS_ENABLE=on \
    PHP_BUILD_XDEBUG_ENABLE=off \
    PHP_BUILD_EXTRA_MAKE_ARGUMENTS=-j20 \
    PHP_BUILD_CONFIGURE_OPTS="--disable-fpm --disable-phpdbg --enable-debug" \
    php-build --ini development 7.4.0 $(phpenv root)/versions/7.4.0-zts-debug/

Feel free to disable any extension using PHP_BUILD_CONFIGURE_OPTS variable. For examle to disabe gd:

- PHP_BUILD_CONFIGURE_OPTS="--disable-fpm --disable-phpdbg --enable-debug"
+ PHP_BUILD_CONFIGURE_OPTS="--disable-fpm --disable-phpdbg --disable-gd --enable-debug"

Notes for macOS

macOS users will need to create a symlink for openssl and customize PHP_BUILD_CONFIGURE_OPTS. For example:

Creating symlinks:

cd /usr/local/include
ln -s ../opt/openssl/include/openssl .

Customizing PHP_BUILD_CONFIGURE_OPTS:

export PHP_BUILD_CONFIGURE_OPTS="\
  --disable-fpm \
  --disable-phpdbg \
  --enable-debug \
  --with-bz2=$(brew --prefix bzip2) \
  --with-curl=$(brew --prefix curl) \
  --with-gettext=$(brew --prefix gettext) \
  --with-gmp=$(brew --prefix gmp) \
  --with-iconv=$(brew --prefix libiconv) \
  --with-icu-dir=$(brew --prefix icu4c) \
  --with-jpeg-dir=$(brew --prefix jpeg) \
  --with-libedit=$(brew --prefix libedit) \
  --with-libxml-dir=$(brew --prefix libxml2) \
  --with-libzip=$(brew --prefix libzip)
  --with-mcrypt=$(brew --prefix libmcrypt) \
  --with-png-dir=$(brew --prefix libpng) \
  --with-readline=$(brew --prefix readline) \
  --with-tidy=$(brew --prefix tidy-html5) \
  --with-xsl=$(brew --prefix libxslt) \
  --with-zlib=$(brew --prefix zlib)" \
  --with-kerberos

For PHP 7.0.* you'll need buffio.h:

ln -s  $(brew --prefix tidy-html5)/include/tidybuffio.h $(brew --prefix tidy-html5)/include/buffio.h

For PHP >= 7.4 you'll need to specify library path for the folowing packages:

  • openssl
  • icu4c
  • libxml2
PKG_CONFIG_PATH=""
for pkg in openssl icu4c libxml2; do
    PKG_CONFIG_PATH="${PKG_CONFIG_PATH}:$(brew --prefix $pkg)/lib/pkgconfig"
done
export PKG_CONFIG_PATH="${PKG_CONFIG_PATH}"

export KERBEROS_LIBS="-lkrb5"
export KERBEROS_CFLAGS=" "
export EDIT_LIBS="-ledit"
export EDIT_CFLAGS=" "

Default configuration

cat <<EOT >> $(phpenv prefix)/etc/conf.d/999-defaults.ini
memory_limit=-1
variables_order=EGPCS
opcache.enable_cli=1
error_reporting=-1
display_errors=1
display_startup_errors=1
date.timezone=UTC
EOT

Install extensions

zephir_parser

git clone https://github.com/phalcon/php-zephir-parser.git /tmp/zephir-parser
cd /tmp/zephir-parser

phpenv local 7.4.0-zts-debug && phpenv rehash
phpize
./configure --with-php-config=$(phpenv which php-config) --enable-zephir-parser
make
make install

echo extension=zephir_parser.so > $(phpenv prefix)/etc/conf.d/zephir_parser.ini

gmp

cd $PHP_BUILD_TMPDIR/source/7.4.0/ext/gmp

phpenv local 7.4.0-zts-debug && phpenv rehash
phpize
./configure
make
make install

echo extension=gmp.so > "$(phpenv prefix)/etc/conf.d/gmp.ini"

PECL extensions

phpenv local 7.4.0-zts-debug && phpenv rehash
phpenv pear-setup

export PHP_PEAR_PHP_BIN=$(phpenv which php)

printf "\n" | pecl install --force igbinary 1> /dev/null
printf "\n" | pecl install --force imagick 1> /dev/null
printf "\n" | pecl install --force yaml 1> /dev/null
printf "\n" | pecl install --force msgpack 1> /dev/null
printf "\n" | pecl install --force memcached 1> /dev/null
printf "\n" | pecl install --force psr 1> /dev/null
printf "\n" | pecl install --force apcu_bc 1> /dev/null
@niden
Copy link

niden commented May 3, 2020

Linux mint bionic

  • libedit cannot be found - > use libedit-dev
  • install libonig-dev

@sergeyklay
Copy link
Author

Added

@niden
Copy link

niden commented May 4, 2020

In the example of the command to run

PHP_BUILD_CONFIGURE_OPTS="--disable-fpm --disable-phpdbg --enable-debug" \
    php-build --ini development 7.4.0 /home/USER/.phpenv/versions/7.4.0-zts-debug/

Note the USER needs to be $USER

@sergeyklay
Copy link
Author

Thank you, fixed

@gweecl
Copy link

gweecl commented Jun 9, 2020

I am trying to install php 7.1.33 on MacOS with LDAP:
--with-ldap=$(brew --prefix openldap)

But I am getting this error:

configure: error: Don't know how to define struct flock on this system, set --enable-opcache=no

Any idea on how to install LDAP extension?

@Pictor13
Copy link

Pictor13 commented Oct 6, 2022

On Ubuntu 22.04, when trying to install php-8.x, it was required to install also libsqlite3-dev via apt.
Apart for that, thank you for the gist and the list of dependencies!

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