How to solve installation error of PyAudio in Ubuntu 18.04 (fatal error: 'portaudio.h' file not found)

Usually, you will be able to install PyAudio with PIP using the following command:

pip install PyAudio

Pitifully, in some distributions of Ubuntu, you will face an exception about a failure when building the wheel for pyaudio. According to the Python Packaging Index’s description, a wheel *is designed to contain all the files for a PEP 376 compatible install in a way that is very close to the on-disk format.

Error

The error output when you try to install the package will be:

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/kj/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/kj/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/kj/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

The error is basically caused by the missing portaudio.h file in the system. PortAudio is a free, cross-platform, open-source, audio I/O library.

Solution

You can fix this exception installing the dev portaudio package and pyaudio with the following command in Ubuntu:

sudo apt-get install portaudio19-dev python-pyaudio

Finally install the package again with the command:

pip install PyAudio

And you should be able to install it properly without an issue !

Happy coding !

This could interest you

Become a more social person