安装cuda环境

操作系统 ubuntu20.04

显卡驱动 / cuda /cuDNN
支持的版本矩阵参考
我选择的版本如下

组件 版本 下载地址参考
显卡驱动 Driver Version: 535.183.01 建议使用图形界面一键安装.(我自己试了试命令行没安装成功)
cuda Cuda compilation tools, release 12.2, V12.2.140 官网引导 下载地址 历史版本
cuDNN 适用于 CUDA 12.x 的 cuDNN 9.2.0 官网引导 下载地址 历史版本

显卡卡驱动与环境配置

安装显卡驱动

桌面版本的在 软件和更新中 选择 535 版本的显卡驱动 进行安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

(base) lqz@lqz-mdz:~$ nvidia-smi
Fri Jun 21 23:20:25 2024
+---------------------------------------------------------------------------------------+
| NVIDIA-SMI 535.183.01 Driver Version: 535.183.01 CUDA Version: 12.2 |
|-----------------------------------------+----------------------+----------------------+
| 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 GTX 1060 6GB Off | 00000000:01:00.0 Off | N/A |
| 21% 36C P8 4W / 130W | 11MiB / 6144MiB | 0% Default |
| | | N/A |
+-----------------------------------------+----------------------+----------------------+

+---------------------------------------------------------------------------------------+
| Processes: |
| GPU GI CI PID Type Process name GPU Memory |
| ID ID Usage |
|=======================================================================================|
| 0 N/A N/A 1058 G /usr/lib/xorg/Xorg 4MiB |
| 0 N/A N/A 23296 G /usr/lib/xorg/Xorg 4MiB |


可以看到支持的cuda版本是12.2

安装cuda

下载12.2的cuda

增加执行权限并执行

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
lqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqk
x CUDA Installer x
x - [ ] Driver x
x [ ] 535.104.05 x
x + [X] CUDA Toolkit 12.2 x
x [X] CUDA Demo Suite 12.2 x
x [X] CUDA Documentation 12.2 x
x - [ ] Kernel Objects x
x [ ] nvidia-fs x
x Options x
x Install x
x x
x x
x x
x x
x x
x x
x x
x x
x x
x x
x x
x x
x Up/Down: Move | Left/Right: Expand | 'Enter': Select | 'A': Advanced options x
mqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqj

安装完成了之后打印如下信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
===========
= Summary =
===========

Driver: Not Selected
Toolkit: Installed in /usr/local/cuda-12.2/

Please make sure that
- PATH includes /usr/local/cuda-12.2/bin
- LD_LIBRARY_PATH includes /usr/local/cuda-12.2/lib64, or, add /usr/local/cuda-12.2/lib64 to /etc/ld.so.conf and run ldconfig as root

To uninstall the CUDA Toolkit, run cuda-uninstaller in /usr/local/cuda-12.2/bin
***WARNING: Incomplete installation! This installation did not install the CUDA Driver. A driver of version at least 535.00 is required for CUDA 12.2 functionality to work.
To install the driver using this installer, run the following command, replacing <CudaInstaller> with the name of this run file:
sudo <CudaInstaller>.run --silent --driver

Logfile is /var/log/cuda-installer.log
1
2
3
4
5
6
7
8
9
10
11
12
13
驱动程序:未选择
工具包:已安装在 /usr/local/cuda-12.2/

请确保:
- PATH 环境变量包含 /usr/local/cuda-12.2/bin
- LD_LIBRARY_PATH 环境变量包含 /usr/local/cuda-12.2/lib64,或者,将 /usr/local/cuda-12.2/lib64 添加到 /etc/ld.so.conf 并作为 root 用户运行 ldconfig

要卸载 CUDA Toolkit,请在 /usr/local/cuda-12.2/bin 中运行 cuda-uninstaller
***警告:安装不完整!此次安装未安装 CUDA 驱动程序。CUDA 12.2 功能正常工作至少需要版本 535.00 的驱动程序。
要使用此安装程序安装驱动程序,请运行以下命令,将 <CudaInstaller> 替换为运行文件的名称:
sudo <CudaInstaller>.run --silent --driver

日志文件位于 /var/log/cuda-installer.log

将PATH 与 LD_LIBRARY_PATH 加入环境变量

1
2
3
4
5
6
7
(base) lqz@lqz-mdz:~$ cat  /etc/profile.d/cuda-Toolkit.sh 

export PATH=/usr/local/cuda-12.2/bin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda-12.2/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}

(base) lqz@lqz-mdz:~$

检查版本号

1
2
3
4
5
6
7
(base) lqz@lqz-mdz:~$ nvcc -V
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2023 NVIDIA Corporation
Built on Tue_Aug_15_22:02:13_PDT_2023
Cuda compilation tools, release 12.2, V12.2.140
Build cuda_12.2.r12.2/compiler.33191640_0
(base) lqz@lqz-mdz:~$

至此 显卡驱动 与 cuda 安装完成

  • 如果要卸载 可以执行
1
/usr/local/cuda-12.5/bin/cuda-uninstaller

安装cuDNN

先决条件

有关操作系统、NVIDIA CUDA、CUDA 驱动程序和 NVIDIA 硬件的最新兼容性软件版本,请参阅 cuDNN 支持矩阵

