bastibe/PySoundCard

Issues installing on raspbian

Closed this issue · 16 comments

Hi, I had a few issues trying to install pysoundcard on raspbian (raspberry pi-specific debian), and I wanted to share my fix in case anyone else has this issue.

Firstly, if installing PortAudio using apt, you have to use the correct package name. Simply sudo apt-get install portaudio doesn't work. You have to use sudo apt-get install portaudio19-dev

Then, if you try to install CFFI with sudo pip install cffi, you get an error:

(a bunch of stuff)...
c/_cffi_backend.c:15:17: fatal error: ffi.h: No such file or directory
#include <ffi.h>
^
compilation terminated.
error: command 'arm-linux-gnueabihf-gcc' failed with exit status 1

You have to first install libffi-dev, so run:
sudo apt-get install libffi-dev
and then (assuming you have numpy, which I did), you can install pysoundcard with
sudo pip install pysoundcard

Thank you for this information. It is true that every Linux distribution has it's own names for these packages. That's why I don't think I mention specific installation instructions anywhere.

It's good to hear that PySoundCard works on Raspbian, though!

@colin-svds: You don't need to install any development packages and you don't need to compile anything. The correct packages to install are libportaudio2 and python3-cffi (or python-cffi for Python 2), see http://python-sounddevice.readthedocs.org/en/latest/#requirements.

@mgeier Multiple ways to skin a cat, I suppose. I prefer to install python packages with pip, but I didn't know about libportaudio2

@colin-svds: You saved my day. I had the same issue with installing python-sounddevice for python 2. Installing libportaudio2 was not enough for some reason, ended up with the same error when pip install sounddevice, which required ffi.h for the compilation. Installing libffi-dev solved my issue.
Thanks,

@slee-ensil Nothing needs to be compiled on your system for the installation!
The most straightforward solution is to install python-cffi (or python3-cffi for Python 3), then the package libffi-dev is not needed.

You can or course compile everything on your system if you want to, but do you have a reason to?

@mgeier I wish I don't have to. That pip install actually did the compilation by itself then failed. I saw a couple of cases of people complaining about import error when they tried to use sounddevice. I presume they didn't notice the compilation error when they did pip install. And I presume that it only happens in python 2.7.

I wish I don't have to.

You don't have to.
You just have to install (but not compile!) the necessary dependencies first.

That pip install actually did the compilation by itself then failed.

pip install only tries to compile CFFI if it is not yet installed (for the appropriate version of Python). You should install CFFI first (using the Linux package manager, not using pip!).

Did you have the Debian package python-cffi installed before running pip install?

If yes, do you have multiple versions of Python 2 installed?

Maybe there is something else missing ... we should find that out!

I saw a couple of cases of people complaining about import error when they tried to use sounddevice.

Can you please point me to those complaints?
Or can you tell them to open a sounddevice issue: https://github.com/spatialaudio/python-sounddevice/issues?

I would like to make the installation as painless as possible for everyone, but I need the help of those people where it doesn't work smoothly!

@mgeier

Did you have the Debian package python-cffi installed before running pip install?

Yes I did.

If yes, do you have multiple versions of Python 2 installed?

Yes, I have.

More specifically, I started with raspbian-jessie 2016-11-25 version, which already has both Python 2.7 and Python 3.x, pip for both versions and even libportaudio2. What I have installed are python-numpy and python-cffi packages using apt-get. Then I did pip install sounddevice.

Can you please point me to those complaints?

Sorry I have no recollection for now. I will tell them if I have a chance.

OK, then something has to be wrong. Let's try to get to the bottom of this ...

Are you able to restore the situation before doing pip install sounddevice but after installing python-cffi?

You can try to do:

pip uninstall sounddevice
pip uninstall cffi

... and answer with the y key when asked if you want to remove the listed files.

At this point, you can try to open an interactive Python prompt and type:

>>> import cffi

If this raises an error, something is wrong.
If not, something else is wrong.

In the latter case, you should follow this up with

>>> cffi.__path__

This should tell you from where the CFFI module was imported from.

What do the following commands show?

pip --version
which pip
python -m pip --version
python --version
which python

I have the suspicion that there will be some mismatch there ...

I did

