You can follow microsoft's official instalation steps, however if you have multiple php instalations (I have php7.4 and 7.3 installed on my laravel forge server) you need to specify what version to build the extension with.
# make sure you have this installed:
sudo apt-get install -y unixodbc-dev
# elevate as root
sudo su
pecl -d php_suffix=7.3 install sqlsrv
pecl -d php_suffix=7.3 install pdo_sqlsrv
printf "; priority=20\nextension=sqlsrv.so\n" > /etc/php/7.3/mods-available/sqlsrv.ini
printf "; priority=30\nextension=pdo_sqlsrv.so\n" > /etc/php/7.3/mods-available/pdo_sqlsrv.ini
phpenmod -v 7.3 sqlsrv pdo_sqlsrv
Check with php -m | grep sql
If you see pdo_sqlsrv
and sqlsrv
in that list you have the extension installed in php-cli.
You need a restart to php-fpm or apache and you're done.
service php7.3-fpm restart
If you installed it wrong and want a fresh start run:
pecl uninstall -r sqlsrv
pecl uninstall -r pdo_sqlsrv
# or manual remove
rm -rf /etc/php/7.3/mods-available/sqlsrv.ini /etc/php/7.3/mods-available/pdo_sqlsrv.ini
If you see error SQLSTATE[IMSSP]: This extension requires the Microsoft ODBC Driver for SQL Server ...
follow this instalation tutorial to install ODBC Driver:
https://learn.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-2017#ubuntu18