Menu

Michael Gracie

Plugging mcrypt into PHP on macOS Sierra 10.12

We climbed up then down El Capitan, yet little changed. We’re still in the Sierras, and there’s still work to do.

The following instructions are for peeps a) developing on macOS Sierra 10.12, b) need mcrypt for their PHP development i.e. PHPMyAdmin and/or encrypting personally identifiable data before storage (unlike your health insurer, Yahoo! and/or pretty much every other knucklehead out there), and c) do not want to recompile PHP or run MAMP. Mcrypt will load dynamically with PHP after completing this tutorial.

We begin by acquiring the following stuffs …

1) libmcrypt-2.5.8, which you can find here; download by clicking the one of the two file links (author used “libmcrypt-2.5.8.tar.gz”);

2) PHP 5.6.24 source code, which is available here; [NOTE: you may someday update OS X beyond 10.12, and PHP may get updated along with it; the author used 10.12, and PHP 5.6.24 was included with that OS version. If necessary use php -v to check your version of PHP and then download the PHP source for that version.];

3) Xcode 8.0, which you can get from the App Store. You will also need the Command Line Tools (macOS 10.12) for Xcode, which you get by selecting “Xcode/Open Developer Tool/More Developer Tools…” from the Xcode menu, then logging into your Apple Developer account (yea you need one of those too); and

