open core »

PHP mcrypt in Snow Leopard with Homebrew

Update: This also works on OS X Lion as it is. But just for the sake of correctness you can replace every occurrence of 10.6 to 10.7 in MACOSX_DEPLOYMENT_TARGET.

If you are using Homebrew as your OSX package manager, chances are that you are using the default PHP that comes with Snow Leopard. This PHP installation does not have the mcrypt extension, so if you are trying to use something like PHPMyAdmin, you’ll get an error message saying that mcrypt is not enabled in php.ini.

I googled the answer and found little info about it. I found this post that it’s pretty complete but it compiles mcrypt manually, which is not Homebrew way of doing it. So the purpose of this post is to gather the info for those who are using Homebrew and the default PHP.

First you have to install mcrypt with Homebrew, but the current Formula for mcrypt is not suitable for those on Snow Leopard, so let’s change it. Open the Formula in your favorite code editor:

$ sudo vim /usr/local/Library/Formula/mcrypt.rb

Note that you only need the sudo if you did not chown your /usr/local directory.

Now lets change the install method to this:

def install
  system "MACOSX_DEPLOYMENT_TARGET=10.6 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 --prefix=#{prefix} --mandir=#{man}"
  system "make -j6"
  system "make install"
end

Save and run it:

$ brew install mcrypt

Just wait some seconds and you’re done with this step. After this you might want to undo the changes in the Formula file to avoid git conflicts when updating Homebrew.

Second, you have to download the PHP 5.3 source to compile the mcrypt PHP extension and install it. You can download the source from this link.

Extract the tarball to a directory of your choice, it can be ~/Desktop and then:

$ cd ~/Desktop/php-5.3.0/ext/mcrypt

And type:

$ phpize

Again wait some seconds and when its complete type:

$ MACOSX_DEPLOYMENT_TARGET=10.6 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=/Developer/SDKs/MacOSX10.6.sdk/usr/bin/php-config; make -j6;sudo make install

Allot will happen and hopefully you won’t see any errors.

Now finally you just need to edit the /etc/php.ini file to enable the extension you just compiled and installed. If you don’t have a /etc/php.ini file you need to copy the default config file:

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

And let’s edit it:

$ sudo vim /etc/php.ini

Find the enable_dl setting and remove the ; in front of the line (if any) and change it to On. It should look like this:

enable_dl = On

Now find the Dynamic Extensions section of the file and add this line at the end of that section:

extension=mcrypt.so

Restart the Apache server and you’re done. Happy coding!