Fixing fatal error: sql.h: No such file or directory when installing PyODBC on Heroku

Fatal error: sql.h: No such file or directory when installing PyODBC on Heroku is an error which occurs when you use pyodbc while the required apt packages are missing.

In today’s blog post I am going to present an annoying and confusing python error and explain why this error is taking place and how to fix it, with a set of possible fixes.

Exploring fatal error: sql.h: No such file or directory when installing PyODBC on Heroku

This is an error which occurs when you use pyodbc while the required apt packages are missing.

You should avoid mixing between different errors. The error message should look like the error message bellow.

                                                                       #
fatal error: sql.h: No such file or directory
                                                                       #

Bellow we will take care of the error using multiple possible solutions according to your needs.

Solution 1 : Add Aptfile and add the Heroku build pack

The first solution to this issue is to add Heroku build pack which is going to preinstall the required apt packages.

You can do this by using this command

                                                                       #
heroku buildpacks:add --index 1 https://github.com/heroku/
heroku-buildpack-apt
                                                                       #

The second step is to Add Aptfile to the repository and the directory root. The Aptfile is a file to keep a list of packages you want apt to install for you when running a particular buildpack on Heroku.

Bellow is the content of the Aptfile.

                                                                       #
# do not forget even one of these packages
libsqliteodbc
unixodbc-dev
unixodbc
python-pyodbc
                                                                       #

Now, you can carry on with your project, everything should be working fine.

If that is not the case, please try the method bellow.

Solution 2 : install the unixODBC devel package

Another popular solution is to install the unixODBC devel package.

You can try this command to install it

                                                                       #
yum install unixODBC-devel
                                                                       #

You can try this for Ubuntu

                                                                       #
sudo apt-get install unixodbc unixodbc-dev
                                                                       #

Or this command for CentOS

                                                                       #
sudo yum install unixODBC-devel
                                                                       #

It is easy to figure out which command to use on other platforms.

I hope the fixes above fixed your problem, good luck with your next project.

Summing-up : 

This is the end of this article guys, I hope one of these solutions worked for you depending on which OS you have, I wish you good luck with your Python Journey. For donations you can use the red Kofi button above.

Thank you for reading, keep coding and cheers. If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/