PHP with Intl and Gettext on OSX Lion

Mac OSX Lion ships with PHP 5.3.6. There are also other ways to get PHP working on Lion, for example:

These are all very good solutions, but since I prefer to travel light and the version in Lion already comes with a lot of useful extensions, I preferred to go with it.

Here is the list of extensions installed with Lion:

bcmath, bz2, calendar, Core, ctype, curl, date, dba, dom, ereg, exif, fileinfo, filter, ftp, gd, hash, iconv, json, ldap, libxml, mbstring, mysql, mysqli, mysqlnd, odbc, openssl, pcre, PDO, pdo_mysql, pdo_pgsql, pdo_sqlite, pgsql, Phar, posix, readline, Reflection, session, shmop, SimpleXML, snmp, soap, sockets, SPL, SQLite, sqlite3, standard, sysvmsg, sysvsem, sysvshm, tidy, tokenizer, wddx, xdebug, xml, xmlreader, xmlrpc, xmlwriter, xsl, zip, zlib

In /etc, there is a php.ini.default file that is not activated, so you will have to modify it to suit your preferences and rename it to /etc/php.ini. For instance, Lion also ships with Xdebug, but it is not activated by default. To activate it, you’ll have to remove the semi-colon on the line that says:

zend_extension="/usr/lib/php/extensions/no-debug-non-zts-20090626/xdebug.so"

Lion doesn’t provide neither PEAR nor PECL by default but it is easy to activate them because Apple provide the required php archive file (phar) for that:

cd /usr/lib/php
sudo php install-pear-nozlib.phar

Then edit the php.ini file and add:

include_path = ".:/usr/lib/php/pear"
extension_dir = "/usr/lib/php/extensions/no-debug-non-zts-20090626"

Then you can update your PEAR/PECL channels:

sudo pear channel-update pear.php.net
sudo pecl channel-update pecl.php.net
sudo pear upgrade-all

If like me you are missing the Intl extension, here is how I installed it (warning : requires Xcode):

  1. Download and install ICU
    tar xzvf icu4c-4_8_1-src.tgz
    cd icu/source
    ./runConfigureICU MacOSX
    make
    sudo make install
  2. Download PHP 5.3.6 sources (update: I tried with 5.3.8, I got a compilation error, so use 5.3.6)
  3. Unpack
  4. Change directory to ext/intl
  5. Use the following commands in Terminal:
    cd ext/intl
    phpize
    ./configure --enable-intl
    make
    sudo cp modules/intl.so /usr/lib/php/extensions/no-debug-non-zts-20090626/
  6. Activate intl in your php.ini file with
    extension=intl.so

Since I mostly work on multilingual sites, I also needed to install Gettext (it’s unfortunate that Apple will not ship gettext and intl by default, a lot of PHP developers use more than English, but that might be another licence issue…). The problem is that Gettext apparently defines stpncpy function, as does something in Lion…

As suggested by Stephen Yeargin in the comments, you can apply a small patch on Gettext that will modify the configure script to make sure function stpncpy is not already defined.

tar xzvf gettext-0.18.1.1.tar.gz
cd gettext-0.18.1.1
# apply patch here, then :
./configure
make
sudo make install

Then, back in the PHP sources directory:

cd ext/gettext
phpize
./configure --with-gettext
make
sudo cp modules/gettext.so /usr/lib/php/extensions/no-debug-non-zts-20090626/

And add this to the php.ini file:

extension=gettext.so

Finally I just restarted Apache from the Sharing Preference pane and I’m ready to start coding.

Updates 02/24/2012: Don’t use 5.3.8. Use the patch suggested in the comments for Gettext to compile.

This entry was posted in mac, php. Bookmark the permalink.

6 Responses to PHP with Intl and Gettext on OSX Lion

  1. Pingback: How do I upgrade intl extension to ICU 4+ on OS X 10.7 Lion with PHP 5.3.8-ZS5.5.0 | SeekPHP.com

  2. Hello,

    very nice guide, i did the same on lion, but with a few tweaks. I don’t like to compile things by hand so i use Homebrew for icu4c and gettext.

    The difference between your guide and my setup is:

    “brew install icu4c” and configure using “./configure –enable-intl –with-icu-dir=/usr/local/Cellar/icu4c/4.8.1″

    “brew install gettext” and configure using “./configure –with-gettext=/usr/local/Cellar/gettext/0.18.1.1″

    Works like a charm!

  3. William Arkine says:

    Bonjour,

    J’ai installé Gettex sur Lion (avec succès) en suivant la méthode donnée ici : http://techshow.me/doc/276 (j’ai en effet dû modifier la ligne 34 pour que cela fonctionne).
    Gettex se trouve dans : /usr/local/lib

    Pourtant le setup de la webgallerie que je suis en train de configurer me renvoie l’erreur suivante :
    PHP gettext() support [is not present].
    Php ne ” reconnaît ” donc pas Gettex.
    Les logout et restart n’ont rien changé.

    J’ai ensuite découvert votre blog et je suppose, puisque Gettex est installé, que j’ai au moins effectué l’équivalent de ce que vous décrivez ici :

    tar xzvf gettext-0.18.1.1.tar.gz
    cd gettext-0.18.1.1
    # edit gettext-tools/gnulib-lib/stpncpy.c
    ./configure
    make
    sudo make install

    Je cale sur la suite car je ne comprends pas ” back in the PHP sources directory: ” et ” cd ext/gettext “.
    Pouriez-vous me dire quel est le path des “sources PHP ” et aussi à quoi correspond ” ext/gettext ” ?

    Et aussi : pourrai-je garder Gettex installé ou devrai-je tout reprendre à zéro en suivant votre méthode ?

    Par avance merci de votre réponse et de votre indulgence. Bien qu’ayant installé Apache, php, mySql, et autres Mcrypt sans trop de souci, je ne suis pas du tout informaticien !

    Bien à vous,

    William Arkine

    Lion 10.7.2 – XCode 4.2 – Apache 2.2.2. – PHP 5.3.6 – Mysql 5.0.8 – Gettext 0.18.1.1

    • @william Tu dois télécharger les sources de PHP pour pouvoir compiler l’extension Gettext, même si PHP est déjà installé. C’est contraignant mais il n’y a pas d’autre solution car c’est là que résident les codes sources des extensions PHP. Donc, il faut décompresser PHP, aller dans le répertoire ext/gettext des sources de PHP comme indiqué dans mon guide, et suivre les consignes. Si Gettext est bien installé, pas besoin de le réinstaller. Bon courage.

  4. William Arkine says:

    Merci beaucoup pour votre réponse !

    Je n’ai malheureusement pas le temps de le faire ces jours-ci mais, dès que cela sera possible, je vous dirai si j’ai réussi.

    Bien à vous,

    William