Homebrew mcrypt mismatch PHP Startup: mcrypt: Unable to initialize module

This has happened to me a few times over the years and I always forget how to fix it. I’m scribbling it down here as much for myself as I am for you.

The problem

When you install php71-mcrypt using:

brew install php71-mcrypt

Brew will install the php71 mcryprypt extension, but when you then use php you’ll note that mcrypt doesn’t work. If you restart php and you get something like this:

PHP Warning:  PHP Startup: mcrypt: Unable to initialize module
Module compiled with build ID=API20160303,NTS
PHP    compiled with build ID=API20160303,NTS,debug

The fix

Is thankfully, very very simple. Run:

brew reinstall php71-mcrypt --build-from-source

Boom! Now it works.

James