Installing mcrypt extension for PHP on OSX Mountain Lion

112,439

Solution 1

I tend to use Homebrew on Mac. It will install and configure all the stuff for you.
http://mxcl.github.com/homebrew/

Then you should be able to install it with brew install mcrypt php53-mcrypt and it'll Just Work (tm).

You can replace the 53 with whatever version of PHP you're using, such as php56-mcrypt or php70-mcrypt. If you're not sure, use brew search php.

Do also remember that if you are using the built in Mac PHP it's installed into /usr/bin you can see which php you are using with which php at the terminal and it'll return the path.

Solution 2

I just went through this on Mountain Lion. Homebrew blocked on libiconv which it thought was missing but was actually up to date. After an hour of trying to get it to recognize libiconv, I gave up and installed it the old fashion way, which took all of five minutes...

(download your php version)
$ wget http://www.php.net/get/php-5.3.21.tar.gz/from/a/mirror
$ tar -xvzf php-5.3.21.tar.gz
$ cd php-5.3.21/ext/mcrypt
$ phpize
$ ./configure
$ make
$ make test
$ sudo make install 

mcrypt.so is now in your PHP ext dir (/usr/lib/php/extensions/no-debug-non-zts-20090626/ in my case), now you need to add to php.ini as a module

$ vi /etc/php.ini
$ (insert) extension=mcrypt.so
$ sudo apachectl restart

Done - no brew necessary. HTH someone.

Solution 3

Installing php-mcrypt without the use of port or brew

Note: these instructions are long because they intend to be thorough. The process is actually fairly straight-forward. If you're an optimist, you can skip down to the building the mcrypt extension section, but you may very well see the errors I did, telling me to install autoconf and libmcrypt first.

I have just gone through this on a fresh install of OSX 10.9. The solution which worked for me was very close to that of ckm - I am including their steps as well as my own in full, for completeness. My main goal (other than "having mcrypt") was to perform the installation in a way which left the least impact on the system as a whole. That means doing things manually (no port, no brew)

To do things manually, you will first need a couple of dependencies: one for building PHP modules, and another for mcrypt specifically. These are autoconf and libmcrypt, either of which you might have already, but neither of which you will have on a fresh install of OSX 10.9.

autoconf

Autoconf (for lack of a better description) is used to tell not-quite-disparate, but still very different, systems how to compile things. It allows you to use the same set of basic commands to build modules on Linux as you would on OSX, for example, despite their different file-system hierarchies, etc. I used the method described by Ares on StackOverflow, which I will reproduce here for completeness. This one is very straight-forward:

$ mkdir -p ~/mcrypt/dependencies/autoconf
$ cd ~/mcrypt/dependencies/autoconf
$ curl -OL http://ftpmirror.gnu.org/autoconf/autoconf-latest.tar.gz
$ tar xzf autoconf-latest.tar.gz
$ cd autoconf-*/
$ ./configure --prefix=/usr/local
$ make
$ sudo make install

Next, verify the installation by running:

$ which autoconf

which should return /usr/local/bin/autoconf

libmcrypt

Next, you will need libmcrypt, used to provide the guts of the mcrypt extension (the extension itself being a provision of a PHP interface into this library). The method I used was based on the one described here, but I have attempted to simplify things as best I can:

First, download the libmcrypt source, available from SourceForge, and available as of the time of this writing, specifically, at:

http://sourceforge.net/projects/mcrypt/files/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.bz2/download

You'll need to jump through the standard SourceForge hoops to get at the real download link, but once you have it, you can pass it in to something like this:

$ mkdir -p ~/mcrypt/dependencies/libmcrypt
$ cd ~/mcrypt/dependencies/libmcrypt
$ curl -L -o libmcrypt.tar.bz2 '<SourceForge direct link URL>'
$ tar xjf libmcrypt.tar.bz2
$ cd libmcrypt-*/
$ ./configure
$ make
$ sudo make install

The only way I know of to verify that this has worked is via the ./configure step for the mcrypt extension itself (below)

building the mcrypt extension

