温馨提示×

centos装pytorch慢为何

小樊
49
2026-10-18 02:16:16
栏目: 智能运维

在 CentOS 上安装 PyTorch 感觉“很慢”,通常不是 CentOS 本身的问题,而是网络、源、依赖和硬件环境共同造成的。下面按常见原因给你拆解,并给出对应解决办法。


一、最常见原因:网络访问官方源太慢(90%的情况)

PyTorch 的官方下载地址是:

  • https://download.pytorch.org
  • https://pypi.org

在国内直接访问经常是 几十 KB/s 甚至超时。

表现

  • pip install torch 卡在 Downloading
  • 下载 .whl 文件极慢
  • 经常 Read timed out

解决办法 ✅

1️⃣ 使用国内镜像(强烈推荐)

pip install torch torchvision \
  -i https://pypi.tuna.tsinghua.edu.cn/simple

或永久配置:

mkdir -p ~/.pip
cat > ~/.pip/pip.conf <<EOF
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
EOF

注意:
PyTorch 的 wheel 不一定在所有镜像都有完整版本,清华、中科大通常较全。


二、装的是“CPU 版”还是“GPU 版”差别巨大

GPU 版 PyTorch

  • 单个 wheel 文件 600MB~2GB
  • 下载慢 + 解压慢

原因

  • CUDA runtime 被打包进 wheel
  • CentOS 服务器带宽通常不高

建议

✅ 如果不需要 GPU:

pip install torch torchvision cpuonly \
  -i https://pypi.tuna.tsinghua.edu.cn/simple

✅ 需要 GPU 时:

  • 用 conda 安装(见下文)

三、CentOS 默认 Python 环境“坑”很多

CentOS 7/8:

  • Python 2.7 / 3.6
  • 系统 Python 被 yum 依赖,不能乱动

常见现象

  • pip 版本旧
  • 编译依赖缺失
  • 安装时自动尝试 源码编译(超级慢)

解决办法 ✅

用 conda(最推荐)

wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
bash Miniconda3-latest-Linux-x86_64.sh

然后:

conda install pytorch torchvision torchaudio pytorch-cuda=11.8 -c pytorch -c nvidia

✅ 优点:

  • 预编译二进制
  • 不需要 gcc / cmake
  • 速度快很多

四、缺少编译依赖(源码安装时才慢)

如果你看到:

Building wheel for torch (setup.py) ...

说明 走源码编译了,这会:

  • 耗时 30 分钟~几小时
  • 吃满 CPU
  • 极易失败

避免方法

  • 不要 --no-binary
  • 不要装过老的 Python
  • 用 conda 或官方 wheel

五、磁盘 / 内存也可能拖慢

CentOS 服务器常见情况:

  • 机械硬盘(HDD)
  • 内存小(< 4GB)
  • /tmp 空间不足

表现

  • 下载完了,但“安装中”很慢
  • pip 卡在 Installing collected packages

检查

df -h
free -h

六、推荐的最佳实践(总结)

✅ 最快最稳方案

  1. 用 Miniconda
  2. 用官方 conda 命令
  3. 国内网络 + 代理(如有)

✅ 纯 pip 方案

  • Python ≥ 3.8
  • 国内镜像
  • CPU 版优先

如果你愿意,可以告诉我:

  • CentOS 版本(7 / 8 / Stream)
  • 是否要 GPU
  • Python 版本
  • 用的是 pip 还是 conda

我可以直接给你一条可复制的安装命令。

0 踩