Install Sphinx search engine on OS X Lion

As of September 2012, the sphinx port available via macport is still the very old version 0.9.9, released more than 2 years ago. Stable version today is already 2.0.5, released on July 28th, 2012. No other choices than compiling it yourself if you want real time index support, and other cool things not available in 0.9.9.

Install Sphinx

To compile yourself, you have to install all the basic library required by sphinx. In this case, we suppose we will need the mysql library to connect the engine to mysql directly, and the libstemmer library (for stemming).

I’ll use macport to install various things

  1. Install mysql via macport
    sudo port install mysql5
  2. Install LibIconv
    Iconv in OS X is a little special, install another version via macport

    sudo port install libiconv
  3. Download Sphinx
    Download the latest version of sphinx at http://sphinxsearch.com/downloads/. We will need the source tarball file.

    Uncompress the folder.

  4. Download libstemmer library
    at : http://snowball.tartarus.org/download.php.

    Download the “C version of the libstemmer library“, and uncompress it. Copy the content of the newly uncompressed folder (named libstemmer_c), to the libstemmer_c folder located in the sphinx folder (the folder created by uncompressing your sphinx download in step 3).

    Be careful, there’s already a folder named libstemmer_c, we want to add the files in that folder, don’t just replace/overwrite the folder with the libstemmer_c library folder you just downloaded.
  5. Configure and Install Sphinx
    We’re ready to compile sphinx. Sphinx documentation says to use

    ./configure
    make -j4
    make install

    By these steps doesn’t apply to os x (anymore), since os x lion is 64bits, and the iconv library on os x is somewhat special, and mysql can not be autodetected.

    Correct configuration for OS X Lion, to enable mysql and libstemmer is :

    CFLAGS='-arch x86_64' \\
    CCFLAGS='-arch x86_64' \\
    CXXFLAGS='-arch x86_64' \\
    CPPFLAGS="$CPPFLAGS -I/opt/local/include" \\
    LIBS="$LIBS -L/opt/local/lib"  \\
    ./configure \\
    --with-mysql-includes=/opt/local/include/mysql5/mysql/ \\
    --with-libstemmer \\
    --with-mysql-libs=/opt/local/lib/mysql5/mysql/

    It enables 64 bits support, use the /opt/local path primary when looking for libraries (to force using the macport iconv library instead of the default one), and specify the path to the mysql libraries. You can throw in a --prefix=/usr/local/sphinx if you want to install it elsewhere.

    If you’re not using the macport Mysql, replace the mysql library and include path in the command above with your own.

    Run the command above, and then

    make -j4
    sudo make install

    to compile and install it.

  6. To confirm it’s installed, run
    indexer

    and it’ll spout:

    Sphinx 2.0.4-release (r3135)
    Copyright (c) 2001-2012, Andrew Aksyonoff
    Copyright (c) 2008-2012, Sphinx Technologies Inc (http://sphinxsearch.com)
    ...

    and a lot of other things.

Install Sphinx PHP extension

2 methods :

Via PEAR

pear install sphinx

It will install the latest stable release.

Compile it yourself

Sometime, you’ll want to install another version than the latest stable, such as a beta, release candidate, or just compile it yourself because you have a special config that can’t be install with pear.

  1. Go to Sphinx PECL homepage, and download the release you want.
  2. Uncompress it
  3. cd inside
  4. phpize
  5. ./configure # optionally, with some --options
  6. make
  7. make install # sudo if you're not root

Optionally, you could use the very very latest build, that is the svn version. It’s sometime worth it, when there’s not new features, and all commits are bug fixes, that aren’t made into a release yet.

Clone the svn repo instead of downloading the archive in step 1 and 2

svn co http://svn.php.net/repository/pecl/sphinx/trunk sphinx_trunk
Don’t forget to restart apache after installation: sudo apachectl restart

The sphinx php extension isn’t available in macport, and I don’t know its status in Homebrew.