As with any PHP extension, the mcrypt extension can be a bit tricky to install. This guide is meant to simplify this task, and most of the work will be done in the Terminal. Before beginning, please ensure that you have Xcode installed, which can be found in the App Store as well as the command line tools. The latter can be accomplished by entering xcode-select –install in the Terminal.
Setting up a Working Directory
Before beginning, let us first create a working directory for this tutorial. Enter the below in your Terminal (/Applications/Utilities/Terminal).
cd ~ && mkdir mcrypt && cd mcrypt
Because we are going to compile the mcrypt extension, autoconf needs to be installed on your system. If it is already installed, then simply ignore this step. If you are unsure, enter which autoconf into the Terminal. If the output is /usr/local/bin/autoconf (or perhaps similar), then you may skip this step.
To install autoconf, simply execute the below commands in the Terminal. Note that the name of the autoconf folder depends on the latest version of autoconf at the time of the download – version 2.69 as of this writing.
curl -O http://ftp.gnu.org/gnu/autoconf/autoconf-latest.tar.gz tar -zxvf autoconf-latest.tar.gz cd autoconf-2.69 ./configure make sudo make install
Now that we have installed the necessary tools, we are ready to begin our work on installing the mcrypt extension. First, download libmcrypt and PHP from the addresses below. Download the PHP version that matches the one you have installed on your system. To determine your PHP version, simply type php -v in your Terminal.
Once done, run the following commands to move the archives to our working directory, expand them and clean up the directory. Note that the file names may differ if your PHP version is different.
mv ~/Downloads/libmcrypt-2.5.8.tar.gz ~/mcrypt mv ~/Downloads/php-5.4.24.tar.gz ~/mcrypt tar -zxvf libmcrypt-2.5.8.tar.gz tar -zxvf php-5.4.24.tar.gz rm *.tar.gz
Now it is time to compile libmcrypt.
cd libmcrypt-2.5.8 ./configure make sudo make install
Do not worry about any warnings displayed during this process, and simply continue unless errors occur. Now we will compile the PHP mcrypt extension.
cd ../php-5.4.24/ext/mcrypt phpize ./configure make sudo make install
Enabling the mcrypt PHP extension
Assuming that everything went well so far, let us enable the mcrypt extension in PHP by adding the following to the /etc/php.ini file.
Note that if you do not have a /etc/php.ini file, then you need to copy a default configuration file to this location with the following command. Otherwise you can simply move on.
sudo cp /etc/php.ini.default /etc/php.ini
You also need to make sure that the following line (or similar) is present in your configuration file, such that extensions are loaded correctly from the folder in which the mcrypt extension has been added to.
extension_dir = "/usr/lib/php/extensions/no-debug-non-zts-20100525"
The name of the directory will depend on your particular version of OS X. To find our which one to use, enter the following commands and replace the folder name with the result.
cd /usr/lib/php/extensions ls
Last but not least, simply restart the Apache web server (chances are that you are using Apache), and you should be good to go!
sudo apachectl restart
And, to clean up our working directory, issue the following command (or simply delete it in the Finder).
rm -rf ~/mcrypt
I hope that this guide was helpful. Thank you for reading!