Upon connecting to sqlserver from Databricks getting error: Can't open lib 'ODBC Driver 17 for SQL Server'

Immaneni, Nanda 20 Reputation points
2025-04-14T13:39:56.4666667+00:00

In our Databricks notebook:

We have done:

%python

Install the ODBC Driver 17 for SQL Server

%pip install pyodbc

%pip install --upgrade 'databricks-connect>=7.3'

Then trying to connect to SQL Server using:

conn = pyodbc.connect(
        f"DRIVER={{ODBC Driver 17 for SQL Server}};SERVER={myHostname};DATABASE={SQL_target_psa_copy_db};UID={myUsername};PWD={myPassword}"
    )

This is throwing Error:
Error: ('01000', "[01000] [unixODBC][Driver Manager]Can't open lib 'ODBC Driver 17 for SQL Server' : file not found (0) (SQLDriverConnect)")
Azure Databricks
Azure Databricks
An Apache Spark-based analytics platform optimized for Azure.
2,413 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Dileep Raj Narayan Thumula 5 Reputation points Microsoft External Staff
    2025-04-15T17:28:58.2466667+00:00

    [unixODBC][Driver Manager]Can't open lib 'ODBC Driver 17 for SQLServer' : file not found

    Happens because the ODBC driver for SQL Server is not installed by default on Databricks clusters

    To install the ODBC driver on a Databricks All-Purpose Cluster, you can run the following script directly in a notebook cell using the %sh magic command:

    %sh
    
    if ! [[ "16.04 18.04 20.04 22.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 msodbcsql17
    
    sudo ACCEPT_EULA=Y apt-get install -y mssql-tools
    
    echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
    
    source ~/.bashrc
    
    sudo apt-get install -y unixodbc-dev
    

    Results:

    enter image description here

    Once the script has been executed, you can confirm that the ODBC driver was installed successfully by running the following Python code in your notebook:

    %sh
    
    odbcinst -q -d -n "ODBC Driver 17 for SQL Server"
    
    sqlcmd -?
    
    dpkg -l | grep msodbcsql17
    

    Results:

    
    [ODBC Driver 17 for SQL Server] Description=Microsoft ODBC Driver 17 for SQL Server Driver=/opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.10.so.6.1 UsageCount=1 bash: line 5: sqlcmd: command not found ii msodbcsql17 17.10.6.1-1 amd64 ODBC Driver for Microsoft(R) SQL Server(R)
    
    

    Running the above Python code will display all ODBC drivers currently installed on the Databricks cluster.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.