can't find mcrypt => Call to undefined function Laravel\mcrypt_create_iv()

can't find mcrypt => Call to undefined function Laravel\mcrypt_create_iv()

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

Stefan Bogdanescu

Founder & Senior Architect

can't find mcrypt => Call to undefined function Laravel\mcrypt_create_iv()

If you are staring at a stack trace complaining about Call to undefined function Laravel\mcrypt_create_iv(), the issue is almost certainly an environment mismatch rather than a bug in your application logic. The PHP extension responsible for cryptographic operations has been deprecated and removed from standard distributions for years, yet legacy Laravel versions still attempt to load it by default.

When you encounter this error on macOS, specifically when php -m does not list mcrypt despite installing the package via Homebrew (located in /usr/local/Cellar), you are facing a classic PHP configuration problem. The operating system has the library installed, but the PHP interpreter running your web server cannot see it. This disconnect usually stems from how the native web server is configured or which PHP binaries are being invoked.

The root cause lies in the separation between system packages and runtime environments. Installing mcrypt via Homebrew places the shared object file (.so) in a directory like /usr/local/lib, but this does not automatically update the php.ini configuration file associated with your web server. If you are running PHP's native web server or Apache with PHP, they load extensions based on the specific php.ini path defined for that binary.

To resolve this, you must verify exactly which PHP binary is active. Run the following command in your terminal:

which php
php -m | grep mcrypt

If mcrypt is missing from the output, check if multiple PHP versions are installed (e.g., via Homebrew and MacPorts). You might have the extension compiled for one version but running another. Ensure that the path to your PHP binary matches the location where you installed the Homebrew package. If necessary, restart your native web server after confirming the installation path aligns with the runtime environment.

However, relying on mcrypt in a modern Laravel setup is often a false solution. The extension was deprecated in PHP 7.1 and removed entirely in PHP 7.3. Even if you manage to get the function defined by reinstalling the legacy package, the underlying cryptographic algorithms used by mcrypt_create_iv are considered insecure for new applications.

The most robust fix involves updating your application to use modern encryption primitives. Laravel 5.5 and later introduced the Illuminate\Encryption facade which abstracts these details, but you should ensure your codebase does not hardcode calls to deprecated functions. If you are upgrading a legacy project, consider replacing mcrypt_create_iv calls with random_bytes(), which is part of the standard PHP core and does not require external extensions.

// Legacy (Deprecated)
$iv = mcrypt_create_iv(16, MCRYPT_DEV_URANDOM);

// Modern Equivalent (Secure & Supported)
$iv = random_bytes(16);

There is a specific trade-off here: legacy code might depend on the specific behavior of mcrypt. Migrating requires auditing all encryption logic to ensure compatibility with AES or other modern standards. Forcing an old extension into a new environment creates technical debt, especially if the underlying cryptographic primitives are no longer vetted for security vulnerabilities.

If you cannot immediately refactor your entire codebase, you might consider updating the PHP version. Modern PHP versions do not support mcrypt. Trying to install the extension on PHP 7.4 or 8.x will fail regardless of Homebrew installation attempts because the function is stripped from the core source code before compilation.

For projects stuck on older Laravel versions that strictly require mcrypt, you may need to pin your PHP version to a legacy release (like PHP 7.0) using a version manager like phpenv or rbenv, though this is generally discouraged due to security risks. Alternatively, if you are working with a commercial hosting provider or a specific enterprise setup, check their documentation for supported legacy extensions, as some vendors provide backports of deprecated modules.

Ultimately, the error message is a signal that your environment configuration does not match the expectations of the application code. Whether through fixing the php.ini path to load the Homebrew extension or migrating to modern cryptographic standards using random_bytes, the goal is to align your runtime with current security best practices. Ignoring the deprecation warning in favor of patching the missing extension often leads to further instability as PHP evolves.

Our network