If you are using the most recent versions of PHP and your project depends on MCrypt, you will get the error specific to Encrypt library which requires MCrypt extension. The main reason for missing MCrypt is that PHP no longer provides it by default and we must explicitly install it in order to continue using it.
Notes: This tutorial assumes that most recent version of PHP is already installed on the system. You can also follow the tutorial How To Install PHP 7 On Ubuntu 20.04 LTS to install PHP 7 on Ubuntu.
Install PHP Dev
Install the PHP development package using the below-mentioned command.
sudo apt install php7.4-dev
Install Build Tools
Install the build tools using the below-mentioned command.
sudo apt -y install gcc make autoconf libc-dev pkg-config
Install MCrypt Dev
Install the MCrypt development package using the below-mentioned command.
sudo apt-get -y install libmcrypt-dev
Install MCrypt
Install the most recent version of MCrypt using the below-mentioned command.
# Install MCrypt sudo pecl install mcrypt-1.0.3
# Output Notice: Trying to access array offset on value of type bool in PEAR/REST.php on line 187 PHP Notice: Trying to access array offset on value of type bool in /usr/share/php/PEAR/REST.php on line 187 WARNING: channel "pecl.php.net" has updated its protocols, use "pecl channel-update pecl.php.net" to update downloading mcrypt-1.0.3.tgz ... Starting to download mcrypt-1.0.3.tgz (33,590 bytes) .........done: 33,590 bytes 6 source files, building running: phpize Configuring for: PHP Api Version: 20190902 Zend Module Api No: 20190902 Zend Extension Api No: 320190902 ---- ----
# Press enter on command prompt to complete installation libmcrypt prefix? [autodetect] :
It will complete the installation and shows the success message as shown below.
Build process completed successfully Installing '/usr/lib/php/20190902/mcrypt.so' install ok: channel://pecl.php.net/mcrypt-1.0.3 configuration option "php_ini" is not set to php.ini location You should add "extension=mcrypt.so" to php.ini
Configure PHP
Now configure the PHP by updating the php.ini file using the below-mentioned commands.
sudo bash -c "echo extension=/usr/lib/php/20190902/mcrypt.so > /etc/php/7.4/cli/conf.d/mcrypt.ini" sudo bash -c "echo extension=/usr/lib/php/20190902/mcrypt.so > /etc/php/7.4/apache2/conf.d/mcrypt.ini"
At last, test your installation.
# Verify MCrypt php -i | grep "mcrypt"
# Output /etc/php/7.4/cli/conf.d/mcrypt.ini Registered Stream Filters => zlib.*, string.rot13, string.toupper, string.tolower, string.strip_tags, convert.*, consumed, dechunk, convert.iconv.*, mcrypt.*, mdecrypt.* mcrypt mcrypt support => enabled mcrypt_filter support => enabled mcrypt.algorithms_dir => no value => no value mcrypt.modes_dir => no value => no value
These are the steps required to install the MCrypt extension on most recent versions of PHP i.e. PHP 7.4. You can also refer How To Install MCrypt For PHP 7 On Ubuntu 18.04 LTS.