在 Ubuntu 上配置 PyTorch 环境,通常推荐使用 conda(或 miniconda) 或 venv + pip。下面给你一份从零开始、最常用、最稳妥的流程。
sudo apt update
sudo apt upgrade -y
sudo apt install -y build-essential cmake git curl wget
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
bash Miniconda3-latest-Linux-x86_64.sh
安装完成后重启终端或执行:
source ~/.bashrc
conda create -n pytorch python=3.10 -y
conda activate pytorch
conda install pytorch torchvision torchaudio cpuonly -c pytorch
先确认显卡驱动:
nvidia-smi
然后安装:
conda install pytorch torchvision torchaudio pytorch-cuda=11.8 -c pytorch -c nvidia
sudo apt install -y python3 python3-venv python3-pip
python3 -m venv pytorch-env
source pytorch-env/bin/activate
pip install torch torchvision torchaudio
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118
python
import torch
print(torch.__version__)
print(torch.cuda.is_available()) # GPU 应为 True
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
pip install jupyter
jupyter notebook
建议使用 conda,避免系统 CUDA 冲突。
✅ 新手 / 科研:Miniconda + PyTorch
✅ 服务器部署:venv + pip
✅ 有 GPU:确认驱动 → 选对应 CUDA 版本
如果你告诉我:
我可以直接给你一条可复制的命令清单。