1
wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/cuda-keyring_1.1-1_all.deb
1
apt-get install zlib1g
1
2
3
4
5
wget https://developer.download.nvidia.com/compute/cudnn/9.2.0/local_installers/cudnn-local-repo-ubuntu2004-9.2.0_1.0-1_amd64.deb
sudo dpkg -i cudnn-local-repo-ubuntu2004-9.2.0_1.0-1_amd64.deb
sudo cp /var/cudnn-local-repo-ubuntu2004-9.2.0/cudnn-*-keyring.gpg /usr/share/keyrings/
sudo apt-get update
sudo apt-get -y install cudnn-cuda-12

检查是否成功

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
26
root@lqz-mdz:/# ls -ld ./usr/include/cudnn*
lrwxrwxrwx 1 root root 29 6月 22 13:45 ./usr/include/cudnn_adv.h -> /etc/alternatives/cudnn_adv_h
lrwxrwxrwx 1 root root 33 6月 22 13:45 ./usr/include/cudnn_backend.h -> /etc/alternatives/cudnn_backend_h
lrwxrwxrwx 1 root root 29 6月 22 13:45 ./usr/include/cudnn_cnn.h -> /etc/alternatives/cudnn_cnn_h
lrwxrwxrwx 1 root root 31 6月 22 13:45 ./usr/include/cudnn_graph.h -> /etc/alternatives/cudnn_graph_h
lrwxrwxrwx 1 root root 26 6月 22 13:45 ./usr/include/cudnn.h -> /etc/alternatives/libcudnn
lrwxrwxrwx 1 root root 29 6月 22 13:45 ./usr/include/cudnn_ops.h -> /etc/alternatives/cudnn_ops_h
lrwxrwxrwx 1 root root 33 6月 22 13:45 ./usr/include/cudnn_version.h -> /etc/alternatives/cudnn_version_h
root@lqz-mdz:/# ls -ld /etc/alternatives/cudnn*
lrwxrwxrwx 1 root root 44 6月 22 13:45 /etc/alternatives/cudnn_adv_h -> /usr/include/x86_64-linux-gnu/cudnn_adv_v9.h
lrwxrwxrwx 1 root root 48 6月 22 13:45 /etc/alternatives/cudnn_backend_h -> /usr/include/x86_64-linux-gnu/cudnn_backend_v9.h
lrwxrwxrwx 1 root root 44 6月 22 13:45 /etc/alternatives/cudnn_cnn_h -> /usr/include/x86_64-linux-gnu/cudnn_cnn_v9.h
lrwxrwxrwx 1 root root 46 6月 22 13:45 /etc/alternatives/cudnn_graph_h -> /usr/include/x86_64-linux-gnu/cudnn_graph_v9.h
lrwxrwxrwx 1 root root 44 6月 22 13:45 /etc/alternatives/cudnn_ops_h -> /usr/include/x86_64-linux-gnu/cudnn_ops_v9.h
lrwxrwxrwx 1 root root 48 6月 22 13:45 /etc/alternatives/cudnn_version_h -> /usr/include/x86_64-linux-gnu/cudnn_version_v9.h
root@lqz-mdz:/# ls -ld /usr/include/x86_64-linux-gnu/cudnn*
-rw-r--r-- 1 root root 30820 6月 2 07:28 /usr/include/x86_64-linux-gnu/cudnn_adv_v9.h
-rw-r--r-- 1 root root 2751 6月 2 07:28 /usr/include/x86_64-linux-gnu/cudnn_backend_v9.h
-rw-r--r-- 1 root root 36699 6月 2 07:28 /usr/include/x86_64-linux-gnu/cudnn_cnn_v9.h
-rw-r--r-- 1 root root 38123 6月 2 07:28 /usr/include/x86_64-linux-gnu/cudnn_graph_v9.h
-rw-r--r-- 1 root root 63633 6月 2 07:28 /usr/include/x86_64-linux-gnu/cudnn_ops_v9.h
-rw-r--r-- 1 root root 2841 6月 2 07:28 /usr/include/x86_64-linux-gnu/cudnn_v9.h
-rw-r--r-- 1 root root 3112 6月 2 07:28 /usr/include/x86_64-linux-gnu/cudnn_version_v9.h
root@lqz-mdz:/#
root@lqz-mdz:/#
root@lqz-mdz:/#

cuda-hello-word

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <stdio.h>

# 调用GPU的函数
__global__ void hello_from_gpu()
{

printf("hello word from the GPU \n");
}

int main(void)
{
# 指定4*4个线程
hello_from_gpu<<<4,4>>>();
# 同步
cudaDeviceSynchronize();
return 0;
}

1
nvcc test.cu -o test
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
root@lqz-mdz:/data/app/cuda# ./test 
hello word from the GPU
hello word from the GPU
hello word from the GPU
hello word from the GPU
hello word from the GPU
hello word from the GPU
hello word from the GPU
hello word from the GPU
hello word from the GPU
hello word from the GPU
hello word from the GPU
hello word from the GPU
hello word from the GPU
hello word from the GPU
hello word from the GPU
hello word from the GPU