想要更新cuda11.7,需要的驱动最低版本515.65.01,而我的电脑原驱动为470,需要更新。

1. 卸载原驱动

使用了命令:

sudo apt-get purge nvidia-*
sudo apt-get autoremove

用以下命令检查是否卸载干净:

sudo dpkg --list | grep nvidia-*

发现有ii文件残留,当时没管。

2. 安装新驱动

(1)系统自带安装(出问题)

通过设置中的软件更新-附加驱动,选择了NVIDIA-535版本点击下载,装完重启电脑,好家伙,图形界面没有了,nvidia-smi显示device no found,显然这个驱动装得有问题,我又卸。

通过那个检查命令发现,刚刚的535相关的ii文件也残留了。似乎得把它们删除干净才行。我取残留文件名的开头+*,用apt-get purge命令删了个干净:

sudo apt-get purge libnvidia*
sudo apt-get purge linux-objects-nvidia-470-5.15.0*
sudo apt-get purge linux-signatures-nvidia-5.15.0*

删完图形界面立刻出现了。

(2)cuda安装程序装驱动(成功)

既然已经没有驱动了,是不是可以用cuda安装程序安一个它以来的驱动上去呢?

结果接连报了许多错,根据提示到/var/log/nvidia-installer.log中查看报错信息。

(a)报错: 正在使用显卡

“An NVIDIA kernel module ‘nvidia-drm‘ appears to already be loaded in your kernel...”

参考这位博主的博客,停止使用GPU显示:

——也许是我的图形界面恢复,正占用着显卡的锅。

具体地:

sudo -i
systemctl isolate multi-user.target
modprobe -r nvidia-drm
logout

执行完第二行,我显示屏上的图形界面被关闭了;执行第三行报了个缺失。我没管,继续跑cuda的安装程序。

(b)报错:没有kernel source tree

"ERROR: Unable to find the kernel source tree for the currently running kernel..."

sudo apt-get install linux-headers-`uname -r`

用这一行命令解决了问题,继续跑cuda安装程序。

(c)报错:gcc版本不符合

Warning: Compiler version check failed:

The major and minor number of the compiler used to
compile the kernel:

x86_64-linux-gnu-gcc (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0, GNU ld (GNU Binutils for Ubuntu) 2.38

does not match the compiler used here:

cc (Ubuntu 9.5.0-6ubuntu2) 9.5.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.


It is recommended to set the CC environment variable
to the compiler that was used to compile the kernel.

因此需要更新gcc到需要的版本:

sudo apt install gcc-11
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 40
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 50

然后就不再报错,成功安装了cuda11.7与驱动515.65.01。

3. 配置系统环境,切换cuda

查看软链接:

stat /usr/local/cuda

已链接到新的cuda11.7,但再用nvcc -V显示的是原有的cuda11.3,说明要修改要实现多cuda切换需要改我原本的系统路径:

vim ~/.bashrc

将原本的CUDA_HOME=/usr/local/cuda-11.3改为了cuda 

gpu版本的torch cup版本的torch torch的cuda版本对应_运维

 然后更新配置:

source ~/.bashrc

再用nvcc -V检查,妥了。 

4.下载cudnn

 在官网:cuDNN Archive | NVIDIA Developer

下载了目前最新的用于cuda11.x的cudnn

gpu版本的torch cup版本的torch torch的cuda版本对应_安装程序_02

 解压tar.xz文件并复制对应文件到cuda下:

tar -xf cudnn-linux-x86_64-8.9.2.26_cuda11-archive.tar.xz
sudo cp cudnn-linux-x86_64-8.9.2.26_cuda11-archive/include/cudnn.h /usr/local/cuda-11.7/include
sudo cp cudnn-linux-x86_64-8.9.2.26_cuda11-archive/lib/libcudnn* /usr/local/cuda-11.7/lib64
sudo chmod a+r /usr/local/cuda-11.7/include/cudnn.h /usr/local/cuda-11.7/lib64/libcudnn*

5. 安装pytorch

在官网选择版本:Start Locally | PyTorch

gpu版本的torch cup版本的torch torch的cuda版本对应_运维_03

我用conda命令装完不知道为什么得到的是pytorch1.12.1?用pip命令安装不出错。

检查torch是否安装好:

import torch
print(torch.__version__)
print(torch.cuda.is_available())

版本对、cuda可用即可。