4) Homebrew (http://brew.sh) which can be installed by typing ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/ install/master/install)" at the command line.

But before we really wind up, let’s turn off System Integrity Protection (SIP):

  1. Click the Apple menu (upper left hand corner of your screen)
  2. Select Restart, then hold down the Command-R keys to boot into Recovery Mode
  3. Select the Utilities menu and then Terminal
  4. In Terminal, type csrutil disable and then hit return; then close Terminal
  5. Click the Apple menu and select Restart

NOTE: When you are done installing mcrypt, you can restart SIP by following the above steps while substituting csrutil enable in step 4.

Proceed.

Open Terminal

Create a directory at root called ‘sourceFiles’ (or something else you can remember) and drop items #1 and #2 in there. Then unzip them using the following commands …

  • #1 – tar -zxvf libmcrypt-2.5.8.tar.gz
  • #2 – tar -zxvf php-5.6.24.tar.gz

Next, move to the libmcrypt-2.5.8 directory, and type this in …

MACOSX_DEPLOYMENT_TARGET=10.12 CFLAGS='-O3 -fno-common -arch i386 -arch x86_64' LDFLAGS='-O3 -arch i386 -arch x86_64' CXXFLAGS='-O3 -fno-common -arch i386 -arch x86_64' ./configure --disable-dependency-tracking

then type make -j6 and finally sudo make install.

libmcrypt prep complete. Now we build the PHP extension.

Jump back to /sourceFiles and down into php-5.6.24/ext/mcrypt …

Next we use Homebrew to install autoconf – if you haven’t done so before, just type brew install autoconf. Then type /usr/bin/phpize; you should see some output like so:

PHP Api Version: 20131106
Zend Module Api No: 20131226
Zend Extension Api No: 220131226

Configure as follows, by typing the following …

MACOSX_DEPLOYMENT_TARGET=10.12 CFLAGS='-O3 -fno-common -arch i386 -arch x86_64' LDFLAGS='-O3 -arch i386 -arch x86_64' CXXFLAGS='-O3 -fno-common -arch i386 -arch x86_64' ./configure --with-php-config=/usr/bin/php-config

Repeat make -j6, then make test (with a “n” when it asks if you want to send a report), and last but not least sudo make install.

Very last leg

Check for php.ini in the /etc directory. If all you see is php.ini.default, copy and paste it, then rename the copy to php.ini. Open it for edit. Make sure enable_dl = On, and add a single line to the Dynamic Extensions section: extension=mcrypt.so

You now enable PHP by modifying your /etc/apache2/httpd.conf file. Open in your favorite text editor – mine is firmly Sublime Text – and remove the “#” from in front of LoadModule php5_module libexec/apache2/libphp5.so. Save – need permission – and close. Now restart Apache with sudo apachectl start, and point your browser to the php info file I’m not going to tell you how to create.

phpinfo() what say you?

phpver-5-6-24sm

phpstream-5-6-24sm

phpmcrypt-5-6-24sm

MG signing off (because macOS Sierra is not that rocky, but has proven useful in the Rockies)

Comments

Josh says:

Thank You so much!!!

This article was extremely helpful!!

I followed the instruction one by one and every think worked perfectly as described.

Do you have an article about installing intl extension?

Lauryn says:

I followed everything till you got to this part:
Jump back to /sourceFiles and down into php-5.6.24/ext/mcrypt …

What does that mean and what do I do. I went to that older in terminal and continued with the instruction but it said no file so Im guessing Jump back to /sourceFiles and down into php-5.6.24/ext/mcrypt …” means to do something but I dont know what. Please help. Newbie here.

Anyhelp is appreciated greatly

You are in the libmcrypt directory, which is under /sourceFiles; navigate up from said directory into the latter, and then down into /php-5.6.24/ext/mcrypt (which is also under /sourceFiles).

Lauryn says:

I went back to follow everything and i could not get localhost to work at all. I decided to reinstall my OS X and viola. The localhost was working again and mcrypt was also installed and working. thanks for the tutorial and thanks for answering all of our questions. you rock!

Raj says:

I followed everything and it worked until I ran /usr/bin/phpize and got this:

grep: /usr/include/php/main/php.h: No such file or directory
grep: /usr/include/php/Zend/zend_modules.h: No such file or directory
grep: /usr/include/php/Zend/zend_extensions.h: No such file or directory
Configuring for:
PHP Api Version:
Zend Module Api No:
Zend Extension Api No:
mkdir: build: Permission denied

I ran again with sudo and it worked, but the PHP Api Version, Zend Module Api No, and Zend Extension Api No were still blank.

Then I tried make -j6 and got this:
make: *** [mcrypt_filter.lo] Error 1
make: *** Waiting for unfinished jobs….
mkdir: .libs: Permission denied
make: *** [mcrypt.lo] Error 1

I ran it again with sudo and got this:
/usr/lib/php/extensions/mcrypt/php-5.6.24/ext/mcrypt/mcrypt_filter.c:21:10: fatal error:
‘php.h’ file not found

Any help is appreciated!

Are you sure PHP is installed? What version?

UPDATE: I probably should have asked if autoconf is installed too.

run xcode-select –install to install the XCode’s Command Line Tools

Pawan says:

when i try to run command “/usr/bin/phpize” inside /usr/bin/phpize inside /sourceFiles/php-5.6.27/ext/mcrypt folde i get –
grep: /usr/include/php/main/php.h: No such file or directory
grep: /usr/include/php/Zend/zend_modules.h: No such file or directory
grep: /usr/include/php/Zend/zend_extensions.h: No such file or directory
Configuring for:
PHP Api Version:
Zend Module Api No:
Zend Extension Api No

php api version is blank and others as you can see.
my os version – 10.12.2
php version – 5.6.27

Pawan says:

Fixed blank version problem(command line tool was missing). now i am confused what are you trying to say, do i need to copy php.ini.default from /etc and paste it in /etc and rename it php.ini or paste it to /sourceFiles/php-5.6.26/ext/mcrypt and rename it.

Thanks.

Just rename it via copy/paste, as it must be in /etc for PHP to run.

Nabin Shrestha says:

I followed every step of the tutorial but can’t get mcrypt extension. What might be the problem? Everything went smooth while following the instructions even though it haven’t worked. Can i find any solution to enable mcrypt extension

There are a lot of very specific steps, hence a lot of potential issues. Unless you’ve seen error messages, the best bet is to start over from scratch as the process is proven (and by many users).

Nabin Shrestha says:

php-5.6.28/ext/mcrypt
when i try to excess in this it say No such file or directory
what might got wrong

Then the directory doesn’t exist relative to where you are. You either didn’t download the correct version, didn’t properly unzip, somehow deleted it, or are just in the wrong directory above that you need to call down to. Try downloading again and repeat the process carefully. Stay aware of where you are in the directory structure while using terminal.

Sascha says:

Thanks a lot for this perfect description.
Could’ve never figured this out on my own

Hassan says:

Hello,
Thanks for the instructions. I followed all the steps exactly, but I get the following when I do /usr/bin/phpize

grep: /usr/include/php/main/php.h: No such file or directory
grep: /usr/include/php/Zend/zend_modules.h: No such file or directory
grep: /usr/include/php/Zend/zend_extensions.h: No such file or directory
Configuring for:
PHP Api Version:
Zend Module Api No:
Zend Extension Api No:
mkdir: build: Permission denied

I checked and found ‘php.h’ under /usr/local/Cellar/php56/5.6.29_5/include/php/main/
While it’s expected to be under /usr/include/php/main/

Can you please help me with this? Thanks in advance.
(macOS Sierra 10.12.3 – PHP 5.6.28)

Hassan says:

Sorry, please remove my question as it was already answered by @Pawan (i.e. missing Xcode Command Line Tools).

Thanks for your post and instruction, very detailed and straight forward
to follow.

However (in my case) I had to do “sudo apachectl restart” at the end instead of ‘start’.

Derek says:

Thanks for the article!

Having an issue starting with running /usr/bin/phpsize. I get this:

-bash: /usr/bin/phpsize: No such file or directory

When trying to build the mcrypt package I am getting this error:

-bash: ./configure: No such file or directory

I installed autoconf with homebrew.

Any thoughts?

Derek says:

Nevermind… typo. phpize.

Ryan says:

Hi I’m having two problems. I’m trying to add mcrypt while also using MAMP. When I follow your instructions, my phpinfo() says I have PHP version 7.1, but “php -v” in terminal says I have 5.6.28. Meanwhile, I’ve downloaded 5.6.24 for the tutorial. MAMP’s preferences are set to v5.6.28 and not 7.1 and I know I downloaded 5.6.28 for your tutorial. What’s going on?? How can I get phpinfo() to show 5.6.x and get all the versions right?

Secondly, when i get to your step about configuring autoconf (autoconf 2.69 is definitely installed) and run this code:

MACOSX_DEPLOYMENT_TARGET=10.12 CFLAGS=’-O3 -fno-common -arch i386 -arch x86_64′ LDFLAGS=’-O3 -arch i386 -arch x86_64′ CXXFLAGS=’-O3 -fno-common -arch i386 -arch x86_64′ ./configure –with-php-config=/usr/bin/php-config

I get this error that I can’t figure out how to fix:



checking for libmcrypt version… >= 2.5.6
checking for mcrypt_module_open in -lmcrypt… no
checking for mcrypt_module_open in -lmcrypt… no
configure: error: Sorry, I was not able to diagnose which libmcrypt version you have installed.

I’ve googled and googled and can’t figure out how to fix that error (I’m a noob).

Any help with these 2 problems would be greatly appreciated as I’m just a lowly web developer and not a terminal expert. Thanks!

Sounds like you have some path issues, and/or have multiple apache instances running? I’ve never had much luck with MAMP – and with macOS all you need is to add MySQL – which is why I assembled this to begin with. It is specifically designed for use without MAMP, so my only suggestion would be removing MAMP, installing MySQL from a community edition binary, and then replaying. You aren’t going to get anywhere with the later steps if the earlier ones are already toast. Note: I’ve heard rumblings about MySQL 5.7 with Sierra; suggest sticking to 5.6.x (I am running 5.6.22 with zero issues).

Steven Waller says:

I was not looking forward to installing mcrypt again after getting a new MacBook, but I followed your instructions and it worked like a charm!!

Thanks for writing this all down!

Oleg G says:

THANK YOU! I have php 5.6.28 for apache from ootb in Mac os Sierra and this trick works even with php 5.6.30 from php.net, no need any new php install from brewhome…

thanks again, so much efforts, and it is not trivial solution helps.

Andrew E says:

Master Michael Gracie,

WOW! So blindly I followed you and so true were your promises of an environment with MAC OS X Sierra + PHP 5.6.24 + mcrypt

Was this so complicated because of Sierra not having and not wanting mcrypt? I tried brew’ing this and it wouldn’t work. Thank you so much again.

[…] place.  After some research I found a clean, working way to install mcrypt.  Michael Gracie did a great write up on this that he posted on his website.  I would recommend going there and following those steps to […]

Jose S. says:

Thanks a lot!!! Awesome tutorial and works perfectly!

Val B says:

Mille GraZie, Michael,
Everything works fine with PHP 5.6.30 + mcrypt too as you described step by step.
Thank you, man!

Mary says:

Hey Michael – your post seems amazing. Unfortunately, when I’m putting in the first “MACOSX_DEPLOYMENT….” I end up getting feedback saying that “configure:2300: error: cannot run C++ compiled programs” – I’m not sure if this is where things get really stuck, or not, but just in case I went ahead and did the make -j6 and it looked as though nothing was able to happen. Any help would be appreciated!! Thanks 🙂

XCode and Command Line Tools installed/up-to-date?

Mary says:

Sorry for the delayed response!! I have Xcode 8.3.2 and Command Line Tools, both seem up to date. Same error though :/

Not sure what is causing this then. Only suggestion right now is to clean out all the files and begin anew.

Chuks Opia says:

Thanks for the tutorial. I followed every instruction without any error but still have the same MCrypt issue when I run composer update in my project folder. I’m new to Mac.
How do I sort this out?

Chuks Opia says:

I should add that the only thing I didn’t do as instructed above was to install autoconf in the PHP…folder. I ran it in the libmcrypt folder.

Best bet is to delete everything and start over, following steps exactly as prescribed.

Chuks Opia says:

You mean delete the libmcrypt and php files and start afresh?

Chuks Opia says:

I’ve done that already. Got no errors but it still did not work

Chuks Opia says:

I did the whole process again and trying to edit my php.ini file in /etc it says it is write protected and I can’t save my changes

Right click on it and hit Get Info; you can change the permissions to Read & Write at the bottom. Make your changes to the file, then revert the permissions back to Read Only.

Chuks Opia says:

Worked! Thanks a lot. 🙂

Or you could try this:

https://brew.sh/
brew install mcrypt
brew install php56-mcrypt
brew install php71-mcrypt

You are my hero and I owe you everything I have!

Leave a Reply to Oleg G Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.