If you have a need to install the mcrypt extension for PHP, for example if you need to use phpMyAdmin, then these instructions are for you.
Thankfully, it is becoming simpler to install PHP extensions than with previous versions of OS X.
Xcode
The Xcode package installs the necessary versions of tools like autoconf which is needed during the PHP extension compilation process. Make sure you have at least Xcode v4.1 installed; the install package ("Install Xcode") is available in your "Applications" folder.
If you are running Xcode 4.3 or above, please check you have the command line tools installed before proceeding.
libmcrypt
libmcrypt is the library that provides the encryption functions. We need to install this before building the PHP extension.
- Open up Terminal.app
- Download libmcrypt from sourceforge
- Unpack the archive somewhere
cd libmcrypt./configuremakesudo make install
PHP Extension
Once we have libmcrypt installed, we can build the PHP extension. One important point: even though PHP 5.3.8 is now the default since Mac OS X 10.7.3, attempting to compile with these sources results in the following error:
php-5.3.8/ext/mcrypt/mcrypt.c:283: error: ‘PHP_FE_END’ undeclared here (not in a function)
Therefore, we need to use PHP 5.3.6 sources which compiles fine and still works with 10.7.3.
- Download PHP 5.3.6 source
- Unpack the archive and
cd php-5.3.6/ext/mcrypt/ - phpize
You should see output similar to the following:
Configuring for: PHP Api Version: 20090626 Zend Module Api No: 20090626 Zend Extension Api No: 220090626
./configuremakesudo make install
On successful install, you will see the following message:
Installing shared extensions: /usr/lib/php/extensions/no-debug-non-zts-20090626/
To enable the module, include the following line in your /etc/php.ini file:
extension = mcrypt.so
Finally, restart your Apache server:
sudo apachectl restart
And that's it: you should now be able to utilise the functions, as detailed in the PHP mcrypt documentation. If you are needing a simple way of accessing these functions, check out my SimpleCrypt class.
