Python – How do I fix pyodbc installation error on Ubuntu 20.04?

How do I fix pyodbc installation error on Ubuntu 20.04?… here is a solution to the problem.

How do I fix pyodbc installation error on Ubuntu 20.04?

I’m trying to install pyodbc on Ubunut 20.04 (LTS) and get the following error.

$pip3 install --user pyodbc
Collecting pyodbc
  Using cached pyodbc-4.0.32.tar.gz (280 kB)
  Preparing metadata (setup.py) ... done
Building wheels for collected packages: pyodbc
  Building wheel for pyodbc (setup.py) ... error
  ERROR: Command errored out with exit status 1:
   command: /home/linuxbrew/.linuxbrew/opt/[email protected]/bin/python3.9 -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-gn6zk8ei/pyodbc_4351fd17af0242d6b5939cae9874f889/setup.py'"'"'; __file__='"'"'/tmp/pip-install-gn6zk8ei/pyodbc_4351fd17af0242d6b5939cae9874f889/setup.py'"'"'; f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io. StringIO('"'"'from setuptools import setup; setup()'"'"'); code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"'); f.close(); exec(compile(code, __file__, '"'"'exec'"'"'))' bdist_wheel -d /tmp/pip-wheel-ceo1au72
       cwd: /tmp/pip-install-gn6zk8ei/pyodbc_4351fd17af0242d6b5939cae9874f889/
  Complete output (9 lines):
  running bdist_wheel
  running build
  running build_ext
  building 'pyodbc' extension
  creating build
  creating build/temp.linux-x86_64-3.9
  creating build/temp.linux-x86_64-3.9/src
  gcc-5 -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -O3 -Wall -fPIC -DPYODBC_VERSION=4.0.32 -I/home/linuxbrew/.linuxbrew/include -I/home/linuxbrew/.linuxbrew/opt/ [email protected]/include -I/home/linuxbrew/.linuxbrew/opt/sqlite/include -I/home/linuxbrew/.linuxbrew/opt/[email protected]/include/python3.9 -c src/buffer.cpp -o build/temp.linux-x86_ 64-3.9/src/buffer.o -Wno-write-strings
  error: command 'gcc-5' failed: No such file or directory
  ----------------------------------------
  ERROR: Failed building wheel for pyodbc
  Running setup.py clean for pyodbc
Failed to build pyodbc
Installing collected packages: pyodbc
    Running setup.py install for pyodbc ... error
    ERROR: Command errored out with exit status 1:
     command: /home/linuxbrew/.linuxbrew/opt/[email protected]/bin/python3.9 -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-gn6zk8ei/pyodbc_4351fd17af0242d6b5939cae9874f889/setup.py'"'"'; __file__='"'"'/tmp/pip-install-gn6zk8ei/pyodbc_4351fd17af0242d6b5939cae9874f889/setup.py'"'"'; f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io. StringIO('"'"'from setuptools import setup; setup()'"'"'); code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"'); f.close(); exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-m8e62n8n/install-record.txt --single-version-externally-managed --user --prefix= --compile -- install-headers /home/user/.local/include/python3.9/pyodbc
         cwd: /tmp/pip-install-gn6zk8ei/pyodbc_4351fd17af0242d6b5939cae9874f889/
    Complete output (9 lines):
    running install
    running build
    running build_ext
    building 'pyodbc' extension
    creating build
    creating build/temp.linux-x86_64-3.9
    creating build/temp.linux-x86_64-3.9/src
    gcc-5 -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -O3 -Wall -fPIC -DPYODBC_VERSION=4.0.32 -I/home/linuxbrew/.linuxbrew/include -I/home/linuxbrew/.linuxbrew/opt/ [email protected]/include -I/home/linuxbrew/.linuxbrew/opt/sqlite/include -I/home/linuxbrew/.linuxbrew/opt/[email protected]/include/python3.9 -c src/buffer.cpp -o build/temp.linux-x86_ 64-3.9/src/buffer.o -Wno-write-strings
    error: command 'gcc-5' failed: No such file or directory
    ----------------------------------------
ERROR: Command errored out with exit status 1: /home/linuxbrew/.linuxbrew/opt/[email protected]/bin/python3.9 -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-gn6zk8ei/pyodbc_4351fd17af0242d6b5939cae9874f889/setup.py'"'"'; __file__='"'"'/tmp/pip-install-gn6zk8ei/pyodbc_4351fd17af0242d6b5939cae9874f889/setup.py'"'"'; f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io. StringIO('"'"'from setuptools import setup; setup()'"'"'); code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"'); f.close(); exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-m8e62n8n/install-record.txt --single-version-externally-managed --user --prefix= --compile -- install-headers /home/user/.local/include/python3.9/pyodbc Check the logs for full command output.

Installing GCC-5 via apt does not work because it is not available for installation. I currently have the gcc-9 compiler installed.

I tried the installation
python3-dev python3-pip such as Error installing pandas_profiling with Pip on Ubuntu 20.04 and https://github.com/mkleehammer/pyodbc/wiki/Install#installing-on-linux, but that didn’t solve the problem.

Any help is greatly appreciated.

Solution

From the docs

sudo apt install python3-pip python3-dev unixodbc-dev
pip3 install --user pyodbc

This is for 18.04, but I suspect it works for you as well.

Related Problems and Solutions