在 Ubuntu 上配置 PyTorch,一般按下面步骤来,适合大多数情况(CPU / NVIDIA GPU)。
# 安装 Miniconda(如已安装可跳过)
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
bash Miniconda3-latest-Linux-x86_64.sh
创建环境:
conda create -n torch python=3.10
conda activate torch
sudo apt update
sudo apt install python3-venv python3-pip
python3 -m venv torch_env
source torch_env/bin/activate
pip install torch torchvision torchaudio
先确认显卡驱动:
nvidia-smi
然后到官网选择对应命令(示例 CUDA 12.1):
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121
常见 CUDA 版本:cu118 / cu121 / cu124
import torch
print(torch.__version__)
print(torch.cuda.is_available()) # GPU 应为 True
nvidia-smi 正常cuXXX 安装命令pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
pip install jupyter
jupyter notebook
import torch
x = torch.randn(3, 3)
print(x)
print("CUDA:", torch.cuda.is_available())
如果你告诉我:
我可以给你精确的一条安装命令。