梦中桃源 發表於 2025-6-10 00:00:00

Ubuntu安装NVIDIA显卡驱动、CUDA以及CuDNN工具的详细教程

<div id="navCategory"><h5 class="catalogue">目录</h5><ul class="first_class_ul"><li>一、简介</li><li>二、查看显卡设备</li><li>三、安装显卡驱动</li><li>四、安装CUDA工具箱</li><li>五、安装CuDNN</li><li>小结</li><li>Ubuntu下如何查看显卡及显卡驱动<ul class="second_class_ul"><li>使用nvidia-smi&nbsp;工具查看</li><li>安装显卡前查看推荐的显卡</li></ul></li></ul></div><p class="maodian"></p><h2>一、简介</h2>
<ul><li>NVIDIA 驱动:操作系统与 NVIDIA 显卡硬件之间的桥梁,负责<strong>驱动显卡</strong>硬件的运行,显卡的&ldquo;<code>底层操作系统</code>&rdquo;,一切的基础。</li><li>CUDA(Compute Unified Device Architecture):NVIDIA 提供的一种<strong>并行计算</strong>平台和编程模型,允许开发者使用 NVIDIA GPU 进行通用计算,显卡的&ldquo;<code>开发工具箱</code>&rdquo;,允许写高性能并行程序。</li><li>cuDNN(CUDA Deep Neural Network):NVIDIA 提供的一个专为<strong>深度学习</strong>优化的&nbsp;<strong>GPU</strong>&nbsp;加速库,用于加速深度神经网络的训练和推理,专为深度学习优化的&ldquo;<code>特定工具包</code>&rdquo;,cuDNN 是 TensorFlow 和 PyTorch 的关键依赖。</li><li>对于深度学习应用,三者通常需要搭配使用,正确的版本匹配非常重要!版本匹配非常重要!非常重要!</li></ul>
<p class="maodian"></p><h2>二、查看显卡设备</h2>
<p>查看系统中所有视频设备(显卡)的详细硬件信息。</p>
<div class="dxycode"><pre class="brush:bash;">sudo lshw -c video</pre></div>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="显示设备信息" src="https://zhuji.jb51.net/uploads/allimg/20250606/2-2506061H225K6.png" /></p>
<p>如果没有显示显卡的具体型号,很有可能是<code>lshw</code>数据库不完整,<code>lshw</code>的硬件信息基于系统的设备数据库(通常是由 /usr/share/hwdata/pci.ids 提供)。如果设备的<code>PCI ID</code>未包含在数据库中,则只会显示厂商名称(如 &ldquo;NVIDIA Corporation&rdquo;),而不会显示具体型号。尝试更新硬件数据库。</p>
<div class="dxycode"><pre class="brush:bash;">sudo update-pciids   # 从 pci-ids.ucw.cz 更新到最新的PCI ID数据库</pre></div>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="更新数据库" src="https://zhuji.jb51.net/uploads/allimg/20250606/2-2506061H225254.png" /></p>
<p>同样,可以使用<code>ls</code>指令查看系统中所有的 DRM(Direct Rendering Manager)设备。每个<code>cardX</code>表示一个显卡设备。</p>
<div class="dxycode"><pre class="brush:bash;">ls /dev/dri</pre></div>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="卡设备" src="https://zhuji.jb51.net/uploads/allimg/20250606/2-2506061H22NY.png" /></p>
<p>查看当前系统的显卡及驱动绑定情况</p>
<div class="dxycode"><pre class="brush:bash;">lspci -k | grep -A 2 -i "VGA"</pre></div>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="显卡驱动绑定" src="https://zhuji.jb51.net/uploads/allimg/20250606/2-2506061H22HI.png" /></p>
<p>开源的<code>Nouveau</code>显卡驱动程序通常会在安装系统的时候自动加载,安装NVIDIA驱动之前,需要确保<code>Nouveau</code>驱动被禁用,避免发生冲突。</p>
<div class="dxycode"><pre class="brush:bash;">sudo vim /etc/default/grub
将---&gt; GRUB_CMDLINE_LINUX_DEFAULT="quiet splash"
替换为---&gt; GRUB_CMDLINE_LINUX_DEFAULT="quiet splash modprobe.blacklist=nouveau"
保存后更新---&gt; sudo update-grub
更新后重启系统---&gt; sudo reboot</pre></div>
<p class="maodian"></p><h2>三、安装显卡驱动</h2>
<p>更新软件包索引</p>
<div class="dxycode"><pre class="brush:bash;">sudo apt update</pre></div>
<p>检测推荐的驱动(不仅仅是N卡驱动)</p>
<div class="dxycode"><pre class="brush:bash;">ubuntu-drivers devices</pre></div>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="查找合适驱动" src="https://zhuji.jb51.net/uploads/allimg/20250606/2-2506061H22J61.png" /></p>
<p>安装特定版本的驱动(一般选择带有recommended的驱动版本),高版本的驱动有的时候不利于一些旧设备的使用,cuda以及cudnn的版本相应也需要提高,安装的时候可以根据设备情况选择稍低版本的驱动。</p>
<div class="dxycode"><pre class="brush:bash;">sudo apt install nvidia-driver-535</pre></div>
<p>在尝试不同版本的驱动的时候,需要将已安装的驱动卸载掉并清理相关的配置文件。</p>
<div class="dxycode"><pre class="brush:bash;">sudo apt remove --purge nvidia-driver-535
sudo apt autoremove --purge
sudo apt autoclean</pre></div>
<p>显卡驱动安装完成后,可以通过<code>nvidia-smi</code> 指令查看显卡信息,<code>nvidia-smi -l 2</code>可以每两秒刷新一下显卡状态,输出类似的显卡信息。</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="显卡信息" src="https://zhuji.jb51.net/uploads/allimg/20250606/2-2506061H22OR.png" /></p>
<p class="maodian"></p><h2>四、安装CUDA工具箱</h2>
<p>使用如下指令,查看ubuntu系统信息。</p>
<div class="dxycode"><pre class="brush:bash;">lsb_release -a
# 或者
cat /etc/os-release</pre></div>
<p>登录英伟达,根据驱动支持的最高CUDA版本,选择一个稍低的版本进行安装,这里显卡驱动最高支持到 <strong>12.2</strong>,选择 <strong>12.0.1</strong> 进行安装。之后选择与自己主机对应的系统、架构以及发行版,并选择<code>runfile(local)</code>进行安装。</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="版本选择" src="https://zhuji.jb51.net/uploads/allimg/20250606/2-2506061H22V09.png" /></p>
<p>选择好之后,紧接着下面会提供具体的安装指令,按照指令下载归档文件,并执行安装程序。</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="安装指令" src="https://zhuji.jb51.net/uploads/allimg/20250606/2-2506061H22Y21.png" /></p>
<p>弹出条款信息,接受即可。</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="接受许可" src="https://zhuji.jb51.net/uploads/allimg/20250606/2-2506061H22V15.png" /></p>
<p>紧接着,需要取消显卡驱动的安装,因为我们之前已经安装过。最后选择安装。</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="安装" src="https://zhuji.jb51.net/uploads/allimg/20250606/2-2506061H22W30.png" /></p>
<p>安装完成后,终端会有如下信息打印,内容不会完全相同,仅参考。</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="终端打印" src="https://zhuji.jb51.net/uploads/allimg/20250606/2-2506061H229112.png" /></p>
<p>根据终端提示,我们可查看<code>/usr/local/cuda-12.0/</code>目录下是否存在指定文件。确认存在后,根据终端提示的<code>PATH</code>和<code>LD_LIBRARY_PATH</code>信息,更新环境变量。使用指令 <code>vim ~/.bashrc</code>打开文件,并在末尾追加下面的环境变量。保存文件之后,使用指令<code>source ~/.bashrc</code>使配置生效。</p>
<div class="dxycode"><pre class="brush:bash;">export PATH="/usr/local/cuda-12.0/bin:$PATH"
export LD_LIBRARY_PATH="/usr/local/cuda-12.0/lib64:$LD_LIBRARY_PATH" </pre></div>
<p>配置完环境变量并激活后,使用指令<code>nvcc -V</code>,如果有如下输出则<strong>CUDA</strong>安装成功。</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="输出" src="https://zhuji.jb51.net/uploads/allimg/20250606/2-2506061H2291a.png" /></p>
<p class="maodian"></p><h2>五、安装CuDNN</h2>
<p>安装<strong>CUDA</strong>之后,进入,选择合适的版本下载压缩包。</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="cudnn下载" src="https://zhuji.jb51.net/uploads/allimg/20250606/2-2506061H2293b.png" /></p>
<p>解压之后,分别进入 <strong>include</strong> 以及 <strong>lib</strong> 目录中,将如下文件拷贝到之前 <strong>CUDA</strong> 的安装目录下。</p>
<div class="dxycode"><pre class="brush:bash;">sudo cp (你的目录)/include/cudnn.h /usr/local/cuda-12.0/include
sudo cp (你的目录)/lib/libcudnn* /usr/local/cuda-12.0/lib64
sudo chmod a+r /usr/local/cuda-12.0/include/cudnn.h
sudo chmod a+r /usr/local/cuda-12.0/lib64/libcudnn*</pre></div>
<p>验证<strong>CuDNN</strong>是否安装成功,可以结合<strong>Pytorch</strong>查看,输出如下信息则证明安装成功。</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="验证cudnn" src="https://zhuji.jb51.net/uploads/allimg/20250606/2-2506061H229144.png" /></p>
<p class="maodian"></p><h2>小结</h2>
<p>以上安装流程在ubuntu-22.04上,基于驱动版本535、cuda版本12.0、cudnn版本8.9测试安装。</p>
<p class="maodian"></p><h2>Ubuntu下如何查看显卡及显卡驱动</h2>
<p class="maodian"></p><h3>使用nvidia-smi&nbsp;工具查看</h3>
<p>查看显卡型号<code>nvida-smi -L</code></p>
<div class="dxycode"><pre class="brush:bash;">$ nvidia-smi -L
GPU 0: NVIDIA GeForce RTX 3050 4GB Laptop GPU (UUID: GPU-4cf7b7cb-f103-bf56-2d59-304f8996e28c)</pre></div>
<p>当然直接使用<code>nvida-smi</code>&nbsp;命令可以查看更多信息</p>
<div class="dxycode"><pre class="brush:bash;">$ nvidia-smi
Mon Feb 12 18:24:15 2024      
+---------------------------------------------------------------------------------------+
| NVIDIA-SMI 535.154.05             Driver Version: 535.154.05   CUDA Version: 12.2   |
|-----------------------------------------+----------------------+----------------------+
| GPUName               Persistence-M | Bus-Id      Disp.A | Volatile Uncorr. ECC |
| FanTemp   Perf          Pwr:Usage/Cap |         Memory-Usage | GPU-UtilCompute M. |
|                                       |                      |               MIG M. |
|=========================================+======================+======================|
|   0NVIDIA GeForce RTX 3050 ...    Off | 00000000:02:00.0 Off |                  N/A |
| N/A   47C    P8               3W /40W |    214MiB /4096MiB |      0%      Default |
|                                       |                      |                  N/A |
+-----------------------------------------+----------------------+----------------------+
+---------------------------------------------------------------------------------------+
| Processes:                                                                            |
|GPU   GI   CI      PID   Type   Process name                            GPU Memory |
|      ID   ID                                                             Usage      |
|=======================================================================================|
|    0   N/AN/A      1854      G   /usr/lib/xorg/Xorg                        161MiB |
|    0   N/AN/A      2225      G   /usr/bin/gnome-shell                         40MiB |
|    0   N/AN/A      3199      G   ...irefox/2987/usr/lib/firefox/firefox      2MiB |
|    0   N/AN/A      9612      G   gnome-control-center                        1MiB |
+---------------------------------------------------------------------------------------+</pre></div>
<blockquote><p>注意,上图中的&nbsp;CUDA&nbsp;Version 后面写了一个版本号。该版本号并不是你已经安装了该版本的 CUDA 的意思,而是说此显卡最大支持的CUDA版本号。因此我们仍然需要手动从官网下载CUDA,且版本号不能高于这个。</p></blockquote>
<p>还可以使用如下命令,查看显卡驱动版本</p>
<div class="dxycode"><pre class="brush:bash;">$ cat /proc/driver/nvidia/version
NVRM version: NVIDIA UNIX x86_64 Kernel Module535.154.05Thu Dec 28 15:37:48 UTC 2023
GCC version:gcc version 12.3.0 (Ubuntu 12.3.0-1ubuntu1~22.04) </pre></div>
<p>也可以使用<code>navidia-settings</code>命令弹出图形界面</p>
<div class="dxycode"><pre class="brush:bash;">$ nvidia-settings</pre></div>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="在这里插入图片描述" src="https://zhuji.jb51.net/uploads/allimg/20250606/2-2506061H230641.png" /></p>
<p class="maodian"></p><h3>安装显卡前查看推荐的显卡</h3>
<p>当然,如果没安装显卡驱动,则需要安装驱动,可使用如下命令</p>
<p>首先我们需要看看显卡硬件有没正确安装到计算机,我们可以通过命令<code>lspci</code>查看一下</p>
<div class="dxycode"><pre class="brush:bash;">$ lspci
00:00.0 Host bridge: Intel Corporation Device a706
00:02.0 VGA compatible controller: Intel Corporation Device a7a0 (rev 04)
00:04.0 Signal processing controller: Intel Corporation Device a71d
00:06.0 PCI bridge: Intel Corporation Device a74d
00:06.2 PCI bridge: Intel Corporation Device a73d
00:07.0 PCI bridge: Intel Corporation Device a73f
00:08.0 System peripheral: Intel Corporation Device a74f
00:0d.0 USB controller: Intel Corporation Device a71e
00:0d.2 USB controller: Intel Corporation Device a73e
00:14.0 USB controller: Intel Corporation Alder Lake PCH USB 3.2 xHCI Host Controller (rev 01)
00:14.2 RAM memory: Intel Corporation Alder Lake PCH Shared SRAM (rev 01)
00:14.3 Network controller: Intel Corporation Device 51f1 (rev 01)
00:15.0 Serial bus controller: Intel Corporation Alder Lake PCH Serial IO I2C Controller #0 (rev 01)
00:16.0 Communication controller: Intel Corporation Alder Lake PCH HECI Controller (rev 01)
00:16.3 Serial controller: Intel Corporation Device 51e3 (rev 01)
00:1f.0 ISA bridge: Intel Corporation Device 519d (rev 01)
00:1f.3 Multimedia audio controller: Intel Corporation Device 51ca (rev 01)
00:1f.4 SMBus: Intel Corporation Alder Lake PCH-P SMBus Host Controller (rev 01)
00:1f.5 Serial bus controller: Intel Corporation Alder Lake-P PCH SPI Controller (rev 01)
01:00.0 Non-Volatile memory controller: KIOXIA Corporation Device 0010 (rev 01)
02:00.0 3D controller: NVIDIA Corporation Device 25ab (rev a1)</pre></div>
<p>使用<code>ubuntu-drivers devices</code>命令,查看所有可用的即推荐的显卡驱动</p>
<div class="dxycode"><pre class="brush:bash;">$ ubuntu-drivers devices
== /sys/devices/pci0000:00/0000:00:06.2/0000:02:00.0 ==
modalias : pci:v000010DEd000025ABsv000017AAsd000050D4bc03sc02i00
vendor   : NVIDIA Corporation
driver   : nvidia-driver-535-open - distro non-free
driver   : nvidia-driver-535-server-open - distro non-free
driver   : nvidia-driver-525-server - distro non-free
driver   : nvidia-driver-535 - distro non-free recommended
driver   : nvidia-driver-525 - distro non-free
driver   : nvidia-driver-535-server - distro non-free
driver   : nvidia-driver-525-open - distro non-free
driver   : nvidia-driver-545-open - distro non-free
driver   : nvidia-driver-545 - distro non-free
driver   : xserver-xorg-video-nouveau - distro free builtin</pre></div>
<p>nvidia-driver-535 - distro non-free recommended 这个即推荐的</p>
<p>使用sudo ubuntu-drivers autoinstall可直接安装推荐的显卡</p>
<p>也可使用sudo apt-get install nvidia-driver-525 制定版本安装</p>
<p>用nvidia-detector 命令,可查看支持的最高版本驱动</p>
<div class="dxycode"><pre class="brush:bash;">$ nvidia-detector
nvidia-driver-545</pre></div>
<p>以上就是Ubuntu 安装 NVIDIA显卡驱动、CUDA 以及 CuDNN工具的详细内容,更多相关资料请阅读琼殿技术社区其它文章!</p>
頁: [1]
查看完整版本: Ubuntu安装NVIDIA显卡驱动、CUDA以及CuDNN工具的详细教程