sudo pip unistall sounddevice
sudo pip uninstall cffi

After that, import cffi did not raise any error. The cffi path reported was

>>> cffi.__path__
['/usr/lib/python2.7/dist-packages/cffi']

And the rest are

$ pip --version
pip 1.5.6 from /usr/lib/python2.7/dist-packages (python 2.7)
$ which pip
/usr/bin/pip
$ python -m pip --version
pip 1.5.6 from /usr/lib/python2.7/dist-packages (python 2.7)
$ python --version
Python 2.7.9
$ which python
/usr/bin/python

One more thing. When I tried to reinstall the cffi (since I uninstalled it), I got this

$ sudo pip install cffi
Requirement already satisfied (use --upgrade to upgrade): cffi in /usr/lib/python2.7/dist-packages
Requirement already satisfied (use --upgrade to upgrade): pycparser in /usr/lib/python2.7/dist-packages (from cffi)
Cleaning up...

Then pip install sounddevice did the compilation again.

$ sudo pip install sounddevice
Downloading/unpacking sounddevice
  Downloading sounddevice-0.3.6-py2.py3-none-any.whl
Downloading/unpacking CFFI>=1.0 (from sounddevice)
  Downloading cffi-1.9.1.tar.gz (407kB): 407kB downloaded
  Running setup.py (path:/tmp/pip-build-kT_lEi/CFFI/setup.py) egg_info for package CFFI
    
Requirement already satisfied (use --upgrade to upgrade): pycparser in /usr/lib/python2.7/dist-packages (from CFFI>=1.0->sounddevice)
Installing collected packages: sounddevice, CFFI
  Found existing installation: cffi 0.8.6
    Not uninstalling cffi at /usr/lib/python2.7/dist-packages, owned by OS
  Running setup.py install for CFFI
    building '_cffi_backend' extension
    arm-linux-gnueabihf-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fno-strict-aliasing -D_FORTIFY_SOURCE=2 -g -fstack-protector-strong -Wformat -Werror=format-security -fPIC -DUSE__THREAD -I/usr/include/python2.7 -c c/_cffi_backend.c -o build/temp.linux-armv7l-2.7/c/_cffi_backend.o
    arm-linux-gnueabihf-gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-z,relro -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -D_FORTIFY_SOURCE=2 -g -fstack-protector-strong -Wformat -Werror=format-security -Wl,-z,relro -D_FORTIFY_SOURCE=2 -g -fstack-protector-strong -Wformat -Werror=format-security build/temp.linux-armv7l-2.7/c/_cffi_backend.o -lffi -o build/lib.linux-armv7l-2.7/_cffi_backend.so

Thanks for the further information, now I know what's the problem!

The Debian/Raspbian package you installed is providing CFFI version 0.8.6, but the sounddevice module requires at least version 1.0 (see spatialaudio/python-sounddevice#49 for why).

I see those possibilities for how to deal with that as a user:

I don't know which one is more complicated, but under the circumstances, local compilation may actually be a reasonable way to proceed.

RezzZ commented

@slee-ensil Nothing needs to be compiled on your system for the installation!
The most straightforward solution is to install python-cffi (or python3-cffi for Python 3), then the package libffi-dev is not needed.

You can or course compile everything on your system if you want to, but do you have a reason to?

installing python3-cffi doesn't solve my problem. still get the error:

Failed to build these modules:
_ctypes

so it seems I still need libffi-dev.
only problem, raspbian stretch doens't know where to get it from: E: Unable to locate package libffi_dev.

This is strange. The _ctypes module is part of Python itself (see https://github.com/python/cpython/tree/master/Modules/_ctypes), it is not part of CFFI.

You indeed need libffi-dev to compile Python (and there are a few issues in the Python issue tracker: https://bugs.python.org/issue30090, https://bugs.python.org/issue34385).
But I guess you don't actually want to compile Python, right?

I don't have a Raspberry Pi, so I can't really reproduce all this ...

Are you sure all your package sources are up to date and you use the latest versions of everything?

RezzZ commented

I had a stupid typo in the libffi-dev (underscore instead of dash). After fixing that I had no problems any more. Sorry for not updating

worked for me (conda):

pip uninstall cffi
conda install -c anaconda cffi