Ubuntu 20.04/22.04安装NVIDIA显卡驱动
Ubuntu 20.04/22.04安装NVIDIA显卡驱动
一、驱动安装方案对比
安装方式 | 优点 | 缺点 | 推荐指数 |
---|---|---|---|
官方.run文件 | 版本灵活可控 | 兼容性问题较多 | ⭐⭐ |
附加驱动工具 | 一键安装无脑操作 | 版本更新滞后 | ⭐⭐⭐⭐ |
PPA源安装 | 版本较新自动更新 | 需要添加第三方源 | ⭐⭐⭐ |
建议选择: 普通用户优先使用附加驱动工具
,深度学习/开发者推荐PPA源安装
二、手动安装NVIDIA驱动全流程
1. 驱动下载与硬件识别
1.1 查看显卡型号
lspci | grep -i nvidia
# 示例输出:01:00.0 VGA compatible controller: NVIDIA Corporation GA104 [GeForce RTX 3070]
1.2 官网下载驱动
访问NVIDIA驱动下载页
选择对应参数:
- 产品类型:GeForce
- 系列:RTX 40 Series(以RTX 4070 Ti SUPER为例)
- 操作系统:Linux 64-bit
2. 安装前关键准备
2.1 安装编译依赖
sudo apt update && sudo apt full-upgrade -y
sudo apt install gcc make g++
2.2 彻底卸载旧驱动
sudo apt purge *nvidia* *cuda* -y
sudo apt autoremove -y
2.3 禁用nouveau驱动
sudo gedit /etc/modprobe.d/blacklist.conf
# 添加以下内容:
blacklist nouveau
options nouveau modeset=0
更新并验证:
sudo update-initramfs -u
reboot
lsmod | grep nouveau # 无输出表示禁用成功
2.4 BIOS设置(必须)
- 开机按
F2/Del
进入BIOS - 关闭
Secure Boot
- 保存设置重启
3. 安装过程(文本模式)
3.1 进入CLI环境
sudo telinit 3
3.2 停止显示服务
sudo service gdm3 stop
3.3 执行安装(关键参数)
cd {你的NVIDIA安装文件路径}
sudo chmod 777 xxxxx.run #给你下载的驱动赋予可执行权限,才可以安装
sudo ./xxxxxxx.run --no-opengl-files #安装
1.The distribution-provided pre-install script failed! Are you sure you want to continue?
continue installation
2.Would you like to register the kernel module souces with DKMS? This will allow DKMS to automatically build a new module, if you install a different kernel later?
No
3.Nvidia's 32-bit compatibility libraries?
No
4.Would you like to run the nvidia-xconfigutility to automatically update your x configuration so that the NVIDIA x driver will be used when you restart x? Any pre-existing x confile will be backed up.
No
参数说明:
--ui=none
:无交互安装--no-questions
:自动确认所有提示--install-libglvnd
:避免OpenGL库冲突
4. 安装后验证
4.1 基础验证
nvidia-smi
# 预期输出:
# +---------------------------------------------------------------------------------------+
# | NVIDIA-SMI 535.154.05 Driver Version: 535.154.05 CUDA Version: 12.2 |
# |-----------------------------------------+----------------------+----------------------+
4.2 图形界面恢复 end
sudo service gdm3 start #重启显示服务,完成
4.3 性能测试
sudo apt install glmark2 -y
glmark2 --fullscreen
# 正常应显示3D渲染场景且无卡顿
三、常见问题解决方案
1. 循环登录问题
现象: 输入密码后闪退回登录界面
修复:
sudo apt install xserver-xorg-video-nouveau --reinstall -y
sudo reboot
2. 分辨率异常
修复:
sudo nano /etc/default/grub
# 修改GRUB_CMDLINE_LINUX_DEFAULT="quiet splash nomodeset"
sudo update-grub
sudo reboot
3. CUDA兼容性错误
解决方案:
sudo apt install nvidia-cuda-toolkit
sudo ldconfig /usr/local/cuda/lib64
四、进阶配置(可选)
1. 多GPU切换(笔记本专用)
sudo prime-select nvidia # 使用NVIDIA显卡
sudo prime-select intel # 切换回集成显卡
sudo reboot
2. 开启性能模式
sudo nvidia-smi -pm 1 # 持久模式
sudo nvidia-smi -pl 250 # 设置功耗墙250W(需硬件支持)
五、版本管理建议
推荐使用dkms
管理驱动版本:
sudo dkms install -m nvidia -v 535.154.05
# 查看已安装版本
dkms status | grep nvidia
实测环境:
- 硬件:RTX 4070 Ti SUPER + i5-12600K
- 系统:Ubuntu 20.04 LTS