在 Ubuntu 上安装 PyTorch 通常推荐用 Conda 或 pip,并且要根据你是否有 NVIDIA 显卡(CUDA) 来选择版本。下面分情况说明。
lsb_release -a
nvidia-smi
pip install torch torchvision torchaudio
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118
其他 CUDA 版本可去官网选:
https://pytorch.org/get-started/locally/
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
bash Miniconda3-latest-Linux-x86_64.sh
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 -i https://pypi.tuna.tsinghua.edu.cn/simple
建议用 虚拟环境(venv 或 conda)
如果你告诉我:
我可以给你一条精确的安装命令。