CUDA 安装教程

安装教程主要针对 Linux (Ubuntu 20.04)

一、使用 Docker 镜像(推荐)

打包好的 cuda 镜像
https://hub.docker.com/r/nvidia/cuda
https://catalog.ngc.nvidia.com/orgs/nvidia/containers/cuda/tags
cuda 镜像的 dockerfile 文件
https://gitlab.com/nvidia/container-images/cuda

Three flavors of images are provided:

  • base: Includes the CUDA runtime (cudart)
  • runtime: Builds on the base and includes the CUDA math libraries, and NCCL. A runtime image that also includes cuDNN is available.
  • devel: Builds on the runtime and includes headers, development tools for building CUDA images. These images are particularly useful for multi-stage builds.

cuda 镜像几个版本之间的关系为
base > runtime > devel > cudnn

1
2
3
4
5
6
7
# 拉取镜像
docker pull nvidia/cuda:10.2-devel-ubuntu18.04
# 运行
docker run -itd --name cuda-11.7.1 \ # 名称不能重复
-v ~/${username}/Workspace/Docker/:/service \ # 映射本机路径到容器路径
--gpus '"device=1"' \ # 选择 gpu 0 是第一张卡
nvidia/cuda:11.7.1-cudnn8-devel-ubuntu20.04 bash # 镜像地址 选择shell

推荐使用 vscode docker 插件,运行后就可直接在 vscode 中使用容器了

二、手动安装

1.显卡驱动安装(必须)

驱动下载地址:
https://www.nvidia.com/download/index.aspx?lang=en-us

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 驱动安装
apt-get update
apt-get install nvidia-drivers
# 查看驱动版本
nvidia-smi
# 查看结果
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 515.65.01 Driver Version: 515.65.01 CUDA Version: 11.7 |
|-------------------------------+----------------------+----------------------+
| 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 Tesla T4 On | 00000000:13:00.0 Off | 0 |
| N/A 74C P8 22W / 70W | 2MiB / 15360MiB | 0% Default |
| | | N/A |
+-------------------------------+----------------------+----------------------+

+-----------------------------------------------------------------------------+
| Processes: |
| GPU GI CI PID Type Process name GPU Memory |
| ID ID Usage |
|=============================================================================|
| No running processes found |
+-----------------------------------------------------------------------------+

如果没有类似的结果,说明驱动没安装成功。

其中需要关注的是

  • Driver Version: 515.65.01
    显卡驱动版本
  • CUDA Version: 11.7
    最高支持CUDA版本

cuda 对驱动的要求
Table 3. CUDA Toolkit and Corresponding Driver Versions 中给出 cuda 对应驱动版本,高版本驱动可向下兼容

2.CUDA 安装(必须)

https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html

1
2
3
4
5
6
7
nvcc -V # 查看CUDA版本
# 查看结果
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2022 NVIDIA Corporation
Built on Wed_Jun__8_16:49:14_PDT_2022
Cuda compilation tools, release 11.7, V11.7.99
Build cuda_11.7.r11.7/compiler.31442593_0

3.cuDNN 安装(可选)

https://developer.nvidia.com/rdp/cudnn-download
https://developer.nvidia.com/rdp/cudnn-archive