[Solved] fatal error: 'portaudio.h' file not found

July 1, 2022 2 min read

I was installing a python module called pyaudio, and to my surprise, I got an error that says:

pip install pyaudio
Failed building wheel for pyaudio
  Running setup.py clean for pyaudio
Failed to build pyaudio
Installing collected packages: pyaudio
  Running setup.py install for pyaudio ... error
    Complete output from command /Users/harry/Desktop/ml/gui/bin/python3 -u -c "import setuptools, tokenize;__file__='/private/var/folders/vd/8zl261fj35j8pst5659glmjc0000gn/T/pip-build-gj9ny3f9/pyaudio/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /var/folders/vd/8zl261fj35j8pst5659glmjc0000gn/T/pip-45sl0b3v-record/install-record.txt --single-version-externally-managed --compile --install-headers /Users/harry/Desktop/ml/gui/include/site/python3.6/pyaudio:
    running install
    running build
    running build_py
    creating build
    creating build/lib.macosx-10.12-x86_64-3.6
    copying src/pyaudio.py -> build/lib.macosx-10.12-x86_64-3.6
    running build_ext
    building '_portaudio' extension
    creating build/temp.macosx-10.12-x86_64-3.6
    creating build/temp.macosx-10.12-x86_64-3.6/src
    clang -Wno-unused-result -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -DMACOSX=1 -I/usr/local/include -I/usr/local/opt/openssl/include -I/usr/local/opt/sqlite/include -I/Users/harry/Desktop/ml/gui/include -I/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/include/python3.6m -c src/_portaudiomodule.c -o build/temp.macosx-10.12-x86_64-3.6/src/_portaudiomodule.o
    src/_portaudiomodule.c:29:10: fatal error: 'portaudio.h' file not found
    #include "portaudio.h"
             ^~~~~~~~~~~~~
    1 error generated.
    error: command 'clang' failed with exit status 1

Usually, things go fine when you try to install modules using pip but sometimes, you might face this issue in some OS distributions. 

Fix for Windows Users

If you quickly want to fix this error in windows, you can head to the unofficial python binaries for windows and download the pyaudio wheel for windows. 

Next, head to your terminal and cd into the directory where you downloaded this wheel file. Type the following command in the terminal. 

pip install <pyaudio-file-name.whl>

This command will install the pre-built wheel file to your computer, which means we don't need to build it from the source. The step which was creating an issue has been fixed by downloading this wheel binary.

The wheel file I downloaded was named PyAudio‑0.2.11‑cp37‑cp37m‑win32.whl, so I ran

pip install PyAudio‑0.2.11‑cp37‑cp37m‑win32.whl

and voila, the error is gone now.

Note: If you are a beginner and don't know what cd into a directory means. You can go to the folder where you downloaded this wheel file and press shift and right-click on some empty area to get an option called "Open command prompt/Powershell window here." This command will open the command prompt in the directory where you have downloaded this wheel file. Now you can type pip install Pyaudio-file-name.whl

Fix for Mac Users

If you are using Mac OS X, you can install the prerequisite portaudio library using homebrew by typing the following commands in the terminal:

brew install portaudio 
pip install pyaudio

Since homebrew takes care of portaudio installation, pyaudio will now be installed without any issues!

 

Fix for Ubuntu 18, Ubuntu 20 & other Ubuntu users 

If you are an Ubuntu user, you can go to the terminal and install portaudio dev version using apt. Type the following command into the terminal to fix this error.

sudo apt install portaudio19-dev python-pyaudio

Make sure to install pyaudio using pip after installing portaudio. Happy coding!

Add a new comment