Skip to content

ODBC on Ubuntu

phaledang edited this page Jan 22, 2025 · 9 revisions

References:

Install the Microsoft ODBC driver for SQL Server (Linux) - ODBC Driver for SQL Server | Microsoft Learn https://learn.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-ver16&tabs=ubuntu18-install%2Calpine17-install%2Cdebian8-install%2Credhat7-13-install%2Crhel7-offline

cmd

if ! [[ "18.04 20.04 22.04 24.04" == *"$(grep VERSION_ID /etc/os-release | cut -d '"' -f 2)"* ]];
then
    echo "Ubuntu $(grep VERSION_ID /etc/os-release | cut -d '"' -f 2) is not currently supported.";
    exit;
fi

Download the package to configure the Microsoft repo

curl -sSL -O https://packages.microsoft.com/config/ubuntu/$(grep VERSION_ID /etc/os-release | cut -d '"' -f 2)/packages-microsoft-prod.deb

Install the package

sudo dpkg -i packages-microsoft-prod.deb

Delete the file

rm packages-microsoft-prod.deb

Install the driver

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

Check deployment

odbcinst -q -d

Update odbc.ini

image

sudo nano /etc/odbc.ini

Add Configuration to database, sample config

[MyAzureSQLDB]
Driver = ODBC Driver 17 for SQL Server
Server = your-server-name.database.windows.net
Database = your-database-name
Port = 1433
User = your-username
Password = your-password
Encrypt = yes
TrustServerCertificate = no

If using Docker, the DockerFile should have odbc installed.

See dockerfile for details

# install odbc
# Install system dependencies
RUN apt-get update && apt-get install -y \
    curl \
    apt-transport-https \
    unixodbc \
    unixodbc-dev \
    odbcinst \
    build-essential \
    gcc \
    && rm -rf /var/lib/apt/lists/*

# Add the Microsoft repository for the ODBC driver
RUN curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add - && \
    curl https://packages.microsoft.com/config/debian/10/prod.list > /etc/apt/sources.list.d/mssql-release.list && \
    apt-get update && ACCEPT_EULA=Y apt-get install -y msodbcsql18

Clone this wiki locally