一 环境准备与系统检查
sudo yum update -ysudo yum groupinstall -y "Development Tools" 与 sudo yum install -y python3 python3-pip python3-develpython3 -m venv ~/venvs/pytorch && source ~/venvs/pytorch/bin/activatewget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh && bash Miniconda3-latest-Linux-x86_64.sh,重启终端后 conda create -n pytorch python=3.10 && conda activate pytorchnvidia-smi(右上显示 Supported/Runtime CUDA)nvcc --versionnvidia-smi 显示的 CUDA 运行时版本为准选择对应的 PyTorch CUDA 版本。二 安装 PyTorch
pip install torch torchvision torchaudioconda install pytorch torchvision torchaudio cpuonly -c pytorchpip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118pip install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu117conda install pytorch torchvision torchaudio cudatoolkit=12.1 -c pytorchpip 安装缓慢,可临时使用国内镜像源(如清华)加速:pip install torch torchvision torchaudio -i https://pypi.tuna.tsinghua.edu.cn/simple(镜像可能滞后,GPU 版本优先使用官方索引)。三 安装验证与常见问题
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)) PYcuda available: True 且能打印设备名。nvcc: command not found:未安装 CUDA Toolkit 或环境变量未配置;GPU 用户建议安装与驱动匹配的 CUDA Toolkit,或直接使用 PyTorch 官方预编译包(自带所需库,无需本地 nvcc 参与运行)。numpy 与 torch 版本不匹配:可尝试 pip install "numpy<2.0" 后再验证。--index-url https://download.pytorch.org/whl/cuXXX,必要时恢复官方源。四 附 常用命令清单
deactivate / source ~/venvs/pytorch/bin/activate 或 conda deactivate / conda activate pytorchpython -c "import torch; print(torch.__version__)"nvidia-sminvcc --versionpython - <<'PY' import torch print("torch:", torch.__version__, "cuda:", torch.cuda.is_available()) PY