Ubuntu 18.04服务器pip install regex失败,求解决方法
解决Ubuntu 18.04上
pip install regex时gcc编译失败的问题 你遇到的x86_64-linux-gnu-gcc failed with exit status 4错误在编译Python扩展包时很常见,结合你的场景,这里有几个经过验证的解决步骤:
先确认你的报错信息(方便后续排查):
error: command 'x86_64-linux-gnu-gcc' failed with exit status 4 'Failed building wheel for regex' error: command 'x86_64-linux-gnu-gcc' failed with exit status 4 ---------------------------------------- Command "/home/rc/Env/rcasipe/bin/python3 -u -c "import setuptools, tokenize;__file__='/tmp/pip-install-ti8hji_4/regex/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-record-i25nnqtk/install-record.txt --single-version-externally-managed --compile --install-headers /home/rc/Env/rcasipe/include/site/python3.6/regex" failed with error code 1 in /tmp/pip-install-ti8hji_4/regex/
解决步骤
1. 补全Python开发与编译依赖
虽然你提到已经装了一些相关包,但可能缺少Python 3.6的开发头文件或者基础编译工具。执行以下命令安装完整依赖:
sudo apt update && sudo apt install python3.6-dev gcc build-essential
这个命令会更新软件源并安装编译Python扩展所需的核心工具和头文件,很多时候能直接解决编译报错。
2. 排查内存不足问题
GCC退出状态4最常见的诱因之一是服务器内存不足,尤其是在小内存的VPS或云服务器上。你可以先用free -h查看当前内存使用:
free -h
如果可用内存低于512MB,建议临时创建交换分区来补充内存:
# 创建1GB交换文件 sudo fallocate -l 1G /swapfile sudo chmod 600 /swapfile sudo mkswap /swapfile sudo swapon /swapfile
完成pip install regex后,若不需要交换分区,可以用以下命令清理:
sudo swapoff /swapfile sudo rm /swapfile
3. 直接安装预编译Wheel包
如果本地编译始终有问题,你可以强制pip下载预编译好的Wheel包,跳过编译步骤:
pip install --only-binary :all: regex
这个命令会让pip从PyPI获取已经编译好的二进制包,避免本地编译的麻烦。
4. 更新pip、setuptools和wheel
旧版本的包管理工具也可能导致编译兼容性问题,先更新它们:
pip install --upgrade pip setuptools wheel
更新完成后再尝试安装regex。
内容的提问来源于stack exchange,提问作者djangoscholar
相关产品推荐
相关产品推荐

