在 Ubuntu 上安装 PyTorch,通常按以下步骤进行。下面以**最常见场景(有 NVIDIA 显卡 + CUDA)和无显卡(CPU)**两种情况说明。
lsb_release -a
nvidia-smi
Ubuntu 一般自带 Python3:
python3 --version
pip3 --version
如果没有 pip:
sudo apt update
sudo apt install python3-pip python3-venv
python3 -m venv pytorch-env
source pytorch-env/bin/activate
激活后,命令行前面会出现 (pytorch-env)
去官网选命令(示例为 CUDA 11.8):
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118
⚠️ 注意:
- 先装好 NVIDIA 驱动
- CUDA 版本要和驱动匹配
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu
python
import torch
print(torch.__version__)
print(torch.cuda.is_available()) # GPU 应为 True
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple torch
conda install pytorch torchvision torchaudio pytorch-cuda=11.8 -c pytorch -c nvidia
如果你愿意,可以告诉我:
我可以给你精确的一条命令。