Call to undefined function mcrypt_decrypt() - even when php5-mcrypt installed

Call to undefined function mcrypt_decrypt() - even when php5-mcrypt installed

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

Stefan Bogdanescu

Founder & Senior Architect

Call to undefined function mcrypt_decrypt() - even when php5-mcrypt installed

You run your script and immediately hit a wall: PHP Fatal error: Call to undefined function mcrypt_decrypt() in file /path/to/script.php on line X. You check the package manager, see that php5-mcrypt is listed as installed (ii), and you are confused because the documentation explicitly states this extension provides the necessary functions.

The issue isn't whether the library exists on your system; it's whether the PHP interpreter actually knows about it. In Linux environments, specifically with Debian-based distributions like Ubuntu (as suggested by the mandatorys-box hostname and package naming), the default installation of PHP often omits extensions to keep the core footprint small. You install them manually later, but simply running apt-get install php5-mcrypt does not automatically load the module at runtime unless you configure the environment correctly.

The Extension Loading Mechanism

The error message indicates that the function is undefined in the current execution context. Even if php -m lists mcrypt, there are scenarios where it might not appear, or the function remains unreachable due to how the extension is compiled versus loaded dynamically.

When you install php5-mcrypt, you are installing a shared object file (.so) and potentially an initialization script. For PHP 5.5 on Ubuntu, this often manifests as mcrypt.so. The critical step many developers miss is ensuring this file is actually loaded into the memory space of your running PHP process.

Check if the extension is active by running:

php -m | grep mcrypt

If this command returns nothing, the function will remain undefined regardless of whether the package is installed. This happens when the zend_extension directive in php.ini points to a non-existent file or if the file path differs between the CLI and Apache modules.

Configuration Discrepancies

A common pitfall involves using different PHP versions for different tasks. The error log shows you have libapache2-mod-php5 and php5-cli. If your web server (Apache) is serving the request, it loads the php.ini from /etc/php5/apache2/, while your local CLI tests load from /etc/php5/cli/.

If you installed php5-mcrypt globally but only configured it in the Apache configuration directory, your CLI tests will fail, and vice versa. Conversely, if you have an older version of PHP running via a specific path (e.g., /usr/bin/php54) while your web server uses a newer version (/usr/bin/php56), they may both claim to be "PHP 5" but cannot see each other's extensions.

Ensure the extension=mcrypt.so line exists in the active php.ini. In some Ubuntu setups, this extension is not part of the default php5-common and requires explicit inclusion:

; /etc/php5/apache2/php.ini
extension=mcrypt.so

Without this directive, PHP initializes without loading the library, leaving the functions undefined.

Deprecation and Modern Alternatives

It is important to note that the mcrypt extension has been deprecated for a long time. As of PHP 7.0, the entire extension was removed from the distribution entirely because modern cryptographic standards have superseded the algorithms it supports (like Blowfish, Cast5, etc.). Even in PHP 5.5, relying on mcrypt is increasingly risky due to its inherent weaknesses and lack of forward compatibility.

If you are maintaining legacy code that relies on mcrypt_decrypt, you must ensure the environment matches exactly what the application expects. However, for any new implementation or migration, switching to OpenSSL's symmetric encryption functions (like openssl_encrypt) or dedicated libraries like libsodium is the recommended path. These libraries offer robust security guarantees and are built into standard PHP distributions without needing separate .so files in many modern contexts.

If you are working on a Laravel project or similar framework that still references legacy crypto functions, ensure your composer.json does not pull in dependencies requiring mcrypt. Instead, update the encryption logic to use the native OpenSSL extension or the paragonie/random_compat library if version compatibility is an issue.

Verification Steps

Before reinstalling packages or restarting services, verify the specific state of your installation:

  1. Check for the file: Locate the shared object. On Ubuntu, it is usually at /usr/lib/php5/20131226/mcrypt.so or similar paths depending on the build date.
  2. Verify php.ini: Confirm the extension line is uncommented and points to the correct path.
  3. Check for conflicts: Ensure no other PHP version manager (like phpenv or php7.x) is shadowing your system defaults, causing a mismatch between the installed package and the running binary.

By ensuring the extension is explicitly loaded in the configuration file that matches your execution environment, you resolve the undefined function error without needing to rebuild the entire PHP stack.

Our network