Installing pyodbc and unixodbc for Apple Silicon

Ed King
2 min readDec 13, 2021
Photo by Elisa Ventur on Unsplash

Just bought a shiny new Macbook M1 Pro for work? Expected to joyfully rebuild your Python virtual environments without breaking a sweat? Wasted a Sunday afternoon trying to understand why pyodbc and unixodbc don’t work whilst your old Mac taunts you? You’re not alone.

The M1 series of Macs are the first to use Apple’s new proprietary ARM-based silicon, replacing the Intel x86-based silicon that they’ve previously been using. Fundamentally, the architecture of these two chipsets is completely different, meaning that drivers built for one won’t work for the other.

Given how new Apple Silicon is, it’s been a game of catch-up for developers to rebuild, test and distribute the squillions of drivers and libraries we all know and love… hopefully before anyone notices. Alas, pyodbc and unixodbc haven’t quite made it and you certainly will notice. Luckily, it’s quite straightforward to get them working for the M1 once you know how, even if it did ruin my weekend.

The Prep

I will assume that you’ve already installed Homebrew and prepared a shiny new Python virtual environment to configure.

It’s worth noting here that I’ve tested the following solution using a virtual environment created using Python v3.8.6 installed from the Python website:

mkvirtualenv whodeenie -p /Library/Frameworks/Python.framework/Versions/3.8/bin/python3

It’s also worth noting that Homebrew on an M1 Mac installs stuff to a different directory than you’d be used to on an Intel Mac, which is important to know when we rebuild pyodbc:

  • M1 Mac Homebrew directory: /opt/homebrew
  • Intel Mac Homebrew directory: /usr/local

The Magic

  • Download the pyodbc-4.0.32.tar.gz source package from PyPI (or whatever the latest version is).
  • Install the Homebrew version of unixodbc. Note that this package already has an M1 Mac build available through Homebrew, which will be automatically used when you’re installing from your M1 machine:
    brew install unixodbc
  • Build and reinstall pyodbc from source for your M1 platform, linking the ODBC driver libraries from the Homebrew-installed unixodbc in the process. Note that version numbers might be different for you so check the paths, and be sure to run these commands in your virtual environment:
pip uninstall pyodbc
export CPPFLAGS="-I/opt/homebrew/Cellar/unixodbc/2.3.9_1/include"
export LDFLAGS="-L/opt/homebrew/Cellar/unixodbc/2.3.9_1/lib -liodbc -liodbcinst"
cd path/to/pyodbc-4.0.32.tar.gz
pip install pyodbc-4.0.32.tar.gz
  • Put down the hammer, you’re done.

--

--