在 CentOS 上安装 PyTorch 感觉“很慢”,通常不是 CentOS 本身的问题,而是网络、源、依赖和硬件环境共同造成的。下面按常见原因给你拆解,并给出对应解决办法。
PyTorch 的官方下载地址是:
https://download.pytorch.orghttps://pypi.org在国内直接访问经常是 几十 KB/s 甚至超时。
pip install torch 卡在 Downloading.whl 文件极慢Read timed outpip 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 不一定在所有镜像都有完整版本,清华、中科大通常较全。
✅ 如果不需要 GPU:
pip install torch torchvision cpuonly \
-i https://pypi.tuna.tsinghua.edu.cn/simple
✅ 需要 GPU 时:
CentOS 7/8:
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
✅ 优点:
如果你看到:
Building wheel for torch (setup.py) ...
说明 走源码编译了,这会:
--no-binaryCentOS 服务器常见情况:
/tmp 空间不足Installing collected packagesdf -h
free -h
✅ 最快最稳方案
✅ 纯 pip 方案
如果你愿意,可以告诉我:
我可以直接给你一条可复制的安装命令。