Skip to content

Instantly share code, notes, and snippets.

@nasrulhazim
Last active November 8, 2023 17:18
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save nasrulhazim/404f8e443a447f84186bd382a9bb47f4 to your computer and use it in GitHub Desktop.
Save nasrulhazim/404f8e443a447f84186bd382a9bb47f4 to your computer and use it in GitHub Desktop.
Install PHP MSSQL Driver in MacOS (Mojave)

Installing Brew

$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Installing MSSQL Tools

brew tap microsoft/mssql-release https://github.com/Microsoft/homebrew-mssql-release
brew update
brew install msodbcsql17 mssql-tools

If you installed this formula with the registration option (default), you'll need to manually remove [ODBC Driver 17 for SQL Server] section from odbcinst.ini after the formula is uninstalled. This can be done by executing the following command:

$ odbcinst -u -d -n "ODBC Driver 17 for SQL Server"

Install dependencies:

$ brew install autoconf automake libtool

Install PHP Drivers for MSSQL

$ sudo pecl install sqlsrv
$ sudo pecl install pdo_sqlsrv

In case your MSSQL .so files didn't load properly, you can find the exact location of the files with following command:

$ find . -name pdo_sqlsrv.so -print
$ find . -name sqlsrv.so -print

In my case I got the following path for the files. Now

/usr/local/lib/php/pecl/20170718/sqlsrv.so
/usr/local/lib/php/pecl/20170718/pdo_sqlsrv.so

Next, just copy the files to your respective PHP Version lib directory:

cp /usr/local/lib/php/pecl/20170718/sqlsrv.so /usr/local/Cellar/php@7.2/7.2.13/lib/php/20170718/
cp /usr/local/lib/php/pecl/20170718/pdo_sqlsrv.so /usr/local/Cellar/php@7.2/7.2.13/lib/php/20170718/

Then make sure the following extensions is loaded in php.ini

extension="pdo_sqlsrv.so"
extension="sqlsrv.so"

Use php -m | grep sqlsrv to check the extensions is loaded properly.

php -m | grep sqlsrv
pdo_sqlsrv
sqlsrv
@avivamientodev
Copy link

Y have two PHP versions (7.4 & 8) This works for PHP 8 but not for the other version. Any idea? Thanks !!

@Groganj89
Copy link

get a Make fail error on both the Pecl install commands. Any thoughts?

     ^~~~~~~

1 error generated.
make: *** [pdo_dbh.lo] Error 1
ERROR: `make' failed

@joseph-montanez
Copy link

I found that I also needed to do this:

sudo CXXFLAGS="-I/opt/homebrew/opt/unixodbc/include/" LDFLAGS="-L/opt/homebrew/lib/" pecl install sqlsrv
sudo CXXFLAGS="-I/opt/homebrew/opt/unixodbc/include/" LDFLAGS="-L/opt/homebrew/lib/" pecl install pdo_sqlsrv

As sql.h could not be found.

@AdetolaAremu
Copy link

I am also battling with the same issue

get a Make fail error on both the Pecl install commands. Any thoughts?

     ^~~~~~~

1 error generated. make: *** [pdo_dbh.lo] Error 1 ERROR: `make' failed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment