4

After installing the sql server, I tried to install the php drivers, sqlsrv and pdo_sqlsrv php extensions, following these commands:

sudo pecl install sqlsrv
sudo pecl install pdo_sqlsrv
sudo su
printf "; priority=20\nextension=sqlsrv.so\n" > /etc/php/7.4/mods-available/sqlsrv.ini
printf "; priority=30\nextension=pdo_sqlsrv.so\n" > /etc/php/7.4/mods-available/pdo_sqlsrv.ini
exit
sudo phpenmod -v 7.4 sqlsrv pdo_sqlsrv

But I am getting this PHP Warning:

PHP Warning:  PHP Startup: Unable to load dynamic library 'sqlsrv.so' (tried: /usr/lib/php/20190902/sqlsrv.so (/usr/lib/php/20190902/sqlsrv.so: cannot open shared object file: No such file or directory), /usr/lib/php/20190902/sqlsrv.so.so (/usr/lib/php/20190902/sqlsrv.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
PHP Warning:  PHP Startup: Unable to load dynamic library 'pdo_sqlsrv.so' (tried: /usr/lib/php/20190902/pdo_sqlsrv.so (/usr/lib/php/20190902/pdo_sqlsrv.so: cannot open shared object file: No such file or directory), /usr/lib/php/20190902/pdo_sqlsrv.so.so (/usr/lib/php/20190902/pdo_sqlsrv.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0

And then when I try to migrate my database using the sql server throws an error saying that can`t find sql driver.

 Illuminate\Database\QueryException  : could not find driver (SQL: select * from sysobjects where type = 'U' and name = migrations)

Does anyone have any idea how to solve? I use Ubuntu 18.04 and PHP version 7.4.10

1

4 Answers 4

10

Finally got it to work. You need to set the cli version to 7.4

sudo apt install php7.4-dev
sudo update-alternatives --set php /usr/bin/php7.4
sudo update-alternatives --set php-config /usr/bin/php-config7.4
sudo update-alternatives --set phpize /usr/bin/phpize7.4
sudo pecl install -f sqlsrv
sudo pecl install -f pdo_sqlsrv
sudo phpenmod -v 7.4 sqlsrv pdo_sqlsrv
sudo service apache2 restart 
4
  • Russell, this saved me so much time after I've been struggling for days. thanks a lot
    – ossentoo
    Oct 5, 2021 at 8:59
  • It worked for me on ubuntu 20.04 with php 7.4 Dec 8, 2021 at 14:07
  • Simplest solution! Thanks! Sep 1, 2022 at 0:45
  • Thanks, this helped fix the same issue for me with php 8.0 and 8.2 being on the same box! Jul 12, 2023 at 10:54
2

In case someone is wondering still relevant for Php 8.2

sudo apt install php8.2-dev
sudo update-alternatives --set php /usr/bin/php8.2
sudo update-alternatives --set php-config /usr/bin/php-config8.2
sudo update-alternatives --set phpize /usr/bin/phpize8.2
sudo pecl install -f sqlsrv
sudo pecl install -f pdo_sqlsrv
sudo phpenmod -v 8.2 sqlsrv pdo_sqlsrv
sudo service apache2 restart 

Works like a charm.

0

This perfectly worked in ubuntu 18.04

sudo apt install php7.4-dev
sudo update-alternatives --set php /usr/bin/php7.4
sudo update-alternatives --set php-config /usr/bin/php-config7.4
sudo update-alternatives --set phpize /usr/bin/phpize7.4
sudo pecl install -f sqlsrv
sudo pecl install -f pdo_sqlsrv
sudo bash -c "echo extension=sqlsrv.so > /etc/php/7.4/mods-available/sqlsrv.ini"
sudo ln -s /etc/php/7.4/mods-available/sqlsrv.ini /etc/php/7.4/apache2/conf.d/sqlsrv.ini
sudo ln -s /etc/php/7.4/mods-available/sqlsrv.ini /etc/php/7.4/cli/conf.d/sqlsrv.ini
sudo bash -c "echo extension=pdo_sqlsrv.so > /etc/php/7.4/mods-available/pdo_sqlsrv.ini"
sudo ln -s /etc/php/7.4/mods-available/pdo_sqlsrv.ini /etc/php/7.4/apache2/conf.d/pdo_sqlsrv.ini
sudo ln -s /etc/php/7.4/mods-available/pdo_sqlsrv.ini /etc/php/7.4/cli/conf.d/pdo_sqlsrv.ini
sudo phpenmod -v 7.4 sqlsrv pdo_sqlsrv
sudo service apache2 restart 
0

I solved the same issue by installing the libodbcinst.so.2 library.

apt-get install unixodbc

Then, added "extension=pdo_sqlsrv.so" and "extension=sqlsrv.so" to the php.ini configuration file.

you can use php -i | grep ini to find the configuration file location.

Restart php-fpm.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .