Installing Sphinx on OS X for PHP

I’m starting to use sphinx in my work, and wanted to get a solid development environment set up for it on my local OS X server. Since I built my local server with Mac Ports, it was actually pretty easy to get that installed:

sudo port install sphinx

However, I had trouble setting up the PHP extension for the Sphinx API. I could have used the sphinxapi.php that ships with the sphinx source code, but having a compiled extension is faster, and I don’t have to add more files to my php project. Installing Sphinx via Mac Ports didn’t help either, because it doesn’t install libsphinxclient, which is required to build the PHP Extension. That was until I found some instructions from someone doing something similar for Ruby.

Here are the steps I took to get the PHP extension up and running:

1. Download the latest sphinx source (0.98.1 as of this writing) from the Sphinx Search site, and uncompress the tarball.
2. Now compile libsphinxclient:

cd ./api/libsphinxclient
CXXCPP="gcc -E" ./configure --prefix=/opt/sphinx
make
sudo make install

3. Now download the latest sphinx client source (1.0.0 as of this writing) from the PECL site, and uncompress the tarball.
4. Now install the extension:

cd ./sphinx-1.0.0
phpize
./configure --with-sphinx=/opt/sphinx
make
sudo make install

5. Add the extension to php.ini and restart apache:

[sphinx]
extension=sphinx.so

6. Check to see if you’ve got it installed correctly. You should see sphinx support => enabled in the output:
php -i | grep sphinx

Now go rock the casbah and build a custom search engine with Sphinx Search!

5 Responses to “Installing Sphinx on OS X for PHP”


  • Awesome! Thanks for the guide, this saved me a lot of trouble.

  • Your article was really helpful. Initially, I wasn’t able to compile the libsphinxclient because I used the sphinx source tarball downloaded by macports. After I downloaded the tarball from the sphinx website, all was well.

  • Nice tut, I have wanted to take sphinx for a test drive for ages, I have a search engine type app I have built as a means to learn php and it really needs a full index and search function, mysql is just to slow but fits the bill for now. Sphinx or lucene fit the bill perfectly but look quite difficult to get to grips with. If I can use this and it works I don’t think I’ll bother trying the latter.

    Out of interest, have you had to install sphinx on a live web server?

    I noticed in cpanel there are some add-ons in PEAR for lucene and sphinx, you need root level access to the server to install, hence I haven’t made the jump. My domain is not on a dedicated server or VPS as yet.

    • I have put it on a live web server. Lucene’s index is quite different from MySQL and Sphinx. If you can get it running Sphinx will be a lot easier to manage. Lucene, especially the Zend_Lucene implementation is slower and requires an entirely rethought approach. In other words, you can’t apply Relational Database thinking to Lucene and expect to get very far. You’ll run into weird issues with the index and with pulling results back.

      Installing Sphinx on a VPS should be very straight forward, especially if you’re on Debian or Ubuntu.

Leave a Reply