在 CentOS 上配置 PyTorch,通常分为 CPU 版 和 GPU(CUDA)版 两种情况。下面按常见、稳妥的方式一步步说明(以 CentOS 7 / 8 为例)。
sudo yum update -y
sudo yum install -y gcc gcc-c++ make wget bzip2
CentOS 自带 Python 版本较老,强烈建议用 Conda。
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
bash Miniconda3-latest-Linux-x86_64.sh
按提示一路 yes,最后重启终端或执行:
source ~/.bashrc
conda create -n torch python=3.9 -y
conda activate torch
pip install torch torchvision torchaudio
验证:
import torch
print(torch.__version__)
print(torch.cuda.is_available()) # False
nvidia-smi
查看右上角的 CUDA Version(如 11.8、12.1)
例如 CUDA 11.8:
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118
CUDA 12.1:
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121
验证:
import torch
print(torch.cuda.is_available()) # True
报错:version 'GLIBC_2.17' not found
解决:
conda install pytorch torchvision torchaudio pytorch-cuda=11.8 -c pytorch -c nvidia
sudo yum install -y libgomp
pip install torch -i https://pypi.tuna.tsinghua.edu.cn/simple
| 系统 | 方式 |
|---|---|
| CentOS 7 | Miniconda + PyTorch CPU |
| CentOS 8 | Miniconda + PyTorch CUDA |
| 服务器无网 | 离线 whl 安装 |
如果你告诉我:
我可以给你 精确的一条命令安装方案。