92

I'm using mac osx 10.10 As the PyAudio Homepage said, I install the PyAudio using

brew install portaudio 
pip install pyaudio

the installation of portaudio seems successful, I can find headers and libs in /usr/local/include and /usr/local/lib but when I try to install pyaudio, it gives me an error that

src/_portaudiomodule.c:29:10: fatal error: 'portaudio.h' file not found
#include "portaudio.h"
         ^
1 error generated.
error: command 'cc' failed with exit status 1

actually it is in /usr/local/include why can't it find the file? some answers to similar questions are not working for me(like using virtualenv, or compile it manually), and I want to find a simple way to solve this.

Xun Jian
  • 931
  • 1
  • 6
  • 7

14 Answers14

193

Since pyAudio has portAudio as a dependency, you first have to install portaudio.

brew install portaudio

Then try: pip install pyAudio. If the problem persists after installing portAudio, you can specify the directory path where the compiler will be able to find the source programs (e.g: portaudio.h). Since the headers should be in the /usr/local/include directory:

pip install --global-option='build_ext' --global-option='-I/usr/local/include' --global-option='-L/usr/local/lib' pyaudio
Paul Schreiber
  • 12,531
  • 4
  • 41
  • 63
fukudama
  • 1,996
  • 2
  • 11
  • 8
  • The version of PyAudio was 0.2.9. – fukudama Nov 20 '15 at 08:21
  • 3
    This seems to work, but why? Shouldn't /usr/local directories be the default? build_ext seems to run (and fail to find things) by default. – Sam Brightman Nov 28 '15 at 12:33
  • 2
    It works for me too. It seems you specified the paths to look for headers manually, is there any way to configure it as default? – Xun Jian Nov 29 '15 at 04:09
  • 4
    this worked for me, but I had to use sudo on my machine (El Capitan, upgraded from Yosemite) – pkuhar Mar 01 '16 at 05:36
  • I had to use sudo on the pip install command to get it to finally install. – Troy May 02 '17 at 20:11
  • 2
    For anyone using macports, /opt/local/include and /opt/local/lib worked for me – Dylan Knight Jul 18 '19 at 16:51
  • Thanks man, that helped. (we have to explicitly add the user's local lib and includes) – nityanarayan44 Oct 01 '19 at 15:04
  • 1
    To get the explicit path to portaudio's header files, use `brew info portaudio`. – Gino Mempin Sep 16 '21 at 08:41
  • 7
    Here's a bit more robust snippet for those who have a non-default homebrew dir, `pip install --global-option='build_ext' --global-option="-I$(brew --prefix)/include" --global-option="-L$(brew --prefix)/lib" pyaudio` – mathandy Nov 04 '21 at 00:42
  • 1
    Neither this nor @mathandy's command worked for me... I still get ERROR: Could not build wheels for pyaudio, which is required to install pyproject.toml-based projects – whenitrains Oct 17 '22 at 06:01
  • 2
    For me this worked: https://stackoverflow.com/a/73338550/1822138 – Martin Nov 04 '22 at 10:18
  • None of the suggestions have worked for me. I've tried everything in all responses and it can't find my `portaudio.h` which is definitely there. Super weird! – Ben Kushigian Apr 12 '23 at 08:19
29

On Ubuntu builds:

sudo apt-get install python-pyaudio

For Python3:

sudo apt-get install python3-pyaudio
Robbie Matthews
  • 1,404
  • 14
  • 22
17

You have to install portaudio first then link that file. Only then you can find that header file (i.e, portaudio.h). To install portaudio in mac by using HomeBrew program use following commands.

brew install portaudio
brew link portaudio
pip install pyaudio

sudo is not needed if you're admin. We should refrain using sudo as it messes up lots of permissions.

chaitanya
  • 693
  • 2
  • 7
  • 16
17

I needed to do the following to install PortAudio on Debian

sudo apt install portaudio19-dev

I also apt install'd python3-portaudio before that, although it didn't work. I'm not sure if that contributed as well.

sawyermclane
  • 896
  • 11
  • 28
14

First, you can use Homebrew to install portaudio.

brew install portaudio

Then try to find the portaudio path:

sudo find / -name "portaudio.h"

In my case it is at /usr/local/Cellar/portaudio/19.6.0/include .

Run the command below to install pyaudio

pip install --global-option='build_ext' --global-option='-I/usr/local/Cellar/portaudio/19.6.0/include' --global-option='-L/usr/local/Cellar/portaudio/19.6.0/lib' pyaudio
Andrew Palmer
  • 2,693
  • 2
  • 13
  • 14
Harry
  • 177
  • 1
  • 5
  • 3
    Instead of `sudo find`, you can also get the path to `portaudio/x.x.x/include` using `brew info portaudio`, which should print out a path like `/usr/local/Cellar/portaudio/19.7.0`. Then it's just a matter of adding `/include`. – Gino Mempin Sep 20 '21 at 07:11
  • In my case, I was trying to install `neopixel`, which required `pyaudio`, which needed `portaudio.h`. Simply doing `brew install portaudio` was enough to resolve this issue. Thanks! – schimmy Jan 29 '23 at 19:12
9

On Raspbian:

sudo apt-get install python-pyaudio
danielrosero
  • 596
  • 8
  • 14
  • 1
    On raspbian with python-env or conda envs: `sudo apt install portaudio19-dev & pip install pyaudio` – mdev May 25 '21 at 10:55
8

on Centos:

yum install -y portaudio portaudio-devel && pip install pyaudio
timest
  • 188
  • 2
  • 9
8

Just for the record for folks using MacPorts and not Homebrew:

$ [sudo] port install portaudio
$ pip install pyaudio --global-option="build_ext"  --global-option="-I/opt/local/include" --global-option="-L/opt/local/lib"
PartialOrder
  • 2,870
  • 3
  • 36
  • 44
  • 1
    `$(brew --prefix)` won't work for those without Homebrew. This answer was specifically for those using MacPorts and not Homebrew. – PartialOrder Nov 15 '21 at 00:22
5

Adding a bit of robustness (in case of a non-default homebrew dir) to the snippet from @fukudama,

brew install portaudio
pip install --global-option='build_ext' --global-option="-I$(brew --prefix)/include" --global-option="-L$(brew --prefix)/lib" pyaudio
mathandy
  • 1,892
  • 25
  • 32
4

For me on 10.10.5 the paths were under /opt/local. I had to add /opt/local/bin to my /etc/paths file. And the command line that worked was

sudo pip install --global-option='build_ext' --global-option='-I/opt/local/include' --global-option='-L/opt/local/lib' pyaudio
Andrew Palmer
  • 2,693
  • 2
  • 13
  • 14
Eric Saund
  • 113
  • 8
2

this is the tested answer for MacBook Pro m2 chip:

first find the location of the portaudio.h file by

sudo find / -name "portaudio.h"

then, once you find the location, copy it and use it in this command.

LDFLAGS="-L/{opt/homebrew/Cellar/portaudio/19.7.0/}lib" CFLAGS="-I/{opt/homebrew/Cellar/portaudio/19.7.0}/include" pip3 install pyaudio

Here replace the location from { } into you file location hopefully this works. I have tried above solutions and this one worked for me.

Jithin Palepu
  • 596
  • 1
  • 7
  • 18
1

If you are using anaconda/miniconda to manage your python environments then

conda install pyaudio

installs portaudio at the same time as pyaudio

The following NEW packages will be INSTALLED:

  portaudio          pkgs/main/osx-64::portaudio-19.6.0-h647c56a_4
  pyaudio            pkgs/main/osx-64::pyaudio-0.2.11-py37h1de35cc_2
Dixit
  • 51
  • 1
  • 6
1

On Termux (this is what worked for me):

  1. pkg install python
  2. bash -c "$(curl -fsSL https://its-pointless.github.io/setup-pointless-repo.sh)"
  3. pkg install portaudio
  4. pip install pyaudio

Source: pyaudio installing #6235

Giorgos Xou
  • 1,461
  • 1
  • 13
  • 32
1

For M1 mac, this is worked for me:

LDFLAGS="-L/opt/homebrew/Cellar/portaudio/19.7.0/lib" CFLAGS="-I/opt/homebrew/Cellar/portaudio/19.7.0/include" pip3 install pyaudio

Res:

 Created wheel for pyaudio: filename=PyAudio-0.2.12-cp310-cp310-macosx_11_0_arm64.whl size=24170 sha256=c74eb581e6bca2400f681f68d33654002722969f1a455ffce87e4e5da05471d8
  Stored in directory: /private/var/folders/m_/kzyr4q_11cl35ngrj77k28f00000gn/T/pip-ephem-wheel-cache-ql1x8ums/wheels/93/08/0b/b915ab1895927641737175e5bc7b6111e8ed0c26daabeecba0
Successfully built pyaudio
Installing collected packages: pyaudio
Successfully installed pyaudio-0.2.12

Be noted, do not using find / its very slow and stupid, using brew info portaudio

Nicholas Jela
  • 2,540
  • 7
  • 24
  • 40