Friday, January 4, 2013

Compiling mongo.so for use with MAMP

So I had a little bit of trouble getting the latest version of the PHP driver for MongoDB (1.3.2) to use with MAMP. The PHP documentation's direction for MAMP users is to go to https://github.com/mongodb/mongo-php-driver/downloads and download the latest OSX driver for your PHP version (I'm using 5.3.6), which happens to be 1.0.1.1 from two years ago. Unfortunately the documentation on php.net for MongoDB has been updated, and if you aren't using the latest driver, you might be getting errors indicating that some classes don't exist.

Other instructions indicate you can just run
sudo pecl install mongo
Or
sudo pecl upgrade mongo
But this seemed to be for those using the XAMPP Developer Package. I was having trouble compiling an updated mongo.so file because my MAMP php installation was missing the necessary header files. I would get this error:
fatal error: 'php.h' file not found
#include <php .h>
         ^
1 error generated.
make: *** [php_mongo.lo] Error 1
ERROR: `make' failed
I noticed when it tried to run phpize it couldn't find any of these:
grep: /Applications/MAMP/bin/php/php5.3.6/include/php/main/php.h: No such file or directory

grep: /Applications/MAMP/bin/php/php5.3.6/include/php/Zend/zend_modules.h: No such file or directory

grep: /Applications/MAMP/bin/php/php5.3.6/include/php/Zend/zend_extensions.h: No such file or directory
So I ran locate on them, and they did exist in Xcode:
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.6.sdk/usr/include/php/main/php.h

/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.6.sdk/usr/include/php/Zend/zend.h
I tried copying the whole /include/php directory to my php directory in MAMP. I had to make an include folder in /Applications/MAMP/bin/php/php5.3.6/
cp -r /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.7.sdk/usr/include/php /Applications/MAMP/bin/php/php5.3.6/include
When I ran 'sudo pecl upgrade mongo' it completed. I just had to restart apache/mysql and phpinfo() showed I was now using the 1.3.2 MongoDB driver.