Skip to content

Instantly share code, notes, and snippets.

@jgaydos
Last active November 21, 2022 21:02
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save jgaydos/0aecbb66a0689c182daaddd8730ff3a8 to your computer and use it in GitHub Desktop.
Save jgaydos/0aecbb66a0689c182daaddd8730ff3a8 to your computer and use it in GitHub Desktop.
Ubuntu 18.04 + PHP7.3 + SQLSRV
#!/bin/bash
#ubuntu 18.04
#php7.3
# install php ppa
apt -y install software-properties-common
add-apt-repository ppa:ondrej/php -y
apt update
# install php w/o apache
apt -y install php7.3-cli php7.3-mbstring php-pear php7.3-dev php7.3-curl php7.3-gd php7.3-zip php7.3-xml
# install sqlcmd
curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
curl https://packages.microsoft.com/config/ubuntu/18.04/prod.list | tee /etc/apt/sources.list.d/mssql-tools.list
apt update
ACCEPT_EULA=Y apt -y install msodbcsql17
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bash_profile
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
source ~/.bashrc
apt -y install unixodbc-dev
# install sqlsrv driver
# if this fails install sqlsrv-5.5.0preview & pdo_sqlsrv-5.5.0preview
pecl install sqlsrv 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
@FatihGoktas
Copy link

after that line "pecl install sqlsrv pdo_sqlsrv" should continue like below lines;

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
sudo phpenmod -v 7.3 sqlsrv pdo_sqlsrv

@jgaydos
Copy link
Author

jgaydos commented Nov 8, 2019

I had not realized there was a better (more correct) way. I see it is listed in Microsoft's docs. Thanks @FatihGoktas!

@torressam333
Copy link

I ran all of these commands for php7.3 but when I do php -v I get the following errors:

PHP Warning:  PHP Startup: Unable to load dynamic library 'sqlsrv.so' (tried: /usr/lib/php/20180731/sqlsrv.so (/usr/lib/php/20180731/sqlsrv.so: cannot open shared object file: No such file or directory), /usr/lib/php/20180731/sqlsrv.so.so (/usr/lib/php/20180731/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/20180731/pdo_sqlsrv.so (/usr/lib/php/20180731/pdo_sqlsrv.so: cannot open shared object file: No such file or directory), /usr/lib/php/20180731/pdo_sqlsrv.so.so (/usr/lib/php/20180731/pdo_sqlsrv.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0

It seems as though the sqlsrv.so files are for php 7.4 because they were created in my 20190902 DIR.

Should I run these commands for the php7.2?

@jgaydos
Copy link
Author

jgaydos commented Jan 14, 2020

@torressam333 Do you by any chance have PHP7.4 installed on the same box?

@torressam333
Copy link

torressam333 commented Jan 14, 2020

@jgaydos

I don't think I do.... I ran this command

✘ sudo apt-get remove -y –purge php7.4*
zsh: no matches found: php7.4*

I did have 7.4 installed but removed it although in my /etc/php there is still a 7.4 folder despite the command above noting no matches found for php7.4.

Just to confirm I am on Ubuntu 18.04

Thanks.

@jgaydos
Copy link
Author

jgaydos commented Jan 14, 2020

@torressam333 PECL is a pain when you have multiple versions of PHP, and only like to bind to one at a time. You might just need to run sudo update-alternatives --set php /usr/bin/php7.3 to make PHP7.3 your default. Then uninstall and reinstall the extensions sqlsrv and pdo_sqlsrv. If that doesn't work and you don't need 7.4 installed, I would probably just remove it.

@jgaydos
Copy link
Author

jgaydos commented Jan 14, 2020

@torressam333 No need to worry about the /etc/php/7.4 director(it's just configs). If you run pear config-show | grep ext_dir that should tell you the currently set extension directory.

@torressam333
Copy link

Ruan the config-show:

PHP extension directory ext_dir /usr/lib/php/20180731

@jgaydos
Copy link
Author

jgaydos commented Jan 14, 2020

@torressam333 That looks correct. If the extensions you installed earlier are still installed you will need to uninstall them ( pecl uninstall sqlsrv pdo_sqlsrv). I would then rerun the pecl install sqlsrv pdo_sqlsrv and fingers crossed it works.

@torressam333
Copy link

Setting and updating the alternatives then uninstall sqlsrv/pdo_sqlsrv then reinstalling seemed to work. Thank you.

@jgaydos
Copy link
Author

jgaydos commented Jan 14, 2020

@torressam333 Glad I could help. PHP drivers can be a royal a pain.

@torressam333
Copy link

@jgaydos is there an easy way to also install sqlsrv on php5.6?

@jgaydos
Copy link
Author

jgaydos commented Jan 14, 2020

@torressam333 Ehh it has been quite some time since I have touched any PHP5.6 code. From what I remember this using FreeTDS looks familiar.

@torressam333
Copy link

Super helpful, thank you.

@pythcon
Copy link

pythcon commented Apr 21, 2020

Thank you for that @jgaydos. You saved me after 4 hours of searching for the fix to pecl.

@ale-softplace
Copy link

Hola! me pueden indicar como o donde buscar info para isntalar varias instancias de sqlserver en un mismo servidor por favor?
graciaS!!

@jgaydos
Copy link
Author

jgaydos commented Jul 14, 2020

@ale-softplace I had to Google Translate, so forgive me if I did not understand your question properly. If you are trying to install multiple instances of MS SQL Server then that is only available on Windows, and not Linux (Source).

@ale-softplace
Copy link

thanks!!

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