Important: This article was originally published in 2011, and I wouldn’t recommend you use mcrypt in your PHP project any more (reasons why). If you absolutely need it… then read on….
If you have a need to install the mcrypt extension for PHP, for example if you need to use phpMyAdmin, Magento or Laravel, 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 tools which are 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.
Autoconf
The most recent version of Xcode does not include autoconf which is a pre-requisite. To check that you have it, run the following command in your Terminal:
autoconf --version
If you see output similar to the following, then it means you have it installed — skip to the libmcrypt section.
autoconf (GNU Autoconf) 2.61
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software. You may redistribute copies of it under the terms of
the GNU General Public License
There is NO WARRANTY, to the extent permitted by law.
Written by David J. MacKenzie and Akim Demaille.
If you get a command not found
error, then you will need to perform the following to install autoconf:
cd /tmp
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
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
./configure
make
sudo 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
./configure
make
sudo 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.
P.S. – Looking for a reliable hosting provider for your PHP projects? I recommend Clook:
Trying to rebuild my private server from a crash, this was gold. Thank you. Saved me hours.
Thanks Glen. This helped me a bunch while installing phpMyAdmin
Thanks-a-lot : )
It was easy and I did it in no time.
Nice. Very helpful. But this line in php.ini didn’t work:
extension = mcrypt.so
Until I added this line earlier in the file:
extension_dir = “/usr/lib/php/extensions/no-debug-non-zts-20090626/”
Just in case anyone runs into that problem.
Pingback: September 28 blog – Dan Spencer » Installing libmcrypt + php mcrypt on Mac OS X Lion
Great tutorial! Was 100% concise and worked like a charm.
Thank you for Great Tutorial! You save me a lot of time.
Glen,
I already have PHP 5.3.6 that came with my mac. What should I do in order to install and enable mcrypt? Or you know to upgrade PHP to 5.3.8 completely?
Hey Prasanna: PHP 5.3.6 does not contain the mcrypt extension, so you need to following the instructions in this post to enable it. Even though you have 5.3.6 installed, you still need to download the source code in order to compile the mcrypt extension correctly.
Works like a charm. THANK YOU !!
Thank you!! I found it very useful.
Thanks a lot more easy and straight forward than other tutorials trying to achieve the same result.
Done with all the steps, but still no php mcrypt documentation and the same error(mcrypt extension is missing) continues….do help:(
“Thanks a lot more easy and straight forward than other tutorials trying to achieve the same result.”
+1
I have tried for some time using different site to get crypt to work on Lion 10.7.3 beta 2
I use php 5.3.8
I have the later Xcode version.
No matter what I do I get
/SourceCache/php-5.3.8/ext/mcrypt/mcrypt.c:282: error: ‘PHP_FE_END’ undeclared here (not in a function)
I have tried to figure if there si som missing includes. But right now I am stuck
Thanks for the instructions, but I can’t get it to work. libmcrypt-2.5.8 builds fine, and php-5.3.6/ext/mcrypt builds fine. I even ran
make test
for the PHP build and all tests pass.I added the two lines into
php.ini
and tried to restart Apache (both from the command line, and from System Preferences / Sharing / Web Sharing) … no luck.Apache won’t start, and there isn’t anything useful in the logs. Running PHP from the command line seems fine, and I can see the mcrypt extension listed in phpinfo().
Any suggestions? I’m on OS X 10.7.2, with Xcode 4.2.1 (4D502).
Further to my previous post, I can’t Apache to start at all now! Taking the mcrypt-related lines out of php.ini doesn’t help. Argh!
Hey Colin,
If you run the following command in your terminal, what output do you get?
sudo apachectl restart ; tail -f /var/log/apache2/error_log
I checked the Apache logs. Nothing relevant.
It turns out that Apache is restarting. I think it’s just taking a bit longer to start up now … and maybe System Prefs has a built in time-out to check if it was successful?
In any event, if I start it from the command line, I can see the process in
ps aux
. If I start it from System Prefs, it doesn’t show it as started right away. The process is there though. If I close System Prefs and re-open it, Web Sharing is checked.Finally, I solved this problem using this article.
But one thing to keep in mind is that don’t forget to copy “php.ini.default” in /etc directory to “php.ini” in the same directory. (There was only exist php.ini.default file, not exist php.ini file in /etc directory!!)
Thank you Glen, it worked at first go!
Sandro
Back again, and after upgrading to 10.7.3, I get the same error as @kgi111:
/Users/cmv/src/php-5.3.8/ext/mcrypt/mcrypt.c:283: error: ‘PHP_FE_END’ undeclared here (not in a function)
Any suggestions? A search recommends changing all occurrences of PHP_FE_END to {NULL, NULL, NULL} … but that just seems like a hack.
Hey Colin,
Yes, this error occurs with the PHP 5.3.8 source. I have no idea why. make still works fine with the 5.3.6 sources, and the resulting compiled extension should still work on 10.3.7 so I suggest using these rather than hacking 5.3.8.
Thank for your support
It works great!
Oh,
On my other machine installing Xcode 4.3, when run ./configure to install libmcrypt-2.5.8, it produce the error:
Applehouses-MacBook-Pro-7:libmcrypt-2.5.8 Applehouse$ ./configure
checking build system type... i686-apple-darwin11.3.0
checking host system type... i686-apple-darwin11.3.0
checking target system type... i686-apple-darwin11.3.0
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for gawk... no
checking for mawk... no
checking for nawk... no
checking for awk... awk
checking whether make sets $(MAKE)... no
checking whether to enable maintainer-specific portions of Makefiles... no
checking whether make sets $(MAKE)... (cached) no
checking for g++... no
checking for c++... no
checking for gpp... no
checking for aCC... no
checking for CC... no
checking for cxx... no
checking for cc++... no
checking for cl... no
checking for FCC... no
checking for KCC... no
checking for RCC... no
checking for xlC_r... no
checking for xlC... no
checking for C++ compiler default output file name... configure: error: C++ compiler cannot create executables
See `config.log' for more details.
I’m not sure whether Xcode 4.3 is the reason. Please help me
Hey Nguyen,
Where do you have the developer tools installed on your other machine? I’ve heard that if you have spaces in the pathname, it may cause problems (for example, if devtools are installed under /Developer 4.3/ rather than just /Developer/).
Hi y’all
First of thank you Glenn, for sharing your experiences.
Though, I ran into the same issue like Nguyen.
The latest XCode does not include the needed GCC.
Download and install the following package:
https://github.com/kennethreitz/osx-gcc-installer
Worked like a charm!
QT
Hey QT,
Thanks for the update. Yes, you’re correct about Xcode 4.3 — it doesn’t have the tools we require to build PHP extensions by default.
I’ve added a blog post here that has instructions on how to get these tools if you have Xcode 4.3:
http://www.glenscott.co.uk/blog/2012/03/07/compiling-php-extensions-using-xcode-4-3-and-above/
Hey Glen
Yeah, right, I knew there was a checkbox somewhere 🙂
Thanks anyway!
QT
Thank you
Thanks. If it weren’t for people like you how post step-by-step instructions on how to do things like this we would all be in the dark ages.
Thank you,
After a few hours of struggling with installing mcrypt, I succeeded thanks to your article.
When run ./configure to install libmcrypt-2.5.8, it produce the error:
aag4server:libmcrypt sydchess$ ./configure
checking build system type… i686-apple-darwin11.3.0
checking host system type… i686-apple-darwin11.3.0
checking target system type… i686-apple-darwin11.3.0
checking for a BSD-compatible install… /usr/bin/install -c
checking whether build environment is sane… yes
checking for gawk… no
checking for mawk… no
checking for nawk… no
checking for awk… awk
checking whether make sets $(MAKE)… no
checking whether to enable maintainer-specific portions of Makefiles… no
checking whether make sets $(MAKE)… (cached) no
checking for g++… no
checking for c++… no
checking for gpp… no
checking for aCC… no
checking for CC… no
checking for cxx… no
checking for cc++… no
checking for cl… no
checking for FCC… no
checking for KCC… no
checking for RCC… no
checking for xlC_r… no
checking for xlC… no
checking for C++ compiler default output file name… configure: error: C++ compiler cannot create executables
See `config.log’ for more details.
aag4server:libmcrypt sydchess$ make
-bash: make: command not found
aag4server:libmcrypt sydchess$ sudo make install
Password:
sudo: make: command not found
Please help me!
Sounds like you don’t have the required command line tools needed to compile. Please see this post for details on how to get these:
http://www.glenscott.co.uk/blog/2012/03/07/compiling-php-extensions-using-xcode-4-3-and-above/
Thanks for this. Followed your directions from start to finish and everything went off without a hitch. One of the few (if not only) times that’s happened when I’ve followed someone’s blog post to solve a problem.
Thank You! Great Article..Concise and very specific!
Thank you Glen, helps me a lot!
Great tutorial! Thank you for letting me know how to plug this module on Mac OS X Lion.
Well, I use Mac OS X Lion 10.7.4, and the default PHP version is 5.3.10. I downloaded PHP source of ver. 5.3.10 instead of ver. 5.3.6, and there was no problem phpize-ing mcrypt. Also, I should have installed autoconf for phpize-ing. Those are the information I would give to you.
Anyway, thank you for tutorial! 😀
Pingback: Turn Mac OS X Lion Into a LAMP Web Server (Without MAMP) ⊆ David Xia
Like Nan above, I ran into the same (I had to instal macports to get autoconfig) but I used PHP version 5.3.6 to build mcrypt. Hope that does not come back to bite me on 10.7.4. Anyway, all is well now so thx for the great headstart on this one!
btw, I am new to lion servers and was wondering: can I just install MAMP pro on these boxes and be done with it? Seems like that would easier… but sure to break things, I’m sure…
Install mcrypt PHP extension on Mac OS X Lion | Glen Scott – Keeping it simple http://t.co/owK4pvjW #Mcrypt, php, lion
Hello,
almost at the end, I get this message:
-phpize
Configuring for:
PHP Api Version: 20090626
Zend Module Api No: 20090626
Zend Extension Api No: 220090626
Cannot find autoconf. Please check your autoconf installation and the
$PHP_AUTOCONF environment variable. Then, rerun this script.
Hi Stanislav,
autoconf is part of the Command Line Tools package provided by Apple. Do you have it installed?
Are you having problems with #laravelphp installation? Maybe you need to install the mcrypt extension. http://t.co/RVYwhprc
Thank you. I tried 4 others posts with no luck. Yours = like a charm first time.
Pingback: mac 10.8에서 mcrypt관련 php오류난 경우 « June
Gracias a @glenscott por esto http://t.co/uSglEtON Thank you!
Install mcrypt PHP extension on Mac OS X Lion | Glen Scott – Keeping it simple http://t.co/3Y62ZQLZ
These instructions ROCKED on OS 10.8.2 “Mountain Lion” using libcrypt 2.5.8 and PHP 5.3.17. Thank you!
Cool! I just added mcrypt extension to #php on Mac OS 10.8.2 Mountain Lion, using these instructions: http://t.co/ly0LZfMy
Thank you this was brilliant advice, simply explained and worked a treat.
Thanks again
Ta, mate! Got phpMyAdmin up and running with this! 😉
Brilliant. Thanks, mate!
will this work if I have PHP5.4.8 ?
Thanks so much. When the Apple OS-X upgrade destroyed my development environment, you helped me get it back.
Thanks for these instructions! I’ve used them 2x now: one on initial set-up, the other after a Mountain Lion upgrade (which blew everything else away).
Thanks a lot. I had spent so many time trying to instal mcrypt
With this tutorial was easy and fast. Thanks again
Thanks a lot.
why apple not do that?
Using Lion 10.8
After trying to ./configure while installing libmcrypt I got the following issue:
*** Could not run libmcrypt test program, checking why…
*** The test program failed to compile or link. See the file config.log for the
*** exact error that occured. This usually means LIBMCRYPT was incorrectly installed
*** or that you have moved LIBMCRYPT since it was installed. In the latter case, you
*** may want to edit the libmcrypt-config script: no
configure: error: *** libmcrypt was not found
Do you have any solution or suggestion to fix this issue:
@Ulises, use this script:
php-osx.liip.ch
works like a charm
Hi, i am getting the following error
correct ‘make’ to ‘_make’ [nyae]?
any ideas thanks
Thanks for making the internets better. Followed several. Yours nailed it. 😉
This was a great and very thorough guide, great that you provided links to all the things whiich needed to be downloaded. I only wish that xcode 4 was available somewhere publicly for download, not just for Apple developers. I wanted to download it from the app store but they require a credit card and I just didn’t want to do that.
Thanks very much for this. Been looking for a simple tutorial for getting mcrypt for Laravel development
Incredibly helpful, thanks so much. Mcrypt had been a missing piece in my local apache install for a long time and I had no idea how to remedy it until I found this.
Thanks for the instructions. Worked like a charm.
Thanks!
Install on OS X 10.8.4 / PHP 5.3.15
now.. laravel work 😉
Thanks worked well 🙂
Where do you find the /etc/php.ini file?
If you never messed with it before it’s in a file called php.ini.default.
Just sudo mv php.in.default php.ini