Installing geoip php library and geoip standard on OS X 10.7 (Lion) for use with MAMP using PECL and Homebrew

I needed to install the GeoIP library for use on a PHP Application, so i tried to download the libraries from MaxMind website, i found that to be little to none helpful, having had experience with libraries and MAMP i started to wonder what is the best way to install this?, my initial idea was spot on! PECL,
i had to first find the version of PECL bundled with MAMP, this isn’t really hard just locate your PHP Version (5.3.6 for me) then try to locate it under MAMP’s Application folder

rroche at Ramon-Roches-MacBook-Pro in /Applications/MAMP/bin/php/php5.3.6
$ la
total 32
drwxr-xr-x 8 rroche admin 272 Dec 5 12:01 .
drwxr-xr-x 5 rroche admin 170 Sep 29 16:34 ..
drwxr-xr-x 14 rroche admin 476 Nov 28 16:54 bin
drwxr-xr-x 5 rroche admin 170 Dec 5 12:37 conf
drwxr-xr-x 39 rroche admin 1326 Dec 5 12:05 include
drwxr-xr-x 4 rroche admin 136 Sep 29 16:34 lib
drwxr-xr-x 3 rroche admin 102 May 25 2011 modules

your php utitlies are located under bin/ so my absolute PECL path is located under

/Applications/MAMP/bin/php/php5.3.6/bin/pecl

Installing GeoIP from Homebrew

So the actual db needs to be installed, luckily for us someone already created a brew formula for this

rroche at Ramon-Roches-MacBook-Pro in /Applications/MAMP/bin/php/php5.3.6/include/php
$ brew install geoip

as easy as that, the PECL installer will take care of the rest

Installing GeoIP PHP libraries from PECL

once I had located PECL I tried searching and installing GeoIP

rroche at Ramon-Roches-MacBook-Pro in ~/
$ /Applications/MAMP/bin/php/php5.3.6/bin/pecl search geoip</code>
 
Retrieving data...0%
Matched packages, channel pecl.php.net:
=======================================
Package Stable/(Latest) Local
geoip 1.0.8 (stable) 1.0.8 Map IP address to geographic places
 
$ /Applications/MAMP/bin/php/php5.3.6/bin/pecl install geoip

Only to find it fail under some missing php dependencies located under /Applications/MAMP/bin/php/php5.3.6/include/php
which on my setup where missing, a quick Google search and I found that i had to download the MAMP Component Zip file from MAMP main site ( you can find the download section here http://www.mamp.info/en/downloads/index.html )
once you downloaded locate the php source zip that relates to what you are using, in my case php-5-3-6.zip (or something similar), unzip it and if your php folder is named php-5-3-6 rename it to simply php then move it to

/Applications/MAMP/bin/php/php5.3.6/include

once there go to the php directory you just moved

cd /Applications/MAMP/bin/php/php5.3.6/includes/php

you will need to configure php to create some directories you will need

rroche at Ramon-Roches-MacBook-Pro in /Applications/MAMP/bin/php/php5.3.6/include/php
$ ./configure
$ (.. all the config verbose ..)

once you have done that, you can try to install geoip from PECL again this time it wont fail, (in my case I had to do `sudo` I’m not sure if this is due to something I previously did I tend to play with my folder permissions and owners)

rroche at Ramon-Roches-MacBook-Pro in /Applications/MAMP/bin/php/php5.3.6/include/php
$ /Applications/MAMP/bin/php/php5.3.6/bin/pecl install geoip

at the end of the installation process you will notice it warns you about adding a reference to the geoip extension to your php.ini file

extension=geoip.so

open MAMP then go to File -> Edit Template -> PHP -> PHP 5.3.6 and add the following almost at the very end

[geoip]
extension=geoip.so

only followed by

; DONT REMOVE: MAMP PRO php5.3.6.ini template compatibility version: 1

Now restart apache, from MAMP just Stop and Start the services, and you will be all done.

  • simon

    thank you ramon, i appreciate your help.
    was really useful your guide. now I’m trying to do it

  • admin

    Thanks Simon let me know how it goes.

  • Marc

    I followed your instructions with adding the new php folder, and it still wouldn’t install the geoip extension properly.

    There is a typo in the tutorial, I think.
    Is the folder that you put the new php in called include or includeS?

  • Marc

    Scratch that, I installed the home brew version and it worked.
    But now, my mamp doens’t have a “file” menu.. I’m using 2.0.5. Could this be the problem?

    How can I edit the php.ini without the file menu?

  • admin

    Hi Marc, I’m not completely sure but I think you can find the php.ini here (depending on the php version you need )

    /Applications/MAMP/bin/php/php5.3.6/conf

    Pretty weird about the missing file menu on MAMP

  • Marc

    I figured out that it’s because I’m using MAMP and not MAMP PRO.
    I found the file itself, but I’m wondering what should I use to edit it?

  • admin

    any text editor will work, VIM, TextMate, TextEdit

  • Marc

    Did it with textedit, and after restating MAMP.. the php commands for geoip don’t do anything?

    The installation appears to be successful (according to the output from Terminal), and I added that last bit to my php.ini,
    but it doesn’t output anything when my .php file contains:

  • Marc

    $country = geoip_country_name_by_name(‘www.example.com’);
    if ($country) {
    echo ‘This host is located in: ‘ . $country;
    }

  • admin

    Maybe we got the ini path wrong, verify the version of php your mamp is configured to use, then try this

    /Applications/MAMP/bin/php/{PHP_VERSION}/bin/php –ini

    eg:
    /Applications/MAMP/bin/php/php5.3.6/bin/php –ini

    this will output something similar to this:

    $ ./php –ini
    Configuration File (php.ini) Path: /Applications/MAMP/bin/php/php5.3.6/conf
    Loaded Configuration File: /Applications/MAMP/bin/php/php5.3.6/conf/php.ini
    Scan for additional .ini files in: (none)
    Additional .ini files parsed: (none)

  • http://www.unicycle.co.nz Steve

    Thanks. This really helped. Your instructions provided the missing pieces i needed…

    I’m using MAMP Pro with php 5.4.4. The Component Zip does not include the right version, so I downloaded the php 5.4.4 source from http://www.php.net/releases, unzipped and renamed as per your instructions and it all worked.

  • Locrizak

    There are a lot of things that I had to do that were not in this tutorial.
    First, follow this tutorial on why PECL extensions cannot be installed (./configure in /includes/php after doing this)
    Second you should download the GeoIPCity.dat from here: http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz. You then need to rename it to “GeoIPCity.dat” and place it in “/opt/local/share/GeoIP/” to get the IP lookup.

    How I can help out someone