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
- Xcode with command line tools (these are installed when you first run Xcode)
- 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 macOS (5.6.30 on macOS Sierra 10.12.6; 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-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)