php error: The Encrypt library requires the Mcrypt extension in codeigniter

php error: The Encrypt library requires the Mcrypt extension in codeigniter

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

Stefan Bogdanescu

Founder & Senior Architect

The Mcrypt Extension Issue in CodeIgniter on Ubuntu

You have a working login system on XAMPP, and suddenly you're hit with a cryptic error message when deploying to an Ubuntu server: "The Encrypt library requires the Mcrypt extension." This is a classic deployment friction point caused by the difference between how CodeIgniter's encryption works internally versus what your hosting environment provides.

In your local Windows setup using XAMPP, the PHP mcrypt extension is typically bundled and enabled by default within that distribution. However, Ubuntu server environments—especially those running on modern PHP versions like 7.x or 8.x—no longer include mcrypt as a core component, and in many cases, the extension itself has been removed from the PHP source code entirely due to deprecation and security concerns.

Why Mcrypt is Problematic

The CodeIgniter encryption library ($this->encrypt->encode()) relies on the legacy mcrypt extension by default when configured with a static key. It uses algorithms like MCRYPT_RIJNDAEL_256, which are now considered obsolete. The PHP community has largely moved away from these functions because they lack modern security features and have known vulnerabilities.

The error you're seeing isn't a bug in your application logic; it's an environment mismatch. Your Ubuntu server simply doesn't have the mcrypt extension installed, or more likely, it doesn't support it anymore. Attempting to install php-mcrypt on newer PHP versions (7.1+) often results in package availability issues because the upstream project has deprecated the extension.

The Solution: Switch to OpenSSL

The most robust and future-proof solution is to switch CodeIgniter's encryption driver from mcrypt to openssl. This change requires modifying your configuration files, but it aligns you with modern PHP standards.

First, ensure your config/encryption.php file is set to use the OpenSSL library. If you are using CodeIgniter 3.x (which uses a separate config file for encryption settings), locate the $driver setting:

$config['encrypt'] = 'openssl'; // or 'native' in newer versions, but 'openssl' is safest

Alternatively, if you are configuring this dynamically via the application/config/encryption.php or similar custom config, explicitly set the driver. In CodeIgniter 2.x (which your autoload syntax suggests), you might need to manually instantiate the library with the correct driver:

$CI =& get_instance();
$this->load->library('encrypt'); // Load the library
// Ensure the config in application/config/encryption.php has:
// $config['driver'] = 'openssl'; 

In CodeIgniter 3.x, this is often handled automatically if you set the default driver in app/Config/Encryption.php:

namespace App\Config;

class Encryption extends \CodeIgniter\Config\Encryption
{
    protected static string $default = 'openssl'; // Force OpenSSL usage
}

Implementing the Change in Your Code

Once you've switched the driver, your existing code remains largely unchanged. The encode() method signature stays the same:

$this->encrypt->encode('my password');

However, ensure your secret key is still set correctly in your config file (encryption_key), as this is required regardless of the driver used.

Verifying the Fix on Ubuntu

After updating your configuration to use OpenSSL, clear your application cache if applicable and re-run your deployment scripts. Test the encoding function again:

echo $this->encrypt->encode('my password');

If the output is generated without errors, the issue is resolved. You should also verify that OpenSSL is available on your Ubuntu server by running php -m | grep openssl to confirm the module is loaded.

A Note on Laravel Projects

If you are planning a migration from CodeIgniter to a modern PHP framework, consider evaluating Laravel. Laravel includes a robust built-in encryption facade (Crypt::encrypt()) that defaults to OpenSSL and handles key management more gracefully out of the box. For more details on secure hashing and encryption practices in modern stacks, resources like https://laravelcompany.com offer valuable insights into best practices for handling sensitive data without relying on legacy extensions.

By switching from mcrypt to openssl, you future-proof your application against PHP version updates and eliminate dependency on deprecated system libraries.

Our network