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.
System Requirements
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) - Terminal.app
(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!
Final Touches
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 ~/.profile
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.
export PATH=/usr/local/php5/bin:$PATH
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.
After installing this way, phpinfo shows the correct version of php and that mcrypt is part of it, yet Laravel tells me that mcrypt is required.
If it is still saying that you need mcrypt, then try typing this:
source ~/.profile
This will reload your .profile file which sort of kicks the mcrypt back into gear again. If this ends up solving the problem, then that means your .profile is not being loaded when terminal is launched. Some system settings might need to be changed to make sure the .profile is loaded each time.
Let me know if that works.
Hey, still get the error “Mcrypt PHP extension requried”. Also if I type . source ~/.profile I get an error no such file… But I can see the .profil file in my finder and I am in the right directory.
Saved my day. Thanks a lot
it worked after trying > source ~/.profile
For those of you using iterm, you have to modify your ~/.bash_profile instead and source that.
Thank you so much! Best solution without installing Xcode!
Yep it’s definitely the best way. But don’t feel bad about downloading Xcode. You’ll need the command line tools from Xcode for other programming needs. They basically prep your Mac for programming. So you would want those installed anyway.
I needed mCrypt to install laravel through composer. After following your instructions to install mCrypt, laravel works when I use “php artisan serve”; however, I’m wanting to use MAMP as my server. How do I point mCrypt to use MAMP’s php instead of where it was installed?
I personally have not had a lot of success running Laravel inside of MAMP. What happens is that you need composer which is installed in your user root. MAMP runs in a sandbox, isolated from the rest of your system. This is usually a good thing, but gets tricky with all of Laravel’s dependencies.
Thats just my opinion. But If you want to still run inside of MAMP, the good news is that the newest version of MAMP will install mcrypt by default. Just take a look for yourself. Boot up MAMP and on the MAMP homepage click the PHPinfo at the top menu. Do a search
(ctrl + f)
for mcrypt and you’ll find that its installed most likely. If it isn’t then I suggest you uninstall MAMP and reinstall it to get the latest version in there.Use this for reference on how to find it:
Wow! Amazing it’s work!! Thanks !!!!!
Amazing. Works like a charm. Thanks.
Wow, this is amazing. Thank you.
Great article but it didn’t have evyirtheng-I didn’t find the kitchen sink!
Thanks a lot!
This worked beautifully.
I’m so happy to move on from this nightmare!
cheers to you!
And good luck to everyone!
Awesome! I had the same problem which is why I posted this. I’m glad it worked so well for you.
Hey,
I don’t know why,
but since I tryed installing homestead on my machine to run my projects in a VM
it seems like it doesnt work anymore…
I tryed re-installing but I prompt me that the package is already installed… Now when i run composer create-project laravel/larevel …
I just get mcrypt errors again..
Besides changing my paths using sudo vi etc/paths for homestead
I have no idea what creates this confusion 🙁
Bluh I hate how setting up homestead is always a painful experience.
This trick will install mcrypt onto your system. But if you’re running a VM then you are running inside an isolated environment, which will not utilize your system mcrypt it will need to install mcrypt itself.
So you could try to just run php artisan serve which uses your system components and the see if you still get the mcrypt error.
Also it’s possible you need to re-source your profile to load the mcrypt again.
Type:
source ~.profile
This will re-load your .profile
This is a most useful coioirbutntn to the debate
Thanks dude,
yeah, running a composer install in my VM will fix the problem,
however, since i edited my path to access the homestead CLI it looks like its not seeing mcrypt anymore , maybe just not using the right php.. idk to be honnest.
is there a way to verify if something changed ?
which php :
/usr/bin/php
echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:~/.composer/vendor/bin
nano ~/.profile
export PATH=/usr/local/php5/bin:$PATH
maybe you see something wrong here ?
Honestly , I am pretty new at this ‘make sure its in your path ‘ stuff so I must have done something wrong.
but i works alright now even tho it is not perfect haha.
I have the same problem..
Try to reboot terminal and then run it again. The only other thing I can think of is that you’ve installed something else that is taking priority of selecting your php install. For example maybe Homebrew? Anyway you can try nano ~/.bash_profile which is an alternative to your .profile file. Bash_profile seems to take precedence over the normal .profile file when sourcing php and other applications.
If you see something else in the bash_profile that is sourcing your php then you have two options. You can add the line from the .profile into the bash_profile at the bottom of the file or you can delete the line from bash_profile.
Warning – there is a reason that the bash_profile is there (it’s not installed on a fresh Mac install, so if it’s there it’s because someone or something added it) so you run the risk that something else stops working. However your new install of php that you’re pointing to might just be fine too, and it might not cause a problem. It’s hard to say.
Oh – how I found this page 3 hour ago. You genius – thank you.
…how wish* I found…
Thanks. This saved me a lot of time and like you said I can’t believe this has to be so complicated. Worked perfectly for me after trying to use Laravel 5 and composer to create new apps. I can see where this would be very useful anytime you need to have mcrypt or other modules installed on your OSX system. I will definitely be recommending your services should someone ask,
This was extremely helpful! I don’t know how many tutorials I tried before I found this. Thanks for the great post! 😀
Awesome! I had the same problem which is why I posted this. I’m glad it worked so well for you.
YAyyyyy!!!!! Gosh thanks so much. Yosemite, how could you betray me so?
Same for Mavericks, cuz I don’t trust Yosemite!
Big thanks. I can’t quite believe it… 2 mins, and it’s done! Installing mcrypt has always been such a hassle – but not anymore 🙂
Thank you so much. I was trying to install this god damn thing but I couldn’t. Even I formatted my mac to do clean installation but it wasn’t about it. So, thank you very much, you fixed my problem. God bless you bro!
Hello
first of all thanks a lot for all your help that is so good
i’m still new in all this so i’m wondering after this installation where is the app so i can run and start the coding thing
or what after that >??
This installed mcrypt onto the root of your machine. There is no app to launch, it is only a script utilized by your system PHP library. Keep in mind that this is a different PHP install than the one used for MAMP. MAMP uses its own install of PHP and it generally has mcrypt installed already. This tutorial simply installs mcrypt at the system level. There is no graphic interface or way to launch the app, you will just have access to it if any PHP application tries to use it.
The most common uses for this that I know of is for Laravel. When using Laravel’s built in server it requires you to run it on the system PHP library and requires mcrypt to function. So many people will get error messages saying the mcrypt cannot be found and this tutorial will install it so that laravel can find it.
Laravel runs mainly in the command line. So there is no app that you launch. You will use the terminal to cd into your laravel project folder and then run
<
pre>php artisan serve<pre> to launch the web server and access your website through
<
pre>http://localhost:8000<pre>
Does it has php5-readline also, since its require in Laravel tinker to support full REPL
This specific script only includes mcrypt but most macs will have php5-readline, I have been able to use laravel tinker without any extra installs.
You’re amazing – thanks!
Why the files are downloaded from a chinese domain? It scares me a little.
CH domain stands for Switzerland (https://en.wikipedia.org/wiki/Switzerland).
https://en.wikipedia.org/wiki/.ch
Switzerland actually. A nice neutral little country 🙂
Really helpful .it save my day . Thanks a lot
Thank you! You saved my day
Top Top Man!!!
You have helped me to solve a long time persisting problem!!! Your post worked just fine! Now I only have to play a little with php.ini file to get rid of some “Notice level” error msgs, shouldn’t need a rocket engineer to get this done. 😉
Thank you soooo much J. Alexander Curtis
J.
You’re welcome. My pleasure.
Hi
I have followed your instructions, but i still see my previous version of php.
You are the man. Thanks
HI I have followed the instructions its all installed but not Abyss web server x2 shows listening problem do you have any ideas
thanks
Adam
Thanks! You Made My Day
I owe you my life haha
Thank you so much!!
Hey,
Thank you for saving my day.
Really good.
Thanks , It work for me.
The uninstall link no longer works and I need to uninstall. Any chance there is an updated link?