How to fix PHP Warning: Module 'mcrypt' already loaded in Unknown on line 0 when using Brew on MacOS

And Brew is supposed to be making our lives easier...

Published May 24th 2018

I was trying to fix an issue with Laravel Valet and was updating my version of PHP, which was installed via Brew. I'd updated my version of PHP by running brew unlink php71 and then brew link --force php71. However, while brew link was creating symlinks – as you'd expect – brew unlink was removing 0.

This resulted in the error below whenever I tried to run anything to do with PHP (e.g. Composer or tried to start Valet).

Module 'mcrypt' already loaded in Unknown on line 0

I tried to uninstall php71 and start again, but when I ran brew uninstall php@7.1 and brew uninstall php71, both returned:

Error: Permission denied @ rb_file_s_lstat - /usr/local/Cellar/php@7.1/7.1.17/var/log/php-fpm.log

I decided to investigate exactly what was happening so I navigated via the command line to /usr/local/Cellar/ and then opened the folder in finder using open ..

On doing so, noticed there was a symlink to the php71 folder. Navigating to it, I found there were two versions of php7.1 installed. That made sense since the error suggests that two instances of mcrypt were running. Deleting the older version resolved the issue. That's it. However, that wasn't the root of the problem. Ultimately the issue is that Brew wasn't removing the old symlinks.

Until they fix the issue, the best way to resolve the issue is when you see it unlinking 0 symlinks is to go in and manually remove the old file yourself.