Ubuntu24.04安装CUDA Toolkit和NVIDIA显卡驱动

1,卸载CUDA和显卡驱动的内容(如果没安装过,可以看下一步)

2,切换到命令行界面

将默认目标切换到图形用户界面(重启生效)

sudo systemctl set-default graphical.target      图形用户界面
sudo systemctl set-default multi-user.target     命令行界面

运行中切换目标(实时生效)

sudo systemctl isolate multi-user.target     命令行界面
sudo systemctl isolate graphical.target     图形用户界面

3,禁用nouveau

nouveau是一个开源的显卡驱动,有他在,N卡驱动安装不上

先查看nouveau情况

lsmod | grep nouveau

打开blacklist.conf

sudo vi /etc/modprobe.d/blacklist.conf  

在blacklist.conf末尾添加如下

blacklist nouveau
options nouveau modeset=0

更新和重启

sudo update-initramfs -u

sudo reboot

开机后再查看nouveau情况

lsmod | grep nouveau

4,安装桌面管理工具lightDM(可选)

我用lightDM弄成功了,实际默认的gdm3应该就可以用。

sudo apt install lightdm

其实不安装,应该也可以,Ubuntu24.04默认是gdm工具管理

5,关闭X服务

sudo service gdm stop    #二选一
sudo service lightdm stop #二选一

6,下载cuda和NVIDIA显卡驱动

官网:https://developer.nvidia.com/cuda-toolkit-archive

因为我要用pytorch,所以要选匹配的,我选择12.4.1

具体网址:https://developer.nvidia.com/cuda-12-4-1-download-archive?target_os=Linux&target_arch=x86_64&Distribution=Ubuntu&target_version=22.04&target_type=runfile_local

下载和安装:

下载
wget https://developer.download.nvidia.com/compute/cuda/12.4.1/local_installers/cuda_12.4.1_550.54.15_linux.run下载
给权限
sudo chmod 777 cuda_12.4.1_550.54.15_linux.run权限
安装
sudo ./cuda_12.4.1_550.54.15_linux.run

说明:cuda_12.4.1_550.54.15_linux.run这个包包含了cuda和nvidia显卡驱动

安装完成的文字信息

===========
= Summary =
===========
Driver:   Installed
Toolkit:  Installed in /usr/local/cuda-12.4/
Please make sure that
 -   PATH includes /usr/local/cuda-12.4/bin
 -   LD_LIBRARY_PATH includes /usr/local/cuda-12.4/lib64, or, add /usr/local/cuda-12.4/lib64 to /etc/ld.so.conf and run ldconfig as root

To uninstall the CUDA Toolkit, run cuda-uninstaller in /usr/local/cuda-12.4/bin
To uninstall the NVIDIA Driver, run nvidia-uninstall
Logfile is /var/log/cuda-installer.log

7,测试安装结果

nvidia-smi

8,恢复正常显示

如果一直用gdm3没改动,那么只需要如下即可

sudo service gdm3 start
sudo systemctl enable gdm3
sudo systemctl start gdm3

切换回GDM3
sudo dpkg-reconfigure gdm3

sudo service gdm3 start      
sudo service lightdm start