This is our actual goal. Hopefully the brief stint into dependency hell is over now.

First, we're going to need to get the source code for the mcrypt extension. This is most-readily available buried within the source code for all of PHP. So: determine what version of the PHP source code you need.

$ php --version # to get your PHP version

now, if you're lucky, your current version will be available for download from the main mirrors. If it is, you can type something like:

$ mkdir -p ~/mcrypt/php
$ cd ~/mcrypt/php
$ curl -L -o php-5.4.17.tar.bz2 http://www.php.net/get/php-5.4.17.tar.bz2/from/a/mirror

Unfortunately, my current version (5.4.17, in this case) was not available, so I needed to use the alternative/historical links at http://downloads.php.net/stas/ (also an official PHP download site). For these, you can use something like:

$ mkdir -p ~/mcrypt/php
$ cd ~/mcrypt/php
$ curl -LO http://downloads.php.net/stas/php-5.4.17.tar.bz2

Again, based on your current version.

Once you have it, (and all the dependencies, from above), you can get to the main process of actually building/installing the module.

$ cd ~/mcrypt/php
$ tar xjf php-*.tar.bz2
$ cd php-*/ext/mcrypt
$ phpize
$ ./configure # this is the step which fails without the above dependencies
$ make
$ make test
$ sudo make install

In theory, mcrypt.so is now in your PHP extension directory. Next, we need to tell PHP about it.

configuring the mcrypt extension

Your php.ini file needs to be told to load mcrypt. By default in OSX 10.9, it actually has mcrypt-specific configuration information, but it doesn't actually activate mcrypt unless you tell it to.

The php.ini file does not, by default, exist. Instead, the file /private/etc/php.ini.default lists the default configuration, and can be used as a good template for creating the "true" php.ini, if it does not already exist.

To determine whether php.ini already exists, run:

$ ls /private/etc/php.ini

If there is a result, it already exists, and you should skip the next command.

To create the php.ini file, run:

$ sudo cp /private/etc/php.ini.default /private/etc/php.ini

Next, you need to add the line:

extension=mcrypt.so

Somewhere in the file. I would recommend searching the file for ;extension=, and adding it immediately prior to the first occurrence.

Once this is done, the installation and configuration is complete. You can verify that this has worked by running:

php -m | grep mcrypt

Which should output "mcrypt", and nothing else.

If your use of PHP relies on Apache's httpd, you will need to restart it before you will notice the changes on the web. You can do so via:

$ sudo apachectl restart

And you're done.

Solution 4

Why You're Getting This Error

PHP complains if one of the files like mcrypt.so is included using the syntax extension="mcrypt.so" but the file is not in the extension_dir path ( use <?php phpinfo(); ?> or php -i to check that).

It will also tell you which php.ini config file is being loaded so you will know where the settings are coming from. Most likely it will be something like /usr/local/etc/php/5.4/php.ini if you are using the homebrew version.

Take note of the part under it that says something like "Scan this dir for additional .ini files" because what that means is it gives you a place to put your own file, like tweaks.ini that is loaded after the main configuration file so that you can make changes and keep up with them easily. Also remember that all the files in this directory get loaded in alphabetical order, so if you have one called adjustments.ini that contains mcrypt directives, and there is a mcrypt.ini, most likely your settings will be overridden.

One alternative to specifying extension="mcrypt.so" is to specify the full path to the mcrypt.so file. The other option is to edit the extension_dir setting.

What worked for me

On Mavericks I didn't have to do either. I did a fresh install of homebrew and then added the josegonzalez tap using:

brew tap josegonzalez/homebrew-php

(My other laptop was running Mountain Lion and was also using homebrew in this setup.)

After you've tapped that awesome repo you can install php and mcrypt using something like:

brew install php54 php54-mcrypt

What if this doesn't work (and why should I use homebrew anyway?)

I would highly advise trying this route before downloading and building it from source. It's not hard to build from source - but I don't want to have to maintain that. It's one of the reasons to use homebrew in the first place - it's a package manager (with a HUGE community).

