ubuntu22.04 安装 CUDA Toolkit 报错(ERROR: Unable to find the kernel source tree for the currently running kernel.)
一、问题
笔者已经根据自己的服务器配置下载了 CUDA Toolkit 的安装包: cuda_12.2.0_535.54.03_linux.run
官网下载地址:https://developer.nvidia.com/cuda-toolkit-archive
执行以下命令安装:sh cuda_12.2.0_535.54.03_linux.run
等待几分钟后,安装失败,结果如下图:
二、原因
根据安装失败提示:Installation failed. See log at /var/log/cuda-installer.log for details
查看 cuda-installer.log 日志文件
vim /var/log/cuda-installer.log
可以看到安装驱动时报错失败:Install of driver component failed. Consult the driver log at /var/log/nvidia-installer.log for more details.
查看 nvidia-installer.log 日志文件
vim /var/log/nvidia-installer.log
报错原因:ERROR: Unable to find the kernel source tree for the currently running kernel. Please make sure you have installed the kernel source files for your kernel and that they are properly configured; on Red Hat Linux systems, for example, be sure you have the 'kernel-source' or 'kernel-devel' RPM installed. If you know the correct kernel source files are installed, you may specify the kernel source path with the '--kernel-source-path' command line option.
参考网友解决方法,应该是linux内核与内核头版本不一致。
查看内核版本:
uname -r
可以看到内核版本为:
5.15.0-71-generic
使用dpkg-query命令检查是否有可用的内核头文件:
dpkg-query -s linux-headers-$(uname -r)
发现未安装:linux-headers-5.15.0-71-generic
三、解决方法
安装相应版本的内核头文件。
1、软件包更新升级
apt-get update
apt-get upgrade
2、使用下面的命令安装匹配的内核头文件
apt-get install linux-headers-$(uname -r)
可以看到失败原因是:E: Package 'linux-headers-5.15.0-71-generic' has no installation candidate
,没有安装候选
因为笔者使用的环境,关闭了linux内核更新,所以它已经过时了,我们要先升级一下linux内核版本。
3、升级linux内核
apt-get upgrade linux-image-generic
可以看到即将更新内核到:5.15.0-79-generic
注意:这里需要将机器重启,否则就会遇到笔者下面的情况
再次安装内核头文件
发现依然是安装
linux-headers-5.15.0-71-generic
失败
查看linux 内核
到这里就懵了,发生了什么?果断重启
4、重启机器
reboot now
查看内核版本和内核头文件
可以看到此时内核版本为:5.15.0-79-generic
内核头文件:linux-headers-5.15.0-79-generic
未安装
5、安装 linux-headers-5.15.0-79-generic
通过以下命令安装linux内核头文件:
apt-get install linux-headers-$(uname -r)
可以看到即将安装 linux-headers-5.15.0-79-generic
再次查看可用的内核头文件
dpkg-query -s linux-headers-$(uname -r)
可以看到已经安装成功!!!
6、安装 CUDA Toolkit
执行以下命令安装CUDA Toolkit:
sh cuda_12.2.0_535.54.03_linux.run
可以看到安装了CUDA Toolkit
和 NVIDIA Driver
执行以下命令,监控和管理 NVIDIA GPU(图形处理器)的状态和性能:
nvidia-smi
至此,问题完全解决,大功告成!!!
注意,笔者所有命令都在root用户权限下执行