背景
最近在做环境迁移,需要将一个包含LLM、PyTorch的深度学习环境,从一台单卡A100开发机迁移到一台拥有8卡A100的高性能服务器集群上。
源服务器:环境较新,Python3.10,CUDA13+,PyTorch2.9(预览版)。
目标服务器:硬件很强,但系统老旧(CentOS7),底层 glibc 版本仅为2.17,且没有root权限,无法安装系统级编译器(g++, cmake, rust)。
本以为简单的 pip install -r requirements.txt 就能搞定,结果不太行。
踩坑记录
坑位 1:直接解压 Conda 环境包失败
起初尝试将源服务器打包好的 conda_env.tar.gz 直接解压到新服务器使用。
报错:version 'GLIBC_2.29' not found。
原因:Conda环境中的二进制文件是基于源服务器较新的Linux内核编译的,直接搬到glibc 2.17的老机器上,底层ABI不兼容,Python解释器直接无法运行。跨操作系统版本迁移,千万不要用tar包直接解压,必须重建环境。
坑位 2:Conda Create 创建环境失败
操作:conda create -n env python=3.10.19
报错:The following specifications were found to be incompatible with your system,提示系统glibc版本过低。
解决:降低Python小版本号,改用Python 3.10.12或3.9,这些版本对老系统的兼容性更好。
坑位 3:硬件绑定包冲突
报错:ERROR: No matching distribution found for cuda-bindings==13.1.1
原因:pip freeze 导出的列表包含了源机器特有的硬件驱动包。源机器是 CUDA 13,新机器是 CUDA 12.2,pip在源里找不到对应的包,且版本不匹配。
解决:删除 requirements.txt 中所有 nvidia-, cuda-, cupy-cuda* 的包,让 PyTorch 自动处理依赖。
坑位 4:源码编译地狱(Pandas, Numpy, Tiktoken, Sentencepiece)
这是最耗时的一步。当 pip 在源中找不到适配 glibc 2.17 的预编译包(Wheel)时,会自动下载源码尝试本地编译。
报错 A:安装 pandas==2.3.3 时,提示 Running g++ --version gave "[Errno 2] No such file or directory"。
报错 B:安装 tiktoken 时,提示 error: can't find Rust compiler。
原因:新服务器为了稳定性,没有安装 gcc/g++ 编译器,也没有 Rust 环境。
解决:见下文"终极方案"。
终极解决方案:Conda + Pip 混合安装法
针对"老系统 + 无编译器"的恶劣环境,单纯依靠 pip 是行不通的。必须利用 Conda 预编译二进制包 的优势来"降维打击"。
第一步:清洗 requirements.txt
编辑 requirements.txt,删除以下三类包:
硬件强绑定包:cuda-bindings, nvidia-, cupy-。
需要编译的底层包:numpy, pandas, pyzmq, sentencepiece, tiktoken。
版本号不存在的包:例如内部预览版的 torch==2.9.0。
第二步:利用 Conda 安装"硬骨头"
Conda 的包是预编译好的,自带依赖库,不依赖系统的 g++。
bash
# 创建环境
conda create -n <name> python=3.10 -y
conda activate <name>
# 优先用 Conda 安装这些编译困难户
# conda-forge 频道对老系统支持更好
conda install -c conda-forge pandas numpy pyzmq sentencepiece tiktoken -y
第三步:安装 PyTorch (官方稳定版)
根据目标机器的 CUDA 版本(例如 12.2),去官网找对应命令。不要用 txt 里的旧版本。
bash
# 安装适配 CUDA 12.1 的 PyTorch
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121
第四步:安装剩余 Python 包
最后处理剩下那些纯 Python 的库。
bash
# 使用国内源加速
pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
总结
在高性能计算集群(HPC)或老旧企业级服务器上部署环境时,pip install -r requirements.txt 往往是一个陷阱。
避开编译:遇到 g++ not found 或 rust compiler missing,立刻停止 pip,转投 Conda 的怀抱。
解绑硬件:迁移前务必清理掉列表中的 nvidia- 和 cuda- 包。
系统兼容:如果遇到 glibc 报错,首先考虑降低 Python 版本。