Mcrypt on OS X for PHP 5.3.15

I’m trying to put together a web application in either Slim or Laravel. Unfortunately, both require (well, Slim only if you want to use encrypted cookies – which of course I do) mcrypt, which isn’t compiled into PHP on Mac. So my choices are either recompile PHP with mcrypt support on my laptop, or develop on the server, right?

Actually, there’s a third way. You can include mcrypt with PHP as a dynamic library, so no need to recompile and reinstall PHP on Mac. Which is nice, because Mac is not always very forgiving of user rebuilds of their installed applications and libraries.

The third way is still a bit tough, though. None of the online instructions that I found worked as stated, and even if they had the process is a bit involved. But this post came awfully damn close. So, basically just follow the instructions there. There was one little thing that was wrong for my system, though: at the end when it says to include the mcrypt library by referncing it at the end of your php.ini file, that didn’t actually work for me. That’s because the default php.ini file for Mac OS X comes with sections, and so on reading the file it will assume that your inclusion line belongs in whatever section is listed last. There is, as it happens, an mcrypt section, so if you include the library in there instead, it works.

For future reference, here’s the whole process.

(1) You may not have monkeyed with your PHP settings. If not, you likely don’t have a working php.ini file on your system. To create one, copy /etc/php.ini.default.

sudo cp /etc/php.ini.default /etc/php.ini


(2) Figure out which version of PHP is installed on your system:

php --version


(3) Grab the sourcecode for that version from http://php.net/releases/index.php. In my case, it was php-5.3.15.tar.bz2. Preferably save the downloaded tarball in some directory that you’ve made just for this purpose – maybe ~/Downloads/Mcrypttmp or something.

(4) In that same directory, grab libmcrypt. Here it’s a little murkier, but you apparently need libmcrypt and NOT mcrypt, and you’re kinda on your own to figure out which version you need. For OS X 10.7.5 users, it’s 2.5.8, and you can get it here.

(5) Now that you have both of those saved in … whatever your sandbox dir is (~/Downloads/Mcrypttmp for the purposes of instruction) … extract them both:

tar xjf libmcrypt-2.5.8.tar.bz2
tar xjf php-5.3.15.tar.bz2


(6) Compile and install libmcrypt:

cd libmcrypt

MACOSX_DEPLOYMENT_TARGET=10.7 CFLAGS='-O3
-fno-common -arch i386 -arch x86_64'
LDFLAGS='-O3 -arch i386 -arch x86_64'
CXXFLAGS='-O3 -fno-common -arch i386
-arch x86_64'  ./configure --disable-dependency-tracking

make -j6

sudo make install


(7) cd into the proper php extension dir

cd php-5.3.15/ext/mcrypt


(8) Run phpize

/usr/bin/phpize


(9) compile and install the extension

MACOSX_DEPLOYMENT_TARGET=10.7 CFLAGS='-O3
-fno-common -arch i386 -arch x86_64'
LDFLAGS='-O3 -arch i386 -arch x86_64'
CXXFLAGS='-O3 -fno-common -arch i386 -arch x86_64'
./configure
--with-php-config=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.7.sdk/usr/bin/php-config

make -j6

sudo make install


(10) Open your (possibly newly-created) php.ini file and add the following line in the mcrypt section

extension=mcrypt.so


Remember – it does no good to add this to the end of the file as suggested in all the instructionals I found. It really does need to go in the section marked [mcrypt]

(11) You will need to restart Apache to force a read of the php.ini file.

And … that should do it! Code away with Laravel!