CentOS 上 PyTorch 安装失败排查与解决
一 准备与基础检查
python3 --version 或 python --versionsudo yum update -ysudo yum install -y gcc-c++ make python3 python3-pip python3-develpython3 -m pip install --upgrade pip二 选择安装方式
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh && bash Miniconda3-latest-Linux-x86_64.shconda create -n torch_env python=3.9conda activate torch_envconda install pytorch torchvision torchaudio cpuonly -c pytorchconda install pytorch torchvision torchaudio cudatoolkit=11.8 -c pytorch -c nvidia(示例为 CUDA 11.8,请与驱动匹配)pip3 install torch torchvision torchaudiopip3 install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu113pip3 install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu116pip3 install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu117pip3 install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu120pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simpleconda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/conda config --set show_channel_urls yes三 常见报错与对应处理
ERROR: Could not find a version… / No matching distribution found
python -m pip install --upgrade pipnvidia-smi 显示的 CUDA 运行时版本为参考,再选择对应的 PyTorch 构建。依赖冲突(包版本不兼容)
conda create -n torch_env python=3.9 或 python3 -m venv pytorch_env && source pytorch_env/bin/activate下载慢或中断(中国大陆常见)
内存不足 MemoryError(构建/解压阶段)
pip3 --no-cache-dir install torch torchvision torchaudio 减少内存占用。权限不足
--user 或切换至有权限的环境。四 安装完成后的验证
import torchprint(torch.__version__)print(torch.cuda.is_available())(GPU 版应返回 True)nvidia-smi 显示的 CUDA 版本与安装包匹配。五 仍未解决请提供以下关键信息
cat /etc/centos-releasepython3 --versionnvidia-smi(若无 GPU 可注明)conda 或 pip