Installing multiple versions of PHP from source

With the recent release of PHP 5.4-RC1, I wanted to give it a spin and make sure there weren’t any upcoming issues for CakePHP. I recently saw a great article from Derick Rethans on getting PHP setup from an SVN checkout. I found getting the shallow SVN checkout he used a bit of a pain. Thankfully, there is a github mirror of the PHP source code.

After cloning the php-src repo locally into ~/Sites/php/php-src, I started adapting Derick’s script for my own purposes. I don’t use a number of extensions that he had enabled, so I opted out of those. Something is not right with the iconv libraries available on Snow Leopard, so I had to disable iconv as well. I also have Postgres and MySQL installed through macports, so I need to update the paths for those. The script I ended up with looks like:

Show Plain Text
  1.  
  2. #!/bin/sh
  3.  
  4. VERSION=$1
  5. DEBUG=$2
  6. ZTS=$3
  7. THIRTYTWO=$4
  8. POSTFIX=
  9. EXTRA_FLAGS=
  10. PHP_DIR=/Users/markstory/Sites/php/php-src
  11.  
  12. if (test "${DEBUG}" = "nodebug"); then
  13.     POSTFIX="$POSTFIX-nodebug"
  14. else
  15.     EXTRA_FLAGS="$EXTRA_FLAGS --enable-debug"
  16. fi
  17.  
  18. if (test "${ZTS}" = "zts"); then
  19.     EXTRA_FLAGS="$EXTRA_FLAGS --enable-maintainer-zts"
  20.     POSTFIX="$POSTFIX-zts"
  21. fi
  22.  
  23. if (test "${THIRTYTWO}" = "32bit"); then
  24.     export CFLAGS="-m32"
  25.     POSTFIX="$POSTFIX-32bit"
  26. fi
  27.  
  28. SCRUBBED=`echo $VERSION | sed 's/[0-9.]*//'`
  29. if (test "${SCRUBBED}" = "dev"); then
  30.     BRANCH=`echo ${VERSION} | sed 's/dev//' | sed 's/\./_/g'`
  31.     cd $PHP_DIR
  32.     git checkout "PHP_$BRANCH"
  33. else
  34.     TAG=`echo ${VERSION} | sed 's/\./_/g'`
  35.     cd $PHP_DIR
  36.     git checkout "PHP_$TAG"
  37. fi
  38.  
  39. echo "Building ${VERSION}${POSTFIX} with ($EXTRA_FLAGS)"
  40.  
  41. make clean
  42. rm -rf configure
  43. ./vcsclean
  44. ./buildconf --force
  45.  
  46. if (test "${THIRTYTWO}" = "32bit"); then
  47.     OPTIONS="--disable-all"
  48. else
  49.     OPTIONS="--with-xpm-dir=/usr \
  50.         --with-apxs2=/opt/local/apache2/bin/apxs
  51.         --with-mysql=mysqlnd \
  52.         --enable-bcmath \
  53.         --enable-pcntl \
  54.         --enable-libxml \
  55.         --enable-dom \
  56.         --enable-memory-limit \
  57.         --enable-filter \
  58.         --enable-simplexml \
  59.         --enable-tokenizer \
  60.         --enable-debug \
  61.         --enable-spl \
  62.         --enable-json \
  63.         --enable-pdo \
  64.         --enable-exif \
  65.         --enable-mbstring \
  66.         --without-iconv \
  67.         --with-gmp \
  68.         --with-readline=/opt/local \
  69.         --with-openssl \
  70.         --with-curl \
  71.         --with-mysqli=mysqlnd \
  72.         --with-mcrypt \
  73.         --with-memcache=/usr/local \
  74.         --with-pdo-pgsql=/opt/local/lib/postgresql90/bin \
  75.         --with-pgsql=/opt/local/lib/postgresql90/bin \
  76.         --with-pdo-mysql=mysqlnd \
  77.         --with-pdo-sqlite \
  78.         --with-ctype \
  79.         --with-bz2=/opt/local \
  80.         --with-zlib \
  81.         --with-xsl"
  82. fi
  83.  
  84. ./configure \
  85. --prefix=/usr/local/php/${VERSION}${POSTFIX} ${EXTRA_FLAGS} ${OPTIONS}
  86.  
  87. make -j 5
  88. make install
  89.  

I put this script in ~/Sites/php. I didn’t really like the way Derick changed his path around. Instead I decided to add /usr/local/php/active to my PATH, and use a script to switch to one of the PHP versions I’ve installed. In ~/Sites/php/link.sh I put:

Show Plain Text
  1. #!/bin/sh
  2.  
  3. PHP_DIR=/usr/local/php
  4.  
  5. echo "Remove old link."
  6. rm $PHP_DIR/active
  7.  
  8. if [[ ! -d "$PHP_DIR/$1" ]]
  9. then
  10.     echo "Cant find that version of PHP"
  11.     exit 1
  12. fi
  13.  
  14. echo "Creating new link."
  15. ln -s "$1/bin" "$PHP_DIR/active"

After I’ve compiled the various versions of PHP I want to use, I can switch between them using ~/php/link.sh 5.4-dev. While it took a bit of work to get going, finally knowing how, and being able to re-build PHP from source is well worth it.

Comments

Well… how was Cake with 5.4? :-)

Ryan on 13/12/11

Ryan: It wasn’t all rainbows at the start, but things work great now :)

mark story on 21/12/11

You create the wrong tag names, here’s a fix:

if (test "${SCRUBBED}" = "dev"); then BRANCH=`echo ${VERSION} | sed 's/dev//'` cd $PHP_DIR git checkout "PHP-$BRANCH" else cd $PHP_DIR git checkout "php-$VERSION" fi

Basically you dont need to convert “.” to “_”.
See: https://github.com/php/php-src/releases

David Steinsland on 4/9/13

Have your say:

*
* You can use Textile markup, but be reasonable