在 Linux 上安装 PyTorch,通常推荐使用 conda 或 pip,并且要根据你是否使用 NVIDIA GPU(CUDA) 来选择对应版本。
下面按常见情况说明:
nvidia-smi
python --version
建议 Python ≥ 3.8
pip install torch torchvision torchaudio
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
conda create -n pytorch python=3.10
conda activate pytorch
conda install pytorch torchvision torchaudio cpuonly -c pytorch
conda install pytorch torchvision torchaudio pytorch-cuda=11.8 -c pytorch -c nvidia
python -c "import torch; print(torch.__version__)"
查看是否能用 GPU:
python -c "import torch; print(torch.cuda.is_available())"
True → GPU 可用False → 当前是 CPU 版或未正确安装驱动使用国内镜像(如清华):
pip install torch torchvision torchaudio -i https://pypi.tuna.tsinghua.edu.cn/simple
Ubuntu 示例:
sudo apt update
sudo apt install python3-pip python3-venv
如果你告诉我:
我可以给你精确的一条命令安装方式。