CentOS 上 PyTorch 安装失败的排查与修复指南
一 前置检查与环境准备
sudo yum update -ysudo yum install -y gcc-c++ makesudo yum install -y python3 python3-pip python3-develpython3 -m venv ~/venvs/torch && source ~/venvs/torch/bin/activateconda create -n torch_env python=3.8 后 conda activate torch_envpython -m pip install --upgrade pip setuptools wheel二 选择正确的安装方式
pip install torch torchvision torchaudioconda install pytorch torchvision torchaudio cpuonly -c pytorchpip install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu113pip install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu116pip install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu117pip install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu120conda install pytorch torchvision torchaudio cudatoolkit=11.7 -c pytorch(将 11.7 替换为你的 CUDA 版本)pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple~/.condarc 添加清华通道(pytorch、conda-forge 等)以加速与稳定性提升。三 常见报错与对应修复
sudo yum install -y gcc-c++ make;必要时升级系统或选择 conda 预编译包以减少本地编译。--no-cache-dir 重试:pip install --no-cache-dir torch torchvision torchaudio。--no-cache-dir;或在资源充足的机器上安装;分步骤下载后再安装。四 安装完成后的验证与排错要点
python - <<'PY' import torch print("torch:", torch.__version__) print("cuda available:", torch.cuda.is_available()) print("device count:", torch.cuda.device_count()) if torch.cuda.is_available(): print("current device:", torch.cuda.current_device()) print("device name:", torch.cuda.get_device_name(0)) PYtorch.cuda.is_available() 为 False:
nvidia-smi 正常;