Hero image for: Manage PHP versions with Phpbrew.

Manage PHP versions with Phpbrew.

By Eduardo García ● CTO | October 17th, 2014

If you are a PHP developer, I’m sure you have been in a situation where you have to maintain several projects and is possible some of them require a different version of PHP.

To resolve this situation described above nowadays the common recommendation is to install a virtual machine like Vagrant or Docker and off course works but if you are not a big fan of Virtual Machines like me and especially to only change the PHP version I have good news you can resolve your problems using the project Phpbrew.

Let me show you how to install and use.

Install Phpbrew.


If you already have an environment to dev in PHP is probable you can skip this section.

The following utils are required.

  • automake
  • autoconf
  • curl
  • pcre
  • re2c
  • mhash
  • libtool
  • icu4c
  • gettext
  • jpeg
  • libxml2
  • mcrypt
  • gmp
  • libevent

If you are in Mac OS X and you use brew, you run the following command.

$ brew install automake autoconf curl pcre re2c mhash libtool icu4c\
 gettext jpeg libxml2 mcrypt gmp libevent
$ brew link icu4c

Download, Install and Initialize.

To install you just need to execute the following commands

$ curl -L -O https://github.com/phpbrew/phpbrew/raw/master/phpbrew
$ chmod +x phpbrew
$ sudo mv phpbrew /usr/bin/phpbrew

Now you must initialize your system executing the following command.

$ phpbrew init

Then add these lines to your .bashrc, .bash_profile or .zshrc file

source ~/.phpbrew/bashrc

Phpbrew commands

List PHP versions available to be installed

$ phpbrew known
5.6:  5.6.1, 5.6.0 ...
5.5:  5.5.17, 5.5.16, 5.5.15, 5.5.14, 5.5.13, 5.5.12, 5.5.11, 5.5.10 ...
5.4:  5.4.33, 5.4.32, 5.4.31, 5.4.30, 5.4.29, 5.4.28, 5.4.27, 5.4.26 ...
5.3:  5.3.29, 5.3.28, 5.3.27, 5.3.26, 5.3.25, 5.3.24, 5.3.23, 5.3.22 ...

Install a PHP specific version of PHP

$ phpbrew install 5.5.10

You need wait until Phpbrew configures your system.

Install a PHP with specific libraries or virtual variant.

$ sudo phpbrew install 5.5.10 +mysql +pdo +ctype +mhash +hash +iconv\
 +json +iconv +gd +apxs2=/usr/sbin/apxs -- --with-gd --enable-gd-natf\
 --with-jpeg-dir --with-png-dir

$ sudo phpbrew install 5.5.10 +default +mysql +hash +iconv +gd\
 +apxs2=/usr/sbin/apxs -- --with-gd --enable-gd-natf --with-jpeg-dir\

If you don’t know where is located the directory for your apache apxs, you can execute the following command.

$ whereis apxs

The previous install works to use with Drupal 8 and Spress.

Get a list of available libraries or variants

$ phpbrew variants
  all, apxs2, bcmath, bz2, calendar, cgi, cli, ctype, curl, dba, debug,
  dom,embed, exif, fileinfo, filter, fpm, ftp, gcov, gd, gettext, hash,
  iconv, icu, imap, inifile, inline, intl, ipc, ipv6, json, kerberos,
  libgcc, maintainer, mbregex, mbstring, mcrypt, mhash, mysql,
  opcache, openssl, pcntl, pcre, pdo, pgsql, phar, phpdbg, posix,
  readline, session, soap, sockets, sqlite, static, tidy, tokenizer,
  wddx, xml, xml_all, xmlrpc, zip, zlib

Virtual variants:
  dbs: sqlite, mysql, pgsql, pdo
  mb: mbstring, mbregex
  default: bcmath, bz2, calendar, cli, ctype, dom, fileinfo, filter, ipc,
  json, mbregex, mbstring, mhash, mcrypt, pcntl, pcre, pdo, phar, posix,
  readline, sockets, tokenizer, xml, curl, zip

Using variants to build PHP:

  phpbrew install php-5.3.10 +default
  phpbrew install php-5.3.10 +mysql +pdo
  phpbrew install php-5.3.10 +mysql +pdo +apxs2
  phpbrew install php-5.3.10 +mysql +pdo +apxs2=/usr/bin/apxs2

List PHP version versions available in your system

$ phpbrew list
Installed versions:
* php-5.5.10

Change PHP Version

$ phpbrew switch php-5.5.10

Disable Phpbrew

$ phpbrew off
phpbrew is turned off.

After disabling Phpbrew, you can execute the following command to check the current status of the system

$ phpbrew list
Installed versions:
* (system)

As you can see the PHP version available if the standard PHP version your system has, of course, you enable another version again using the switch command explained above.

Enable PHPbrew version in Apache.

After switching a version you must to inform to apache about the new version of PHP apache editing the apache config file /etc/apache2/httpd.conf or /private/etc/apache2/httpd.conf as you can see in the following snippet.

LoadModule php5_module libexec/apache2/libphp5.so #Disabled

LoadModule hfsapplemodule libexec/apache2/modhfsapple.so LoadModule php5_module libexec/apache2/libphp5.5.10.so

As you can imagine is required to restart the Apache Server.

Update Phpbrew

$ phpbrew self-update

Modify your PHP info.

Is so probably after enabling a new version of PHP you lost your previous setup, and you need to apply that tweaks and tricks in your new PHP.ini to determine where are located you need to execute the next command.

$ phpbrew info | grep php.ini
Loaded Configuration File => ~/.phpbrew/php/php-5.5.10/etc/php.ini

Remove the grep portion to get the whole information.

Related Posts