Install mcrypt PHP extension on Mac OS X Lion

If you have a need to install the mcrypt extension for PHP, for example if you need to use phpMyAdmin, 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 versions of tools like autoconf which is 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.

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.

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. If you are needing a simple way of accessing these functions, check out my SimpleCrypt class.

mcrypt php extension

Comments

  1. Atle says:

    Trying to rebuild my private server from a crash, this was gold. Thank you. Saved me hours.

  2. Ryan says:

    Thanks Glen. This helped me a bunch while installing phpMyAdmin

  3. s@ch@x says:

    Thanks-a-lot : )
    It was easy and I did it in no time.

  4. Kevin says:

    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.

  5. Great tutorial! Was 100% concise and worked like a charm.

  6. berec says:

    Thank you for Great Tutorial! You save me a lot of time.

  7. Prasanna says:

    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?

  8. Glen says:

    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.

  9. Schniebo says:

    Works like a charm. THANK YOU !!

  10. Guillermo says:

    Thank you!! I found it very useful.

  11. Global says:

    Thanks a lot more easy and straight forward than other tutorials trying to achieve the same result.

  12. Rohit Sangal says:

    Done with all the steps, but still no php mcrypt documentation and the same error(mcrypt extension is missing) continues....do help:(

  13. safoo says:

    "Thanks a lot more easy and straight forward than other tutorials trying to achieve the same result."

    +1

  14. kgi111 says:

    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

  15. Colin says:

    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).

  16. Colin says:

    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!

  17. Glen says:

    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

  18. Colin says:

    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.

  19. Marvin Yoon says:

    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!!)

  20. Sandro says:

    Thank you Glen, it worked at first go!
    Sandro

  21. Colin says:

    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.

  22. Glen says:

    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.

  23. Nguyen Tran Viet says:

    Thank for your support
    It works great!

  24. Nguyen Tran Viet says:

    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

  25. Glen says:

    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/).

  26. QT says:

    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

  27. Glen says:

    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/

  28. QT says:

    Hey Glen

    Yeah, right, I knew there was a checkbox somewhere :-)

    Thanks anyway!
    QT

  29. zouyang says:

    Thank you

  30. Nathan says:

    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.

  31. GGavrilut says:

    Thank you,

    After a few hours of struggling with installing mcrypt, I succeeded thanks to your article.

  32. Paul says:

    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!

  33. EC says:

    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.

  34. Glen says:

    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/

  35. Thank You! Great Article..Concise and very specific!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>