基于Ubuntu 22.04的Dockerfile安装numpy失败求助
基于Ubuntu 22.04构建Docker镜像时Numpy安装编译失败的解决办法
问题背景
在基于Ubuntu 22.04构建Docker镜像过程中,安装numpy==1.18.1时持续触发编译错误,尝试多种常规方案后仍无法解决。
原Dockerfile内容
FROM ubuntu:22.04 # ENV PATH="/scripts:${PATH}" COPY ./requirements.txt /requirements.txt WORKDIR / RUN apt-get update && \ apt-get install -y software-properties-common && add-apt-repository ppa:deadsnakes/ppa RUN apt-get update RUN apt-get install -y wget tar python3.7 python3.7-dev libc-dev gcc make python3-pip build-essential libcurl4-openssl-dev libpq-dev libssl-dev libffi-dev libjpeg-dev libfreetype6-dev zlib1g-dev redis-server RUN wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb RUN apt install -y ./google-chrome-stable_current_amd64.deb RUN wget http://prdownloads.sourceforge.net/ta-lib/ta-lib-0.4.0-src.tar.gz && tar -xzvf ta-lib-0.4.0-src.tar.gz WORKDIR /ta-lib RUN ./configure --prefix=/usr && make && make install RUN pip install --upgrade pip RUN pip install setuptools==65.5.0 RUN pip install cython==0.29.37 RUN pip install importlib-metadata==4.13.0 RUN pip install numpy==1.18.1
报错信息(末尾部分)
119.5 error: Command "x86_64-linux-gnu-gcc -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O2 -Wall -g -fstack-protector-strong -Wformat -Werror=format-security -g -fwrapv -O2 -fPIC -DNPY_INTERNAL_BUILD=1 -DHAVE_NPY_CONFIG_H=1 -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE=1 -D_LARGEFILE64_SOURCE=1 -Ibuild/src.linux-x86_64-3.10/numpy/core/src/umath -Ibuild/src.linux-x86_64-3.10/numpy/core/src/npymath -Ibuild/src.linux-x86_64-3.10/numpy/core/src/common -Inumpy/core/include -Ibuild/src.linux-x86_64-3.10/numpy/core/include/numpy -Inumpy/core/src/common -Inumpy/core/src -Inumpy/core -Inumpy/core/src/npymath -Inumpy/core/src/multiarray -Inumpy/core/src/umath -Inumpy/core/src/npysort -I/usr/include/python3.10 -Ibuild/src.linux-x86_64-3.10/numpy/core/src/common -Ibuild/src.linux-x86_64-3.10/numpy/core/src/npymath -c build/src.linux-x86_64-3.10/numpy/core/src/multiarray/scalartypes.c -o build/temp.linux-x86_64-cpython-310/build/src.linux-x86_64-3.10/numpy/core/src/multiarray/scalartypes.o -MMD -MF build/temp.linux-x86_64-cpython-310/build/src.linux-x86_64-3.10/numpy/core/src/multiarray/scalartypes.o.d -std=c99" failed with exit status 1 119.5 [end of output] 119.5 119.5 note: This error originates from a subprocess, and is likely not a problem with pip. 119.5 ERROR: Failed building wheel for numpy 119.5 Failed to build numpy 119.5 ERROR: Could not build wheels for numpy, which is required to install pyproject.toml-based projects
解决步骤及修改后的Dockerfile
核心问题分析
报错中显示编译器使用Python3.10的头文件,但Dockerfile中安装的是Python3.7,说明pip默认关联了系统自带的Python3.10,版本不匹配触发编译失败;同时旧版本Numpy在Ubuntu22.04的高版本gcc下需要兼容编译参数。
修改方案
- 替换
python3-pip为python3.7-pip,确保pip与目标Python版本一致 - 所有pip命令改用
pip3.7执行 - 添加环境变量禁用gcc的严格安全编译选项,适配旧版本Numpy的编译逻辑
- 合并冗余的RUN指令,优化镜像构建效率
修改后的Dockerfile
FROM ubuntu:22.04 COPY ./requirements.txt /requirements.txt WORKDIR / # 安装依赖并配置Python3.7 RUN apt-get update && \ apt-get install -y software-properties-common && \ add-apt-repository ppa:deadsnakes/ppa && \ apt-get update && \ apt-get install -y wget tar python3.7 python3.7-dev python3.7-pip libc-dev gcc make build-essential libcurl4-openssl-dev libpq-dev libssl-dev libffi-dev libjpeg-dev libfreetype6-dev zlib1g-dev redis-server && \ # 安装Chrome wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb && \ apt install -y ./google-chrome-stable_current_amd64.deb && \ # 安装TA-Lib wget http://prdownloads.sourceforge.net/ta-lib/ta-lib-0.4.0-src.tar.gz && \ tar -xzvf ta-lib-0.4.0-src.tar.gz && \ cd /ta-lib && \ ./configure --prefix=/usr && make && make install && \ # 清理临时文件 cd / && rm -rf ta-lib ta-lib-0.4.0-src.tar.gz google-chrome-stable_current_amd64.deb # 设置编译兼容环境变量 ENV CFLAGS="-Wno-error=format-security" # 使用Python3.7的pip安装依赖 RUN pip3.7 install --upgrade pip && \ pip3.7 install setuptools==65.5.0 cython==0.29.37 importlib-metadata==4.13.0 numpy==1.18.1
内容的提问来源于stack exchange,提问作者Mohit Sharma
相关产品推荐
相关产品推荐

