How to deal with fatal error: sql.h: No such file or directory when installing pecl sqlsrv

Background

Whilst following Microsoft’s official instructions on how to install SQL Server for PHP, you might encounter the following problem:

/tmp/pear/temp/sqlsrv/shared/xplat.h:30:10: fatal error: sql.h: No such file or directory
#include <sql.h>
^~~~~~~
compilation terminated.
Makefile:194: recipe for target 'conn.lo' failed
make: *** [conn.lo] Error 1
ERROR: `make' failed

Solutions for Mac M2

This solution might also work on other Macs.

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

Reference: https://learn.microsoft.com/en-us/answers/questions/698379/get-sql-h-file-not-found-when-install-sqlsrv-drive

Solution for Debian/Ubuntu

The Microsoft ODBC driver’s installation page mentions that the development libraries are optional – they are not. So run the command below to get it working:

sudo apt-get install unixodbc-dev

Reference

https://docs.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-ver15

Share this article

1 thought on “How to deal with fatal error: sql.h: No such file or directory when installing pecl sqlsrv”

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to Top