I'm trying to install PyODBC on Heroku, but I get fatal error: sql.h: No such file or directory
in the logs when pip runs. How do I fix this error?

- 121,510
- 29
- 395
- 339

- 1,031
- 1
- 8
- 7
-
1Did you ever get this to work on Heroku? – cph Apr 24 '16 at 08:05
-
1@cph This project currently works on Heroku with such a setup https://github.com/bmwant/pr-review-notifier – Most Wanted Mar 09 '18 at 19:43
7 Answers
To follow up on the answer below...
Example for Ubuntu:
sudo apt-get install unixodbc unixodbc-dev
Example for CentOS:
sudo yum install unixODBC-devel
Example for Fedora:
sudo dnf install unixODBC-devel
On Windows:
conn = pyodbc.connect('DRIVER={SQL Server};SERVER=yourserver.yourcompany.com;DATABASE=yourdb;UID=user;PWD=password')
On Linux:
conn = pyodbc.connect('DRIVER={FreeTDS};SERVER=yourserver.yourcompany.com;PORT=1433;DATABASE=yourdb;UID=user;PWD=password;TDS_VERSION=7.2')
-
Hi Flipper!thanks for the commands. I'm using Git Bash for windows. Can you please suggest how do i do this. One more thing is i'm able to install pyodbc package locally in my system. But when heroku server is trying to install the same package on server side, i'm receiving this error. – shashi kanth Jul 12 '15 at 03:47
-
No problem! So, you're using Windows for development and Linux in production on Heroku? This can get tricky (the holy grail is to have your development and production environments be as identical as possible). First I would suggest definitely using a virtualenv, then using pyodbc with the SQL Server driver on Windows / dev, and FreeTDS/unixODBC on Linux / prod Heroku. I'll amend my example above. – FlipperPA Jul 12 '15 at 16:34
-
-
Let me know if it works. I would also highly recommend trying to get your development and production environments at least on the same operating system. Good luck. – FlipperPA Jul 13 '15 at 20:17
-
1Did anyone get this to work on Heroku? None of the buildpacks seem to be working now that supported django-pyodbc – cph Apr 24 '16 at 08:06
-
Can you suggest, how do I solve this error when I use MongoDB as a database? The Python code connects to the Mongodb database in the cloud and I get the same error while trying to push into Heroku.Thanks – mari Feb 01 '18 at 12:50
-
If I'm deploying to Heroku would I include the sudo apt-get install unixodbc unixodbc-dev in the Aptfile? – user2200270 Jul 13 '20 at 06:53
-
1
You can add Heroku build pack to preinstall required apt packages first
heroku buildpacks:add --index 1 https://github.com/heroku/heroku-buildpack-apt
Add Aptfile
in the your directory root and to the repository as well
unixodbc
unixodbc-dev
python-pyodbc
libsqliteodbc
It will install everything you need to work with pyodbc
or aioodbc
packages from python on Heroku

- 6,254
- 5
- 53
- 70
-
3Worked like charm! Tried a lot of other buildpacks for odbc but this one actually worked. Thanks for putting it up. – Afsan Abdulali Gujarati Jun 21 '18 at 17:39
-
is Aptfile a file just like the ProcFile that we create ? (am new to this ) – MoonLight Sep 20 '21 at 10:32
-
1and how do I do this "Add Aptfile in the your directory root and to the repository as well" – MoonLight Sep 20 '21 at 10:42
You need the unixODBC devel package. I don't know what distro you are using but you can google it and build from source.

- 1,093
- 11
- 26
You don't have the required ODBC header files on your machine. You need to run below command to get g++ installed
yum install unixODBC-devel

- 4,486
- 12
- 52
- 87
The other answers are more or less correct; you're missing the unixodbc-dev[el]
package for your operating system; that's what pip
needs in order to build pyodbc
from source.
However, a much easier option is to install pyodbc
via the system package manager. On Debian/Ubuntu, for example, that would be apt-get install python-pyodbc
. Since pyodbc
has a lot of compiled components and interfaces heavily with the UnixODBC OS-level packages, it is probably a better fit for a system package rather than a Python/pip-installed one.
You can still list it as a dependency in your requirements.txt
files if you're making code for distribution, but it'll usually be easier to install it via the system PM.

- 3,796
- 3
- 35
- 52
-
1The only problem with installing pyodbc from the OS repositories is that they tend to be *really old* versions of pyodbc. IIRC the Ubuntu repositories install v3.0.7 while the current stable version as I write this is v4.0.21, and 4.x is much better than 3.x at handling Unicode. – Gord Thompson Nov 29 '17 at 21:23
I recently saw this error in Heroku. To fix this problem I took the following steps:
Add
Apt File
to the root folder, with the following: unixodbc unixodbc-dev python-pyodbc libsqliteodbcCommit that
Run
heroku buildpacks:clear
Run
heroku buildpacks:add --index 1 heroku-community/apt
Push to Heroku
For me the problem was that I previously installed the buildpack for python, which was not needed. By running heroku buildpacks:clear
I removed all un-needed buildpacka, then add back the one I needed. So if you do follow these steps be sure to make note of the build packs you need. To view the buildpacks you have run heroku buildpacks
before following these steps.

- 11
- 1
RedHat/CentOS:
dnf install -y unixODBC-devel
along with unixODBC installation

- 143
- 1
- 6