在 Linux 上安装 PyTorch,常见方式是使用 pip 或 conda。下面按场景给你最实用的方法。
python3 --version
建议 Python ≥ 3.8
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
如果你是 RTX 30/40 系列,一般用
cu118或cu121
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
CPU:
conda install pytorch torchvision torchaudio cpuonly -c pytorch
GPU(CUDA 11.8):
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 可用 ✅
nvidia-smipip install torch -i https://pypi.tuna.tsinghua.edu.cn/simple
如果你告诉我:
我可以给你精确的一条命令安装方案。