I’ve missed having something like chruby for use with PHP. I am a huge
fan of Direnv. Since chruby is really not much more than setting your
$PATH
, I figured I could do the same for PHP with Direnv.
I primarily use macOS and Homebrew. The default php
formula installs the
latest PHP version. I also use shivammathur/php
taps to install older
verisons.
With Homebrew’s setup, you really just need to set $PATH
(and optionally
$MANPATH
to the right locations for everything to Just Work™.
To accomplish that, I setup the following function in ~/.direnvrc
:
In each PHP project where I want a custom PHP version, just add the following
to .envrc
in the project root:
To enable it run (from the project root):
Check that you see the right PHP with:
You can also check that man pages work properly:
Scroll to the bottom and make sure the version reported matches the expected PHP version you configured.
Homebrew takes care to setup distinct versioned php.ini
files for you. But,
if you need to set a custom one, you could also define $PHPRC
. For example,
if you had config/php/php.ini
in the root of your project, you could
configure it with:
Note: The $PHPRC
variable should point to a folder, not the full path to
php.ini
.
I’ve also used a similar approach on Linux servers. On our dev server at work,
we maintain apps with a few different PHP versions. They are installed as
php
for the default PHP 7.4 binary, then we have php8
and php72
, etc.
To make this work with Direnv, I just created a separate directory tree and symlinked each PHP binary and accompanying commands.
Besides php
, you also have these commands in /usr/bin
or similar:
- pear
- peardev
- pecl
- phar
- phar.phar
- php
- php-cgi
- php-config
- phpdbg
- phpize
There are commands in /usr/sbin
(currently just the one):
- php-fpm
How you set these up will depend on your OS and package manager, but a rough idea is:
Then, in the project root, I just add the following to .envrc
: