Decrypting Mcrypt on Mac OSX

A tutorial on the steps I took to install the Mcrypt PHP extension on Mac OSX Mavericks (10.9) which was needed for the 'hybridsessions' module on Silverstripe.

Published on Mar 6 2015 by Phil Kyriacou

Recently, I worked on a SilverStripe project using the hybridsessions module that required a PHP extension that I had never heard - mcrypt. I did some research and found that it's very useful tool, widely used in many web apps. Keeping it short, it’s basically a large array of cryptographic algorithms allowing developers to encrypt data.

Unfortunately the version of PHP on my Mac (Mavericks 10.9), didn’t come loaded with the extension, so I needed to install it. 

This guide takes you through the steps I took to successfully get it running. So how does one install mcrypt on their Mac OSX?

What You’ll Need:

  1. Terminal.
  2. 3+ years experience copying and pasting text.
  3. Patience. 

Step 1: Command Line Tools

Install xCode Command Line Tools by running the following command in Terminal:

xcode-select --install

A popup will appear asking you to install the tool. 

xcode ss
You don’t need the full Xcode app to get the Command Line Tools so just hit the install button. To verify that you’ve successfully installed the tool and the directory exists use:

xcode-select -p
/Library/Developer/CommandLineTools

Step 2: Download and Unzip

Next we’ll change directory to our home account and create a new folder called mcrypt.

cd ~ ; mkdir mcrypt ; cd mcrypt

Download the following files:

Move both files into the new ‘mcrypt’ directory we just created and expand them.

Step 3: Configuring libmcrypt

Go back to terminal and command into the libmcrypt folder we just expanded. 

 cd ~/mcrypt/libmcrypt-2.5.8

Type the following to configure libmcrypt.

./configure
make
sudo make install

Step 4: Installing Autoconf

To check if your system has autoconf use the following command.

autoconf --version

If you see an output displaying the version number along with other 'jibajaba' then autoconf is already installed and you can skip to the next step. If you get an output along the lines of ‘command not found error’, then perform the following to download and install it.

cd ~/mcrypt
curl -O http://ftp.gnu.org/gnu/autoconf/autoconf-latest.tar.gz
tar xvfz autoconf-latest.tar.gz
cd autoconf-2.69/
./configure
make
sudo make install

Step 5: Compiling Mcrypt

You should see an output similar to the following. The numbers will vary depending on the PHP version you’re using.

Configuring for:
PHP Api Version: 20121113
Zend Module Api No: 20121212
Zend Extension Api No: 220121212
./configure
make
sudo make install

After a successful install, you should see a similar message.

Installing shared extensions: /usr/lib/php/extensions/no-debug-non-zts-20100525/

Step 6: Enable the extension

To enable the extension, include the following line at the end of your /etc/php.ini file:

extension = mcrypt.so

If the file does not exist, then duplicate the php.ini.default file located in the same directory and change the write permissions.

sudo cp /etc/php.ini.default /etc/php.ini
sudo chmod u+w  /etc/php.ini

open the file and paste the above line. 

Step 7: Restart Apache 

Last step is to restart apache. Do this with the following command.

sudo apachectl restart

Step 8: Celebrate!

That’s it. Time to put on Holiday by Madonna and kick your feet up as you now (should) be able to utilise the mcrypt functions.

fire Bonus Round fire

I did run into a problem after applying the above steps. The issue was with Apache not starting in MAMP. If you are having the same issue, close MAMP and use the following command line to stop the Apache. 

sudo apachectl stop

Load MAMP back up and it should now be fixed.