How to configure pymssql with SSL support on Ubuntu 16.04 LTS?
<p>What are the steps required to configure pymssql with SSL support on Ubuntu 16.04 LTS so I can connect to a SQL Server instance that requires an encrypted connection (e.g., Azure)?</p>

<p>The following worked for me on a clean install of Xubuntu 16.04 LTS x64:</p>

<p>The first challenge is that the FreeTDS we get from the Ubuntu repositories does not support SSL "out of the box", so we need to build our own. Start by installing python-pip (which also installs build-essentials, g++, and a bunch of other stuff we'll need) and libssl-dev (the OpenSSL libraries required for building FreeTDS with SSL support)</p>



<pre class="lang-none prettyprint-override"><code>sudo apt-get install python-pip libssl-dev
</code></pre>

<p>Download the source code for FreeTDS 0.95 (<strong><em>not</em></strong> 1.x, since the current 2.1.3 release of pymssql will not build against FreeTDS 1.x) from</p>

<p><a href="ftp://ftp.freetds.org/pub/freetds/stable/freetds-0.95.tar.gz" rel="nofollow">ftp://ftp.freetds.org/pub/freetds/stable/freetds-0.95.tar.gz</a></p>

<p>and unpack it. Switch to the freetds-0.95 directory and then do</p>

<pre class="lang-none prettyprint-override"><code>./configure --with-openssl=/usr/include/openssl --with-tdsver=7.3
make
sudo make install
</code></pre>

<p>Check the build with</p>

<pre class="lang-none prettyprint-override"><code>tsql -C
</code></pre>

<p>and ensure that "TDS version: 7.3" and "OpenSSL: yes" are listed. Then use tsql to test a "raw" FreeTDS connection, e.g.,</p>

<pre class="lang-none prettyprint-override"><code>tsql -H example.com -p 1433 -U youruserid -P yourpassword
</code></pre>

<p>Now to install pymssql. By default, recent versions ship as a pre-compiled "wheel" file that <em>does not</em> support encrypted connections (at least it didn't for me) so we need to install from the pymssql source using</p>

<pre class="lang-none prettyprint-override"><code>sudo -H pip install --no-binary pymssql pymssql
</code></pre>

<p>When the build is complete, pymssql is installed.</p>

<p>But... it won't work (yet). When we try to do <code>import pymssql</code> in Python we get</p>

<blockquote>
  <p>ImportError: libsybdb.so.5: cannot open shared object file: No such file or directory</p>
</blockquote>

<p>because apparently that file is in the "wrong" place. The fix (ref: <a href="https://github.com/pymssql/pymssql/issues/8" rel="nofollow">here</a>) is to create a symlink in the "right" place that points to the actual file</p>

<pre class="lang-none prettyprint-override"><code>sudo ln -s /usr/local/lib/libsybdb.so.5 /usr/lib/libsybdb.so.5
sudo ldconfig
</code></pre>

<p>Now pymssql works with SSL connections.</p>

<p>For me, anyway.</p>

python ubuntu-16.04 pymssql