mcrypt support for PHP on 64 bit Mac OS X 10.5

Please note: The article below refers to installing mcrypt on Mac OS X 10.5. If you have 10.6 Snow Leopard, please see my updated guide: Install mcrypt PHP extension on OS X Snow Leopard

Adding additional functionality to the standard Apple-supplied PHP on Mac OS X 10.5 is a little tricky if you are running a 64 bit processor such as the Intel Core 2 Duo. The reason is that any dynamic extensions that you add will need to be 64 bit, and many shared libraries by default will compile as 32 bit binaries. Trying to use a 32 bit extension with a 64 bit PHP results in the following unfriendly error message:

PHP Warning: PHP Startup: Unable to load dynamic library './mcrypt.so' - (null) in Unknown on line 0

mcrypt is a good example of a useful extension that can be added to PHP with a little bit of effort:

  • Open up your Terminal.app
  • To explicitly build for 64 bit architecture export CFLAGS="-arch x86_64"
  • Download libmcrypt from sourceforge http://sourceforge.net/projects/mcrypt
  • Unpack the archive
  • cd libmcrypt
  • ./configure --disable-shared
  • make
  • sudo make install
  • download PHP 5.2.6 source from http://www.php.net/get/php-5.2.6.tar.bz2/from/a/mirror
  • unpack the archive and go into the php-5.2.6/ext/mcrypt/ dir
  • phpize
  • ./configure
  • make
  • sudo make install
  • verify the extension is 64 bit: file /usr/lib/php/extensions/no-debug-non-zts-20060613/mcrypt.so

/usr/lib/php/extensions/no-debug-non-zts-20060613/mcrypt.so: Mach-O 64-bit bundle x86_64

To actually use the extension, you can simply create a symbolic link to it. For example:

  • cd ~/Sites
  • ln -s /usr/lib/php/extensions/no-debug-non-zts-20060613/mcrypt.so

Example code: mcrypt.php

Drop the following code into your ~/Sites directory to verify everything is working:

    <?php
    
    if ( ! extension_loaded('mcrypt') ) {
        dl('mcrypt.so');
    }
    
    $key   = "this is a secret key";
    $input = "Let us meet at 9 o'clock at the secret place.";
    
    $td = mcrypt_module_open('tripledes', '', 'ecb', '');
    $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
    mcrypt_generic_init($td, $key, $iv);
    $encrypted_data = mcrypt_generic($td, $input);
    mcrypt_generic_deinit($td);
    mcrypt_module_close($td);
    
    print_r($encrypted_data);
    
    ?>

Glen Scott

I’m a freelance software developer with 18 years’ professional experience in web development. I specialise in creating tailor-made, web-based systems that can help your business run like clockwork. I am the Managing Director of Yellow Square Development.

More Posts

Follow Me:
TwitterFacebookLinkedIn

