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 8 On Ubuntu 18.04 LTS and How To Install PHP 8 On Ubuntu 20.04 LTS to install PHP 8 on Ubuntu.
Verify PHP
We can check the PHP version using the command as shown below.
# Check PHP version php -version
# Output PHP 8.0.5 (cli) (built: May 3 2021 11:30:57) ( NTS ) Copyright (c) The PHP Group Zend Engine v4.0.5, Copyright (c) Zend Technologies with Zend OPcache v8.0.5, Copyright (c), by Zend Technologies
Install PHP Dev
Install the PHP development package using the below-mentioned command.
sudo apt install php8.0-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.4
# Output WARNING: channel "pecl.php.net" has updated its protocols, use "pecl channel-update pecl.php.net" to update downloading mcrypt-1.0.4.tgz ... Starting to download mcrypt-1.0.4.tgz (27,056 bytes) .........done: 27,056 bytes 6 source files, building running: phpize Configuring for: PHP Api Version: 20200930 Zend Module Api No: 20200930 Zend Extension Api No: 420200930 ---- ----
# 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/20200930/mcrypt.so' install ok: channel://pecl.php.net/mcrypt-1.0.4 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/20200930/mcrypt.so > /etc/php/8.0/cli/conf.d/mcrypt.ini" sudo bash -c "echo extension=/usr/lib/php/20200930/mcrypt.so > /etc/php/8.0/apache2/conf.d/mcrypt.ini"
At last, test your installation.
# Verify MCrypt php -i | grep "mcrypt"
# Output /etc/php/8.0/cli/conf.d/mcrypt.ini Registered Stream Filters => zlib.*, string.rot13, string.toupper, string.tolower, 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 for the most recent version of PHP i.e. PHP 8. You can also refer How To Install MCrypt For PHP 7 On Ubuntu 20.04 LTS and How To Install MCrypt For PHP 7 On Ubuntu 18.04 LTS.
Summary
This tutorial provided the steps to install the most recent version of MCrypt for PHP 8. It also provided the resources to install PHP 8 on Ubuntu.