Linux系统更新PyTorch版本的步骤
conda activate your_env_name(conda)或source your_env/bin/activate(venv)激活目标环境,确保操作在正确环境中进行。若之前通过pip安装过PyTorch,建议先卸载旧版本,避免残留文件引发冲突:
pip uninstall torch torchvision torchaudio
确保pip为最新版本,以提升安装稳定性和兼容性:
pip install --upgrade pip
pip install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu117
cu117替换为对应版本号即可。pip install torch torchvision torchaudio
安装完成后,通过Python验证版本:
python -c "import torch; print(torch.__version__)"
若输出最新版本号(如2.1.0),则说明更新成功。
确保conda为最新版本,提升包管理效率:
conda update conda
若之前通过conda安装过PyTorch,建议先卸载旧版本:
conda remove pytorch torchvision torchaudio -c pytorch
conda install pytorch torchvision torchaudio cudatoolkit=11.7 -c pytorch
cudatoolkit=11.7替换为目标版本即可。conda install pytorch torchvision torchaudio cpuonly -c pytorch
通过conda环境查看PyTorch版本,或进入Python环境执行验证命令:
conda list torch  # 查看conda环境中PyTorch版本
python -c "import torch; print(torch.__version__)"  # 验证实际加载版本