13 thoughts on “mcrypt support for PHP on 64 bit Mac OS X 10.5

  1. Matt

    Hi Glen,

    I’ve been trying to install mcrypt and libmcrypt successfully on my 64-bit Mac (10.5.6) for some time now, and I’ve just now stumbled upon your instructions. While they’ve gotten me closer than I’ve ever been to actually getting mcrypt working (I actually have a 64-bit mcrypt.so file now, and apache starts without errors), something is still not working properly for me.

    I get the following error whenever I load my phpinfo() script:

    dyld: lazy symbol binding failed: Symbol not found: _mcrypt_readdir
    Referenced from: /usr/local/lib/libmcrypt.4.dylib
    Expected in: flat namespace

    I suspect that libmcrypt is not being installed properly, for whatever reason. This is how I’m trying to install libmcrypt-2.5.8 (as root):

    CFLAGS=”-arch x86_64″ ./configure –enable-dynamic-loading
    make
    make install

    which eventually outputs:

    Libraries have been installed in:
    /usr/local/lib

    If you ever happen to want to link against installed libraries
    in a given directory, LIBDIR, you must either use libtool, and
    specify the full pathname of the library, or use the -LLIBDIR'
    flag during linking and do at least one of the following:
    - add LIBDIR to the
    DYLD_LIBRARY_PATH’ environment variable
    during execution

    See any operating system documentation about shared libraries for

    more information, such as the ld(1) and ld.so(8) manual pages.

    test -z “/usr/local/share/aclocal” || /bin/sh ../mkinstalldirs “/usr/local/share/aclocal”
    /usr/bin/install -c -m 644 ‘libmcrypt.m4’ ‘/usr/local/share/aclocal/libmcrypt.m4’
    Making install in src
    make[2]: Nothing to be done for install-exec-am'.
    make[2]: Nothing to be done for
    install-data-am’.
    Making install in doc
    make[2]: Nothing to be done for install-exec-am'.
    test -z "/usr/local/man/man3" || /bin/sh ../mkinstalldirs "/usr/local/man/man3"
    /usr/bin/install -c -m 644 './mcrypt.3' '/usr/local/man/man3/mcrypt.3'
    make[2]: Nothing to be done for
    install-exec-am’.
    make[2]: Nothing to be done for `install-data-am’.

    Do you have any idea why this isn’t working? I’m completely lost, as I’ve just been trying to muddle my way through this to get mcrypt working.

    Thanks,
    Matt

    Reply
  2. merlos

    I used this line to configure ( valid for x86 64 bits architectures)

    MACOSX_DEPLOYMENT_TARGET=10.5 CFLAGS=”-arch x86_64 -g -Os -pipe -no-cppprecomp” CCFLAGS=”-arch x86_64 -g -Os -pipe” CXXFLAGS=”-arch x86_64 -g -Os -pipe” LDFLAGS=”-arch x86_64 -bind_at_load” ./configure

    regards.

    Reply
  3. Orolin

    Hey, and thanks for a great tutorial:)

    After I’ve followed your instructions everything works just perfectly and I see Mcrypt on the phpinfo-page in my ~Sites directory.

    However, I have a phpMyAdmin-installation located in the root directory of my web server which requires Mcrypt. When I open the phpMyAdmin-login page, I still get the error that it could not load the mcrypt extension. I’m guessing this has something to do with the symbolic link we made as it appears to work exclusively for the ~Sites directory. How can I make Mcrypt available for my entire webserver with all its sub-directories etc?

    (I’d rather not go around creating symbolic links to every single directory in which I plan to use php-files…)

    Thanks again!

    Reply
  4. Glen Post author

    Hey Orolin. If you want to make mcrypt available for the entire webserver, you will need to make sure the /etc/php.ini file contains the following line:

    extension=mcrypt.so

    Also, make sure the extension_dir line is commented out:

    ;extension_dir = "./"

    This will make PHP look in the /usr/lib/php/extensions/no-debug-non-zts-20060613/ directory for extensions by default.

    After making the necessary changes, restart your webserver and you should find mcrypt is available to apps like phpMyAdmin.

    Reply
  5. Atypik Design

    Hi,

    I used the tutorial to install mcrypt on Snow Leopard 10.6 and it worked with 2 twicks that were necessary (for me!) to have the mcrypt extension get loaded AND no error for “time zone”: define the FULL path to extensions directory AND set “timezone”.

    In php.ini, i did the following:

    For extensions dir:
    – find and copy/paste the line : ;extension_dir = “./”
    – uncomment the copied line (remove 😉
    – replace “./” with FULL path to your extensions directory (for me it is: /usr/lib/php/extensions/no-debug-non-zts-20090626)
    – if ever mcrypt.so was installed in “20090613”, use terminal to cpy it to “20090626”
    – add the following line: extension=mcrypt.so

    To set “timezone”, i added this line:
    date.timezone = “Europe/Paris”

    Hope this helps !

    Reply
  6. Kike

    Hi… I have a problem compilling mcrypt…
    Terminal returns:
    configure: error: *** libmcrypt was not found

    when I make: ./configure –disable-shared

    Reply
  7. Mady

    Hi Glen,
    I am newbie.I am planning to work with default php that comes with imac.I installed mysql,phpmyadmin also.Does libcrypt work with default php? I am following your instructions but make doesn’t work, says bash:make:command not found. Please help.Once this is resolved I’ll be all set to learn.
    Thanks.

    Reply
    1. Glen Post author

      Yes, you need the Apple Developer Tools installed – Installing XCode should also install build tools like make.

      Reply

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.