PHP with FreeTDS on macOS Sierra

Need your macOS-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 macOS-ready mssql.so (compiled for PHP 5.6.30 on macOS Sierra 10.12.6) and skip to the end. (18 Aug 2017)

Previous versions are available below.

Prerequisites

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-patched.tar.gz
$ cd freetds-1.00.54
$ ./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 (18 Aug 2017): 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.12.sdk/usr/include/php/ /usr/include/php. System Integrity Protection will need to be disabled first.

Here goes:

$ tar zxf php-5.6.30.tar.gz
$ cd php-5.6.30/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-20131226/

If you have trouble with the final step, System Integrity Protection is probably enabled. Disable it temporarily.

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

extension=mssql.so

And restart Apache if necessary.

Done!

Previous versions

If you’re running an old version of macOS, you might find one of these binaries helpful:

  • mssql.so compiled for PHP 5.4.17 on OS X Mavericks 10.9 can be downloaded here. (17 Nov 2013)
  • mssql.so compiled for PHP 5.4.24 on OS X Mavericks 10.9.4 can be downloaded here. (6 Jul 2014)
  • mssql.so compiled for PHP 5.5.14 on OS X Yosemite 10.10.1 can be downloaded here. (22 Dec 2014)
  • mssql.so compiled for PHP 5.5.27 on OS X Yosemite 10.10.5 can be downloaded here. (16 Sep 2015)

23 thoughts on “PHP with FreeTDS on macOS Sierra”

  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/

    1. I’m on 10.12.6, with PHP 5.6.30, so I’ve updated the post with that. Should be interoperable with 5.6.24, but YMMV.

  10. Hi, I’m on 10.13.2 with PHP 5.6.32. I follow your post with homebrew installation, but I stuck on “make” step. I always get error:

    fatal error: too many errors emitted, stopping now [-ferror-limit=]
    9 warnings and 20 errors generated.
    make: *** [php_mssql.lo] Error 1

    Could you tell me how to solved this?
    Thanks..

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.