在Ubuntu上升级PyTorch版本的步骤
pip show torch
(pip安装)或conda list torch
(conda安装);nvcc --version
(需与PyTorch版本兼容,如PyTorch 2.1需CUDA 11.7+)。conda activate your_env
(conda)或source your_env/bin/activate
(venv)激活目标环境,避免全局依赖干扰。运行以下命令卸载现有PyTorch及相关组件(避免版本冲突):
pip uninstall torch torchvision torchaudio -y
。
确保pip为最新版本,提升安装稳定性:
pip install --upgrade pip
。
pip install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu117
。pip install torch torchvision torchaudio
。运行Python代码检查版本:
python -c "import torch; print(torch.__version__)"
,输出应为最新版本(如2.1.2)。
运行以下命令卸载现有PyTorch及相关组件:
conda remove pytorch torchvision torchaudio -y
。
conda install pytorch torchvision torchaudio cudatoolkit=11.7 -c pytorch
。cpuonly
通道安装:conda install pytorch torchvision torchaudio cpuonly -c pytorch
。运行conda list torch
,确认torch
版本为最新。
pip install --upgrade --force-reinstall torch torchvision torchaudio
强制覆盖。sudo apt install nvidia-driver-535
(Ubuntu 22.04)安装驱动。pip install torch torchvision torchaudio --extra-index-url https://pypi.tuna.tsinghua.edu.cn/simple --extra-index-url https://download.pytorch.org/whl/cu117
。