> ## Content Index
> Fetch the complete content index at: https://yinguobing.com/blog/llms.txt
> Use this file to discover other available public pages before exploring further.

# 以Runfile的方式安装CUDA 11
- URL: https://yinguobing.com/blog/install-cuda11-with-runfile/
- Published: 2022-03-05T07:32:57.000Z
- Updated: 2022-05-29T04:52:00.000Z
- Description: 在Ubuntu Server 20.04上安装CUDA 11。
- Author: 尹国冰
- Tags: 新手入门

NVIDIA提供了两种CUDA安装方式。一种使用包管理器；另一种使用Runfile。这篇文章记录了以Runfile形式安装CUDA 11的过程。

### 环境信息

- 操作系统：Ubuntu Server 20.04.4 LTS
- 显卡：RTX 3090

### 准备工作

在正式开始前，请确认以下步骤。

#### 确认GPU已正确安装

```bash
lspci | grep -i nvidia
```

输出类似：

```bsh
01:00.0 VGA compatible controller: NVIDIA Corporation Device 2204 (rev a1)

```

#### 清理环境

Runfile安装方式与包管理器安装方式冲突，所以需要卸载系统中已有的Nvidia组件。如果你确定系统中不包含类似组件，可以跳过此步。

```bash
sudo apt-get --purge remove "*cublas*" "*cufft*" "*curand*" \
 "*cusolver*" "*cusparse*" "*npp*" "*nvjpeg*" "cuda*" "nsight*" 
sudo apt-get --purge remove "*nvidia*"
sudo apt-get autoremove
```

#### 确认GCC已安装

```bash
sudo apt install gcc, make
```

#### 安装内核头文件

```bash
sudo apt-get install linux-headers-$(uname -r)
```

#### 下载安装文件

我当前安装的CUDA版本为11.3。请根据实际安装版本替换。

```bash
wget https://developer.download.nvidia.com/compute/cuda/11.3.1/local_installers/cuda_11.3.1_465.19.01_linux.run
```

#### 禁用社区驱动

使用如下命令，观察是否有任何输出。

```bash
lsmod | grep nouveau
```

如果有任何输出，则需要先禁用社区驱动。如果没有任何输出，跳过此步骤。

建立文件 `/etc/modprobe.d/blacklist-nouveau.conf` ，并在其中写入如下内容：

```bash
blacklist nouveau
options nouveau modeset=0
```

之后更新内核文件。

```bash
sudo update-initramfs -u
```

并重新启动计算机。

### 安装

准备工作完成后，正式开始安装。

```bash
sudo sh cuda_11.3.1_465.19.01_linux.run
```

首先弹出的是使用协议，输入 `accept` 确认。

![](https://yinguobing.com/blog/content/images/2022/03/cuda-01.webp)

之后定制安装组件。方向键定位，空格键切换勾选。

![](https://yinguobing.com/blog/content/images/2022/03/cuda-02.webp)

选择完成后点击 `Install` 开始安装。

稍等片刻后安装完成，并显示安装总结。

![](https://yinguobing.com/blog/content/images/2022/03/cuda-03.webp)

### 安装后操作

虽然CUDA所需的文件已经安装完成，但是要想顺畅的使用还需要执行一些善后操作。

#### 更新可执行文件路径

```bash
export PATH=/usr/local/cuda-11.3/bin${PATH:+:${PATH}}
```

以便执行 `nvcc` 等程序。

#### 更新动态链接库路径

```bash
export LD_LIBRARY_PATH=/usr/local/cuda-11.3/lib64\
                         ${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
```

以避免找不到动态链接库错误。

### 验证

最后，重新启动计算机，并验证是否安装成功。

```bash
nvidia-smi
Sat Mar  5 07:24:39 2022       
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 465.19.01    Driver Version: 465.19.01    CUDA Version: 11.3     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|                               |                      |               MIG M. |
|===============================+======================+======================|
|   0  NVIDIA GeForce ...  Off  | 00000000:01:00.0 Off |                  N/A |
|  0%   36C    P0     1W / 350W |      0MiB / 24265MiB |      0%      Default |
|                               |                      |                  N/A |
+-------------------------------+----------------------+----------------------+
                                                                               
+-----------------------------------------------------------------------------+
| Processes:                                                                  |
|  GPU   GI   CI        PID   Type   Process name                  GPU Memory |
|        ID   ID                                                   Usage      |
|=============================================================================|
|  No running processes found                                                 |
+-----------------------------------------------------------------------------+
```

接下来就可以安装cuDNN深度学习库了。