@TOC
Ubuntu20.04 新系统安装PyAudio问题
用VMware虚拟机新装了一个Ubuntu系统,使用的默认配置安装,可能默认采用了最小安装,因此系统的一些依赖缺很多,导致配置环境时出现很多问题。
常规方法
首先,常规直接使用pip安装pyaudio,基本都会因为缺少相关依赖而报错,因此需要安装portaudio19-dev
,注意,这个需要使用apt进行安装,它会将相关依赖安装上:
sudo apt-get install portaudio19-dev
正常情况下,再安装pyaudio一般不会报错了:
pip install pyaudio
Conda安装
还有另外一种安装方式,直接使用conda进行安装,能解决大部分依赖问题,绝对比pip安装可靠,只需要替换一下镜像源,就可以愉快的使用了。
conda install pyaudio
实测使用时,会自动安装一个portaudio相关的库,不过当时未记录截图,而且是在WSL2的虚拟环境中安装的。
众所周知,WSL2默认安装的环境,是不支持普通USB声卡,或者其它音频驱动,目前默认环境只解决了GUI显示问题,想实时播放音频还需要使用网络方式转接,这也是我放弃WSL2的Ubuntu而选择VMware的原因。
关键问题
之所以记录这篇博客,就是想提示一下大家,安装PyAudio时,报错:
Failed to build pyaudio
ERROR: Could not build wheels for pyaudio, which is required to install pyproject.toml-based projects
不一定只是portaudio19-dev
依赖的原因,我这个将依赖安装完成后,出现的报错内容如下:
Building wheels for collected packages: pyaudio
Building wheel for pyaudio (pyproject.toml) ... error
error: subprocess-exited-with-error
× Building wheel for pyaudio (pyproject.toml) did not run successfully.
│ exit code: 1
╰─> [14 lines of output]
running bdist_wheel
running build
running build_py
creating build
creating build/lib.linux-x86_64-cpython-311
creating build/lib.linux-x86_64-cpython-311/pyaudio
copying src/pyaudio/__init__.py -> build/lib.linux-x86_64-cpython-311/pyaudio
running build_ext
building 'pyaudio._portaudio' extension
creating build/temp.linux-x86_64-cpython-311
creating build/temp.linux-x86_64-cpython-311/src
creating build/temp.linux-x86_64-cpython-311/src/pyaudio
gcc -pthread -B /home/he/miniconda3/envs/TTS/compiler_compat -DNDEBUG -fwrapv -O2 -Wall -fPIC -O2 -isystem /home/he/miniconda3/envs/TTS/include -fPIC -O2 -isystem /home/he/miniconda3/envs/TTS/include -fPIC -I/usr/local/include -I/usr/include -I/home/he/miniconda3/envs/TTS/include/python3.11 -c src/pyaudio/device_api.c -o build/temp.linux-x86_64-cpython-311/src/pyaudio/device_api.o
error: command 'gcc' failed: No such file or directory
[end of output]
note: This error originates from a subprocess, and is likely not a problem with pip.
ERROR: Failed building wheel for pyaudio
Failed to build pyaudio
ERROR: Could not build wheels for pyaudio, which is required to install pyproject.toml-based projects
注意一下报错内容重点:error: command ‘gcc’ failed:No such file or directory
我属实没想到,Ubuntu安装后,默认竟然没有gcc!!!
sudo apt-get install g++
果然,做开发,还是要细心一点,之前没有仔细阅读一下错误信息,浪费不少时间。