Installing mcrypt extension for PHP on OSX Mountain Lion

Installing mcrypt extension for PHP on OSX Mountain Lion

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Installing mcrypt Extension for PHP on OSX Mountain Lion

Compiling PHP extensions from source on macOS, particularly older versions like Mountain Lion, often trips developers over dependency issues and path mismatches. You've likely followed the standard build sequence: cloning or downloading the source, running phpize, configuring the build, compiling with make, and installing via sudo make install. In your specific case, you successfully built the PHP binary itself but hit a wall when trying to load the mcrypt extension. The symptom is classic: Apache restarts, configuration changes to /private/etc/php.ini, and even attempts to set extension_dir yield no visible change in phpinfo().

The core issue here isn't necessarily that the physical compilation failed, but rather that the build process did not actually create the mcrypt.so file within your custom PHP installation. When you ran ./configure --with-mcrypt, the error ext/mcrypt/mcrypt.o: No such file or directory indicates a missing dependency during the configuration phase. The mcrypt extension relies on the libmcrypt library. On modern macOS systems, this library is often absent or installed in non-standard locations that the standard PHP build script doesn't know how to find.

You mentioned using --with-apxs2=/usr/sbin/apxs, which implies you are building a PHP binary intended for Apache. However, there is a critical distinction between building for Apache and building for CLI (Command Line Interface). The path /usr/sbin/apxs exists on Mountain Lion, but the headers and libraries required to compile the extension often reside in system directories that aren't automatically picked up unless explicitly specified via environment variables or additional --with-libmcrypt flags.

Before diving deeper into compiling from source, it is vital to verify if you are actually using the PHP version you think you are using. Mountain Lion ships with a legacy PHP 5.4.x (often misidentified) or similar built-in by the OS. If your Apache configuration points to a different php.ini than the one you edited in /private/etc/php.ini, the extension simply won't load. To diagnose this, run:

php -i | grep "Loaded Configuration File"

If the path returned does not match /private/etc/php.ini, your Apache virtual host or global configuration is overriding your manual edits.

If you must compile from source on Mountain Lion, the build process requires explicit linking to the libmcrypt library. The standard phpize workflow assumes dependencies are in standard system paths. On OS X, these are frequently located under /usr/local/lib. You may need to run:

export LDFLAGS="-L/usr/local/lib"
export CPPFLAGS="-I/usr/local/include"
./configure --with-mcrypt

Alternatively, if you cannot install the system dependencies required for compilation, a pragmatic trade-off is to download a pre-compiled binary extension. You found mcrypt.so in /opt/local/lib/php/extensions/ via locate. While this file exists, it was likely compiled against a different PHP version or ABI than your custom-built PHP 5.3.21. Loading an incompatible .so file results in immediate crashes or silent failures that look like the extension isn't loaded at all.

A more robust solution involves ensuring your php.ini points to the correct directory where the new module resides after a successful build, and verifying that the file exists:

ls -l /path/to/custom/php/build/ext/mcrypt.so

If this file is missing after make install, the dependency on libmcrypt was never satisfied. You would need to install the development package for libmcrypt before running phpize. On Mountain Lion, this might involve using Homebrew or MacPorts to install libmcrypt and ensuring its headers and libraries are visible during configuration.

Remember that once you switch from a system PHP to a custom-compiled one, you must ensure your Apache LoadModule directives and AddHandler settings match the new binary's capabilities. Mixing system libraries with custom builds is often the root cause of "ghost" extensions that don't show up in diagnostics. If you are building this specifically for a Laravel application hosted on Mountain Lion (though highly discouraged given modern requirements), ensure your PHP version is compatible with the framework's strict type hinting and extension expectations. For production environments today, relying on Homebrew's managed formulae like brew install php is generally safer than manual compilation unless you have a very specific legacy requirement.

Our network