There is a lot of development on the homebrew project and - if you have problems I'd suggest checking out their issues page

So yes you can build it from source and that might seem like a good option right now if you just want mcrypt to work but you may hate yourself for doing this later...

If you don't want to be using php54 there is also the php53 branch. They have some instructions at the repo about how to use both of them / switch between them.

If you're new to homebrew you should know that you check out what else is available using brew search php54, which gives something like:

php54                php54-lzf          php54-snappy        
php54-amqp           php54-mailparse    php54-solr          
php54-apc            php54-mcrypt       php54-ssh2          
php54-apcu           php54-memcache     php54-stats         
php54-boxwood        php54-memcached    php54-svm           
php54-chdb           php54-midgard2     php54-tidy          
php54-couchbase      php54-mongo        php54-timezonedb    
php54-dbase          php54-msgpack      php54-tokyotyrant   
php54-ev             php54-mysqlnd_ms   php54-twig          
php54-gearman        php54-oauth        php54-uploadprogress
php54-geoip          php54-opcache      php54-uuid          
php54-gmagick        php54-parsekit     php54-varnish       
php54-graphdat       php54-pcntl        php54-wbxml         
php54-http           php54-pdflib       php54-xcache        
php54-igbinary       php54-phalcon      php54-xdebug        
php54-imagick        php54-proctitle    php54-xhgui         
php54-inclued        php54-pspell       php54-xhp           
php54-intl           php54-pthreads     php54-xhprof        
php54-ioncubeloader  php54-raphf        php54-xmldiff       
php54-jsmin          php54-redis        php54-yac           
php54-judy           php54-riak         php54-yaf           
php54-leveldb        php54-runkit       php54-yaml          
php54-libevent       php54-scrypt       php54-yaz           
php54-libvirt

TLDR

  • You should use homebrew to install mcrypt if at all possible
  • If you're getting errors it's probably because your configuration file(s) are messed up. Check the extension_dir path and figure out where the mcrypt.so file is and see if there is a discrepancy (or specify the full path)

Solution 5

For me, on Yosemite

$ brew install mcrypt php56-mcrypt

restart computer

did the trick.

Share:
112,439
Admin
Author by

Admin

Updated on July 06, 2020

Comments

  • Admin
    Admin almost 4 years

    Apologies in advance for the potential n00b questions, I am trying to install the mcrypt extension for PHP on my OSX Mountain Lion machine.

    The following steps in terminal is what I have done so far to achieve my PHP install

    cd /path/to/downloaded/php-5.3.21/ext/mcrypt/
    /usr/bin/phpize
    ./configure
    cd /path/to/downloaded/php-5.3.21
    ./configure --with-config-file-path=/private/etc/php.ini --with-apxs2=/usr/sbin/apxs
    make
    sudo make install
    

    Which seems to work well and installs PHP 5.3.21 fine. I have then done

    sudo nano /private/etc/php.ini
    

    And included

    extension=mcrypt.so
    

    Along with an Apache restart, phpinfo() doesn't show that the mcrypt extension is loaded.

    I then tried to specify the extension_dir inside php.ini, again with no luck.

    I have done

    locate mcrypt.so
    /opt/local/lib/php/extensions/no-debug-non-zts-20090626/mcrypt.so
    /usr/local/Cellar/php53-mcrypt/5.3.18/mcrypt.so
    

    And tried both directories as the extension_dir, with no luck.

    I have also tried the following, after much Googling

    ./configure --with-config-file-path=/private/etc/php.ini --with-apxs2=/usr/sbin/apxs --with-mcrypt
    

    Which seems to work OK, but then upon "make", it returns

    ext/mcrypt/mcrypt.o: No such file or directory
    ext/mcrypt/mcrypt_filter.o: No such file or directory
    

    Again, no success.

    What am I doing wrong? It seems like the physical compile of mcrypt.so is not happening, or is compiling incorrectly as I would suspect there to be another mcrypt.so found under locate?

    Anyone please help? I've gone through pages upon pages of Google searches with no luck!