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/
。