If you are a PHP developer working with Laravel, Magento, CakePHP or other PHP frameworks and you develop on a Mac Mavericks environment than you have likely encountered the dreaded error:
REQUIRED MCRYPT EXTENSION
This is the worst error I have ever gotten. I have wasted hours searching the web for help on this and there is little help to be given. Most of the guides are outdated. To be honest, in 2014 I don’t know why it is still so complicated to install such a trivial PHP extension.
Lucky for you, I have discovered the easiest way to install mcrypt php so you can get onto what you really want to do, develop awesome web applications.
Take a quick mental inventory to make sure your computer is compatible. But for the most part, if you are using a Mac built in the last 3 years then this should work just fine.
- PHP 5.6/5.5/5.4/5.3 installed on your computer already
(If you don’t know, then run which php in your terminal)
- Mac OSX 10.6/10.7/10.8/10.9/10.10
(also known as Snow Leopard/Lion/Mountain Lion/Mavericks/Yosemite)
(This is found in Applications/Utilities/Terminal)
Alright, with that down, pretty much anyone running on a modern mac qualifies. Let’s start the magic.
One Line Installation
Copy and Paste the following line into your terminal and then click “enter”.
curl -s http://php-osx.liip.ch/install.sh | bash -s 5.6
The install will ask you for a password. This is where you type your user or admin password (if you are a developer and own this computer, then this is the same password that you use to verify any install or to log in). Please keep in mind you won’t see any feedback that you are typing anything. There are no dots or asterisks to indicate you have typed anything. Just type it and click enter.
This will install all sorts of files. If you want to know whats happening, this script is completely re-installing PHP onto your system into your local user directory. Don’t worry though, it is not overwriting your system PHP which Apple installed. So that remains untouched. I should also add that we are installing the current stable version of PHP, which is 5.6. This is a significant upgrade from the 5.3 or 5.4 version that Apple installed with your operating system.
When it is complete, you will see the last few lines indicating that Apache was restarted. This is a good sign. You might get a warning that says it could not find a domain, so it used [your computer name].local instead. This is also completely normal.
Congratulations you have a brand new version of PHP for your development and you have a ton of useful extension you will want to have, but most importantly, you now have mcrypt installed!
Although you have the new PHP and Mcrypt installed, it doesn’t mean your system knows that it exists. So we need to do a couple last (still easy) things in order to actually take advantage of what we just installed.
We are using the command line, but don’t panic. Just follow my instructions carefully.
We need to tell the system where you installed the new PHP. There is a hidden configuration file which will tell your computer this information. It is called .profile. The dot at the beginning means that it is hidden. This is why we need to use terminal, because it still allows us to edit hidden files. Type the following lines into your terminal to edit this configuration file.
Nano is the name of the text editor built into terminal. You will see either a blank screen or possibly one or two lines of $PATH files. If you have some lines already, then just navigate (using arrow keys) down to a new line under the existing lines. If your file is blank, then that is completely ok. Just work from the first line. Now, add this line into your file.
Perfect. Make sure that this is on its own line. Now that it’s added it is time to save this document you have been editing. Because we are using a text editor built into your terminal, you have no access to your mouse to save the file. Instead you will use some shortcuts to save and quit. The bottom of your document will have all of your shortcuts and menus. You will see that many of them are prefixed with a ^ character. This is the “control” key on your mac (usually the bottom far-left key on desktops or the bottom second to the most left on laptops). So to save, we actually just need to Exit. So as you can see, we should click (control + X).
Simple enough, now you will need to indicate that you want to overwrite the existing file. The next two images illustrate this. You will first type “Y” and then it will ask you if you want to change the filename. We don’t want to change the filename, so just press enter and then you will be sent back to the terminal like normal.
Note: ignore the first “export PATH” in my example image here. The first line in my document is for Ruby on Rails developers. It essentially does the same thing we are doing here, but for Ruby on Rails. This was already in my .profile when I added my PHP mcrypt path that this tutorial is about. Like I mentioned it is ok if your .profile was empty when you launched it. It’s possible that your file will only have one line (my second line) when you save it.
The last thing you need to do after you save this file, is restart your terminal for the changes to take effect. Pressing the red bubble in the corner of the window will not work though. You actually need to fully quit the program. Either go up to Terminal > Quit in the menu, or press Command + Q.
Now launch the terminal like you did before and you will now have access to mcrypt. I tested this by going into a laravel installation folder and typing php artisan serve which launches the laravel server which normally requires mcrypt. If it launches without the error than you know it worked! Chances are you were trying to do something and got the error message before you started seeking out this tutorial. So go ahead and do that now. If you have trouble, comment below and I will do my best to help.
[Tweet “I just installed mcrypt php extension on my mac in one line of code”]
Uninstalling (if needed)
If you decide you want to undo all this awesomeness. Then you should probably re-think your life. Because you don’t fully appreciate how much time this script saves you. But for those of you wanting to uninstall despite my desperate urges otherwise, then you can uninstall what we just did by following this link.
More Information on this Script
If you want all the details about this script you can get them from the PHP-OSX script homepage. I simplified their steps quite a bit on my post right here. But if you are needing more information that please go direct to the source.