Unable to install PyAudio on M1 Mac [PortAudio already installed]

Viewed 6041

I have visited many forums, tried diffrent methods like brew, pip, port and many more but still am facing the same error.

View this Image for more detail

src/_portaudiomodule.c:29:10: fatal error: 'portaudio.h' file not found
    #include "portaudio.h"
             ^~~~~~~~~~~~~
    1 error generated.
    error: command '/usr/bin/gcc' failed with exit code 1

Can anyone help?

5 Answers

For me it was:

brew install portaudio
python -m pip install --global-option='build_ext' --global-option='-I/opt/homebrew/Cellar/portaudio/19.7.0/include' --global-option='-L/opt/homebrew/Cellar/portaudio/19.7.0/lib' pyaudio

This Solution is tested on M1 Macs[Please do check with other].

After the installation of HomeBrew on your system, perform the installation of PortAudio. Next follow the steps mentioned below:

Use the command to install PortAudio

sudo brew install portaudio

After successful installation of PortAudio, enter the following command.

sudo nano $HOME/.pydistutils.cfg

Next, enter the following lines in the opened window

[build_ext]
include_dirs=/Users/<enter-your-system-username>/homebrew/Cellar/portaudio/19.20140130/include/
include_dirs=/Users/<enter-your-system-username>/homebrew/Cellar/portaudio/19.20140130/lib/

Note: PortAudio location may be different for you and also don't forget to replace your PC username.

Finally run the command

pip install pyaudio

or

pip3 install pyaudio

These steps worked on M1 Pro chips

  1. Install portaudio
brew install portaudio
  1. Link portaudio
brew link portaudio
  1. Copy the path where portaudio was installed (use it in the next step)
brew --prefix portaudio
  1. Create .pydistutils.cfg in your home directory
sudo nano $HOME/.pydistutils.cfg

then paste the following

[build_ext]
include_dirs=<PATH FROM STEP 3>/include/
library_dirs=<PATH FROM STEP 3>/lib/

  1. Install pyaudio
pip install pyaudio

or

pip3 install pyaudio

vkshah has an error in his second line. It should ready library_dirs instead of include_dirs:

include_dirs=/Users/<enter-your-system-username>/homebrew/Cellar/portaudio/19.20140130/include/
library_dirs=/Users/<enter-your-system-username>/homebrew/Cellar/portaudio/19.20140130/lib/```

The main challenge we're encountering here is that pyaudio doesn't know where to find the portaudio libraries since, according to the source, it only looks in these locations for darwin platform installations:

include_dirs += ['/usr/local/include', '/usr/include']
external_libraries_path += ['/usr/local/lib', '/usr/lib']

This won't do if you're using a homebrew installation of portaudio, particularly so with Apple Silicon since there isn't a pre-built wheel in PyPi. This means setuptools/pip will need to build the package from source.

There are a few ways of configuring setuptools to use additional paths, including passing arguments, using a .pydistutils.cfg file, among others, but the second option worked for me given I'm installing pyaudio to a few Python environments.

Building off of some of the answers above, you can copy the following below to install pyaudio in one go.

# Install portaudio, will print list of paths where it's been installed if already done
brew list portaudio || brew install portaudio
# Link portaudio, will print a warning if already linked
brew link portaudio
# Create a pydistutils config file with the portaudio lib paths set correctly (no copying/pasting paths required)
cat <<EOF >> .pydistutils.cfg
[build_ext]
include_dirs=`brew --prefix portaudio`/include/
library_dirs=`brew --prefix portaudio`/lib/
EOF
# install (build) package with pip
python -m pip install pyaudio

All we're doing is telling setuptools to also look for C/C++ headers where portaudio has been installed by homebrew.

Related