Ubuntu系統更新PyTorch到最新版的步驟
sudo apt update && sudo apt upgrade -y
,確保系統軟件包為最新版本,避免依賴沖突。pip3 install --upgrade pip
升級pip。conda activate your_env
或source venv/bin/activate
),避免全局安裝導致版本混亂。pip是PyTorch官方推薦的安裝方式,能快速獲取最新版本。
pip3 uninstall torch torchvision torchaudio
,徹底移除舊版本,避免殘留文件導致沖突。pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121
pip3 install torch torchvision torchaudio
注:CUDA版本需與系統安裝的驅動兼容(如CUDA 12.1需驅動版本≥520.61.05),可通過
nvcc --version
查看當前CUDA版本。
import torch
print(torch.__version__) # 輸出最新版本號(如2.1.0)
print(torch.cuda.is_available()) # GPU版本應返回True
conda能自動解決依賴關系,適合新手或需要管理多個環境的情況。
conda update conda
,確保conda為最新版。conda remove pytorch torchvision torchaudio cudatoolkit
,徹底移除舊版本。conda install pytorch torchvision torchaudio cudatoolkit=12.1 -c pytorch -c nvidia
cudatoolkit
參數:conda install pytorch torchvision torchaudio -c pytorch
conda list torch
,查看PyTorch版本是否為最新;或在Python中執行上述驗證代碼。torch.cuda.is_available()
返回False的問題。pip3 config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
。