PHP with FreeTDS on OS X Mavericks and Yosemite

Need your OS X-hosted PHP code to talk to Microsoft SQL Server? Here’s the guide I couldn’t find when I needed it.

Or you could just download my Mavericks-ready mssql.so and skip to the end.

Update (6 July 2014): A newer mssql.so compiled for PHP 5.4.24 on Mavericks 10.9.4 can be downloaded here.

Update (22 December 2014): A newer mssql.so compiled for PHP 5.5.14 on Yosemite 10.10.1 can be downloaded here.

Update (16 September 2015): A newer mssql.so compiled for PHP 5.5.27 on Yosemite 10.10.5 can be downloaded here.

Prerequisites

  • Xcode with command line tools (these are installed when you first run Xcode, as of the latest version)
  • Latest autoconf source from http://ftp.gnu.org/gnu/autoconf/ (or Homebrew installed)
  • Latest FreeTDS source from http://www.freetds.org/ (or Homebrew installed)
  • Source for the same version of PHP that ships with OS X (5.4.17 on 10.9; run `php -v` from a terminal if unsure)

Build and install autoconf

If you’re a Homebrew user, brew install autoconf is easier than the following.

$ tar zxf autoconf-latest.tar.gz 
$ cd autoconf-2.69
$ ./configure 
$ make
$ sudo make install

Build and install FreeTDS

FreeTDS is on Homebrew too: brew install freetds

Alternatively:

$ tar zxf freetds-stable.tgz
$ cd freetds-0.91
$ ./configure 
$ make
$ sudo make install

Build and install mssql.so

Don’t worry, unlike some of the Internets will tell you, there’s no need to rebuild PHP itself. Nor do you need to write an essay after .configure.

Update (16 September 2015): phpize doesn’t seem to work out-of-the box anymore. If it can’t find the files it needs (you’ll see grep errors), try adding a symbolic link like this: sudo ln -s /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/usr/include/php /usr/include/php

Here goes:

$ tar zxf php-5.4.17.tar.gz
$ cd php-5.4.17/ext/mssql
$ phpize
$ ./configure --with-php-config=/usr/bin/php-config --with-mssql=/usr/local/
$ make
$ sudo cp modules/mssql.so /usr/lib/php/extensions/no-debug-non-zts-20100525/

Finally, add this line to your php.ini (probably in /etc/php.ini):

extension=mssql.so

And restart Apache if necessary.

Done!

20 thoughts on “PHP with FreeTDS on OS X Mavericks and Yosemite”

  1. Hi Luke, great post, but I have an issue:

    When run the $ phpize, I see this:

    grep: /usr/include/php/main/php.h: No such file or directory
    grep: /usr/include/php/Zend/zend_modules.h: No such file or directory
    grep: /usr/include/php/Zend/zend_extensions.h: No such file or directory
    Configuring for:
    PHP Api Version:
    Zend Module Api No:
    Zend Extension Api No:

    And in $ make I have the folowing error:

    /php-5.4.17/ext/mssql/php_mssql.c:29:10: fatal error: ‘php.h’ file not found
    #include “php.h”

    Can you help me to solve this?

    Thanks
    Nice work!

  2. Hi Luke, great post, but I have an issue:

    In $ make I have the folowing error:

    /php-5.4.17/ext/mssql/php_mssql.c:29:10: fatal error: ‘php.h’ file not found
    #include “php.h”

    Can you help me to solve this?

    Thanks
    Nice work!

    1. Without knowing exactly how you’ve extracted the source, it’s a bit hard to help you troubleshoot this one! I do hope you’ve been able to solve it since you posted.

  3. I had the same problems as Gabriel, but I installed the xcode command lines tools as Lars posted, and now it worked. Thanks for the post

  4. Good post, many thanks.

    Managed to create a mssql.so, added reference to php.ini, restarted apache (AMPPS) I found this error in the apache logs.

    “PHP Warning: PHP Startup: Unable to load dynamic library ‘/Applications/AMPPS/php-5.5/lib/extensions/ext/mssql.so’ – dlopen(/Applications/AMPPS/php-5.5/lib/extensions/ext/mssql.so, 9): no suitable image found. Did find:\n\t/Applications/AMPPS/php-5.5/lib/extensions/ext/mssql.so: mach-o, but wrong architecture in Unknown on line 0”

    Same with the two mssql.so files taht you linked for download.

    How do I select which architecture to compile it for. I have a 64 bit system.

    Thanks again, this post got me further down the road, if not all the way. :)

    1. For: wrong architecture in Unknown on line 0 – this solved my issue:

      Recompile freetds using (brew uninstall if necessary)

      $ tar zxf freetds-stable.tgz
      $ cd freetds-0.91
      $ ./configure LDFLAGS=”-arch i386″ CFLAGS=”-arch i386″
      $ make
      $ sudo make install

      Compile mssql extension with

      ./configure LDFLAGS=”-arch i386″ CFLAGS=”-arch i386″ –with-php-config=/usr/bin/php-config –with-mssql=/usr/local/

  5. I’ve followed your instructions (using your precompiled mssql.so) and I think I have the mssql extension now working on MAMP with PHP5.5.14. I’m still waiting to get the database password from a colleague but the error I’m getting when calling mssql_connect only refers to the bad password, so I think the extension itself is fine.
    However it would be really good to have the PDO MSSQL working as well, and I believe that is a separate extension module (pdo_mssql.so). Do you happen to have a precompiled file suitable for PHP 5.5.14 hanging around that you could send me?

  6. Hi everyone!

    If you’re having problems copying ‘mssql.so’ in OSX El Capitan (10.11.x), specifically with this line:
    $ sudo cp modules/mssql.so /usr/lib/php/extensions/no-debug-non-zts-20100525/
    Most likely you’ll get an ‘Operation not permitted’ error, because El Capitan has a ‘Rootless’ safety feature enabled by default.
    You can disable it, copy the driver and enable it again.
    1. Restart your Mac.
    2. Before the Apple logo appears, press Cmd-R to access Recovery Mode.
    3. When in Recovery Mode, click ‘Utilities’ then ‘Terminal’
    4. In Terminal, run ‘csrutil disable’
    5. Restart your Mac
    6. Copy your driver file
    7. Repeat steps 1-3, and run ‘csrutil enable’
    8. Restart one last time. You’ll have ‘Rootless’ protection again.

    Cheers

    1. Getting the following error on El Capitain (PHP 5.6.11)

      configure: error: Directory /usr/local is not a FreeTDS installation directory

      Any help is appreciated!

  7. Hi,
    I followed the tutorial, installed autoconf and freetds. Now I’m not sure about the “Build and install mssql.so” Section. Where it says:

    tar zxf php-5.4.17.tar.gz

    I don’t have that file. What am I missing?

    Thanks for your help. I’m on OS X El Captain

  8. my god, you really save me. just install el capitan yesterday and got stuck on xampp to connect to mssql. btw i use this web as reference but i use bahasa to explain your article, thx a bunch man!

  9. Just pointing out that if you are following these directions for php installed via homebrew, the config command needs to be:

    ./configure –with-php-config=/usr/local/bin/php-config –with-mssql=/usr/local/

Leave a Reply