For upcoming Drupal 8 projects I’ve had to update my installed version(s) of PHP. My development system is a System76 Ubuntu LTS laptop. In general I really enjoy the system and try to keep it in a “mainstream or stable state (I only install official packages or packages from PPA — I rarely build from hand). Since I use Ubuntu LTS my OS is currently at 12.04 which uses PHP 5.3.10-1ubuntu3.11 with Suhosin-Patch. This is somewhat dated now.
Since I want to keep a relatively stable system. I looked for a way to install PHP (which can be done by compiling the sourcecode yourself) while not affecting the default PHP managed and installed by Ubuntu’s default package manager APT-GET.
As of this writing the installed version of PHPBrew I am using is 1.13.1. From the PHPbrew homepage it can:
- Build php with different variants like PDO, mysql, sqlite, debug …etc.
- Compile apache php module and seperate them by different versions.
- Build and install php(s) in your home directory, so you don’t need root permission.
- Switch versions very easily and is integrated with bash/zsh shell.
- Automatic feature detection.
- Install & enable php extensions into current environment with ease.
- Install multiple php into system-wide environment.
PHPBrew allows you to install different versions of PHP within a hidden home directory folder, typically .phpbrew. Using PHPBrew you may also install Extensions for PHP. I’m currently finding this tool very useful as I work on current Drupal 7 projects — and begin to wet my feet with Drupal 8 development.
Installation and Setup
In order to install PHPBrew you can download the PHAR file and place it in your /usr/bin/ path:
curl -O https://raw.github.com/c9s/phpbrew/master/phpbrew chmod +x phpbrew sudo cp phpbrew /usr/bin/phpbrew
Then from your $home directory issue: phpbrew init
Example Usage Commands
From here you can install and use any PHP version you basically want and include Extensions with simple markup. For example on my local development laptop to install php 5.4.2 I issued the following:
# Install php 5.4.2 on Ubuntu 12.04 LTS $ phpbrew install php-5.4.2 +default +mysql +gettext +mcrypt \ +intl +iconv +ftp +exif +dba +openssl +soap \ +apxs2=/usr/bin/apxs2 -- --with-libdir=lib/x86_64-linux-gnu \ --with-gd=shared --enable-gd-native-ttf \ --with-jpeg-dir=/usr --with-png-dir=/usr --enable-wddx \ --with-mysql-sock=/var/run/mysqld/mysqld.sock
In order to fully get GD support using this setup I had to use the following snippet of code for PHPbrew:
phpbrew ext install gd \ -- --with-libdir=lib/i386-linux-gnu \ --with-gd=shared \ --enable-gd-native-ttf \ --with-jpeg-dir=/usr \ --with-png-dir=/usr
With the above 2 commands I have PHP 5.4.2 installed. You can view installed PHP versions using:
You can switch to another version of PHP using:
phpbrew switch <PHP-VERSION-NAME>
Over time phpbrew may get new features or bugfixes you can easily update phpbrew itself using:
You can install as many version(s) of PHP your project(s) require, new or old with bit-rot, you can safely remove a version of PHP with:
phpbrew remove <PHP-VERSION-NUMBER> # or phpbrew purge <PHP-VERSION-NUMBER> # this additionally removes config php files for the installed version.
Lastly, if things go terribly wrong for any reason you can go back to whatever the system is using for its default PHP installation using:
Since it all lives in your home directory you can safely delete the .phpbrew directory and remove these files from your system and leave you system using the factory default libraries!