This is an old question, so here's an updated (year 2023) answer.
The error
ImportError: Error loading shared library libodbc.so.2: No such file or directory
requires one to install the Microsoft ODBC driver for SQL Server. At first, I tried the solution provided above, namely
# This solution is incomplete!
sudo apt install unixodbc-dev
however this solution is incomplete, as it will NOT install all the required drivers. For instance, the Python script
import pyodbc
print(pyodbc.drivers())
will still return the empty list []
.
The solution instead is to follow the instructions given by Microsoft itself. For Ubuntu:
if ! [[ "18.04 20.04 22.04 23.04" == *"$(lsb_release -rs)"* ]];
then
echo "Ubuntu $(lsb_release -rs) is not currently supported.";
exit;
fi
curl https://packages.microsoft.com/keys/microsoft.asc | sudo tee /etc/apt/trusted.gpg.d/microsoft.asc
curl https://packages.microsoft.com/config/ubuntu/$(lsb_release -rs)/prod.list | sudo tee /etc/apt/sources.list.d/mssql-release.list
sudo apt-get update
sudo ACCEPT_EULA=Y apt-get install -y msodbcsql18
# optional: for bcp and sqlcmd
sudo ACCEPT_EULA=Y apt-get install -y mssql-tools18
echo 'export PATH="$PATH:/opt/mssql-tools18/bin"' >> ~/.bashrc
source ~/.bashrc
# optional: for unixODBC development headers
sudo apt-get install -y unixodbc-dev
This should solve all the problems, and the aforementioned Python script should now output ['ODBC Driver 18 for SQL Server']
.
WARNING:
If you try the incomplete solution mentioned above, and then follow the instructions provided by Microsoft (like I did at first) you'll run into nasty problems related to cyclic and incomplete installations and related dependencies. If you accidentally executed the incomplete solution first, it is best to sudo apt purge unixodbc-dev
before following the steps recommended by Microsoft.
Finally, note that Microsoft now fully supports ODBC, so the instructions above should be considered the way to install ODBC, and therefore the correct answer to the original question by OP.
sudo apt install unixodbc-dev
to get the missing file(s). – Depositoryldd pyodbc.cpython-35m-i386-linux-gnu.so
, one of the lines islibodbc.so.2 => /usr/lib/i386-linux-gnu/libodbc.so.2 (0xb7670000)
, which is the equivalent location on a 32-bit install of Xubuntu. However,libodbc.so.2
is a symlink to the actual filelibodbc.so.2.0.0
in the same directory. Have you verified that yourlibodbc.so.2
symlink is valid? – Depository