Docker容器安装numpy==2.0.0遇cc编译器错误,求解决方案
解决Docker中安装numpy==2.0.0时的编译错误
问题重现
在Docker容器中执行以下命令时出现编译错误:
# pip install numpy==2.0.0 Collecting numpy==2.0.0 Downloading numpy-2.0.0.tar.gz (18.3 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 18.3/18.3 MB 35.3 MB/s eta 0:00:00 Installing build dependencies ... done Getting requirements to build wheel ... done Installing backend dependencies ... done Preparing metadata (pyproject.toml) ... error error: subprocess-exited-with-error × Preparing metadata (pyproject.toml) did not run successfully. │ exit code: 1 ╰─> [12 lines of output] + /usr/local/bin/python3.13 /tmp/pip-install-l4g9wf4v/numpy_5970e636fac947f4876fa7f4f02504f9/vendored-meson/meson/meson.py setup /tmp/pip-install-l4g9wf4v/numpy_5970e636fac947f4876fa7f4f02504f9 /tmp/pip-install-l4g9wf4v/numpy_5970e636fac947f4876fa7f4f02504f9/.mesonpy-fskz9xxu -Dbuildtype=release -Db_ndebug=if-release -Db_vscrt=md --native-file=/tmp/pip-install-l4g9wf4v/numpy_5970e636fac947f4876fa7f4f02504f9/.mesonpy-fskz9xxu/meson-python-native-file.ini The Meson build system Version: 1.2.99 Source dir: /tmp/pip-install-l4g9wf4v/numpy_5970e636fac947f4876fa7f4f02504f9 Build dir: /tmp/pip-install-l4g9wf4v/numpy_5970e636fac947f4876fa7f4f02504f9/.mesonpy-fskz9xxu Build type: native build Project name: NumPy Project version: 2.0.0 ../meson.build:1:0: ERROR: Compiler cc cannot compile programs. A full log can be found at /tmp/pip-install-l4g9wf4v/numpy_5970e636fac947f4876fa7f4f02504f9/.mesonpy-fskz9xxu/meson-logs/meson-log.txt [end of output] note: This error originates from a subprocess, and is likely not a problem with pip. error: metadata-generation-failed × Encountered error while generating package metadata. ╰─> See above for output. note: This is an issue with the package mentioned above, not pip. hint: See above for details.
环境信息
| 工具 | 版本 |
|---|---|
| 主机系统 | macOS 13.6 |
| Docker镜像 | python:3.13.3-slim |
| Python | 3.13.3 |
| numpy | 2.0.0 |
解决方案
1. 安装编译依赖工具链
python:3.13.3-slim镜像为缩小体积移除了C编译器及相关构建工具,安装numpy前需先补全依赖:
apt update && apt install -y build-essential gcc
执行完成后,重新运行pip install numpy==2.0.0即可正常安装。
2. (可选)直接使用预编译wheel包
若不想安装编译工具,可尝试强制pip使用预编译二进制包(需对应平台存在numpy 2.0.0的预编译wheel):
pip install numpy==2.0.0 --only-binary :all:
原因说明
numpy 2.0.0在当前环境中需要从源码编译,但slim镜像缺少必要的C编译器(gcc)和构建工具链,导致Meson构建系统无法完成编译检查,从而抛出Compiler cc cannot compile programs错误。
内容的提问来源于stack exchange,提问作者siruku6
相关产品推荐
相关产品推荐

