Linux(Ubuntu22.04)子系统安装cuda,cudnn

显卡驱动是共享的,不需要安装,但是需要禁用内核更新

  • sudo apt-mark hold linux-image-generic linux-headers-generic 禁止内核更新。

安装cuda

  1. 我的显卡信息
Thu Jan 19 16:23:40 2023
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 517.00       Driver Version: 517.00       CUDA Version: 11.7     |
|-------------------------------+----------------------+----------------------+
| GPU  Name            TCC/WDDM | Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|                               |                      |               MIG M. |
|===============================+======================+======================|
|   0  NVIDIA GeForce ... WDDM  | 00000000:01:00.0 Off |                  N/A |
| N/A   30C    P0    23W /  N/A |      0MiB /  6144MiB |      0%      Default |
|                               |                      |                  N/A |
+-------------------------------+----------------------+----------------------+

+-----------------------------------------------------------------------------+
| Processes:                                                                  |
|  GPU   GI   CI        PID   Type   Process name                  GPU Memory |
|        ID   ID                                                   Usage      |
|=============================================================================|
|  No running processes found                                                 |
+-----------------------------------------------------------------------------+
  1. 下载cuda11.3方便做深度学习,没必要安装高版本的cuda,显卡支持11.7以下的cuda版本,https://developer.nvidia.com/cuda-toolkit-archive
    Linux(Ubuntu22.04)子系统安装cuda,cudnn_第1张图片

  1. sudo apt update
  2. 安装gcc, make sudo apt install gcc``sudo apt install make
  3. 进入windows下载文件目录,swift@LAPTOP-3N6UNIUA:/mnt/d$ cd download/idm/ 或者进入Ubuntu下载cuda11.3目录
  4. sudo ./cuda_11.3.1_465.19.01_linux.run 需要等待好一会

如果报错,查看错误原因
cat /var/log/cuda-installer.log
解决:

[ERROR]: unsupported compiler version: 11.3.0. Use --override to override this check.
  1. 修改为sudo ./cuda_11.3.1_465.19.01_linux.run --override再次等待一会。会一段时间无响应。
  2. 出现界面,输入accept
┌──────────────────────────────────────────────────────────────────────────────┐
│  End User License Agreement                                                  │
│  --------------------------                                                  │
│                                                                              │
│  The CUDA Toolkit End User License Agreement applies to the                  │
│  NVIDIA CUDA Toolkit, the NVIDIA CUDA Samples, the NVIDIA                    │
│  Display Driver, NVIDIA Nsight tools (Visual Studio Edition),                │
│  and the associated documentation on CUDA APIs, programming                  │
│  model and development tools. If you do not agree with the                   │
│  terms and conditions of the license agreement, then do not                  │
│  download or use the software.                                               │
│                                                                              │
│  Last updated: Mar 24, 2021.                                                 │
│                                                                              │
│                                                                              │
│  Preface                                                                     │
│  -------                                                                     │
│                                                                              │
│  The Software License Agreement in Chapter 1 and the Supplement              │
│  in Chapter 2 contain license terms and conditions that govern               │
│  the use of NVIDIA software. By accepting this agreement, you                │
│──────────────────────────────────────────────────────────────────────────────│
│ Do you accept the above EULA? (accept/decline/quit):                         │
│                                                                              │
└──────────────────────────────────────────────────────────────────────────────┘
  1. 显示以下界面,这里和真实的Ubuntu系统安装不同,没有出现驱动选项,直接安装就行。移动光标到install,回车。等待一会。
 CUDA Installer                                                               │
│ + [X] CUDA Toolkit 11.3                                                      │
│   [X] CUDA Samples 11.3                                                      │
│   [X] CUDA Demo Suite 11.3                                                   │
│   [X] CUDA Documentation 11.3                                                │
│   Options                                                                    │
│   Install
  1. 出现以下界面就安装成功了
===========
= Summary =
===========

Driver:   Not Selected
Toolkit:  Installed in /usr/local/cuda-11.3/
Samples:  Installed in /home/swift/, but missing recommended libraries

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

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

Logfile is /var/log/cuda-installer.log
  • 然而此时输入nvcc -V报错
swift@LAPTOP-3N6UNIUA:/mnt/d/download/idm$ nvcc -V
Command 'nvcc' not found, but can be installed with:
sudo apt install nvidia-cuda-toolkit
  1. 需要添加环境变量, vim ~/.bashrc添加到最后。
export PATH=/usr/local/cuda-11.3/bin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda-11.3/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
  1. source ~/.bashrc
  • 之后输入nvcc -V之后就能显示
swift@LAPTOP-3N6UNIUA:/mnt/d/download/idm$ nvcc -V
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2021 NVIDIA Corporation
Built on Mon_May__3_19:15:13_PDT_2021
Cuda compilation tools, release 11.3, V11.3.109
Build cuda_11.3.r11.3/compiler.29920130_0

安装cudnn

  1. 下载cudnn和cuda版本配套,选择11.x https://developer.nvidia.com/rdp/cudnn-archive
  2. 下载得到文件cudnn-linux-x86_64-8.6.0.163_cuda11-archive.tar.xz
  3. 解压tar -xvf cudnn-linux-x86_64-8.6.0.163_cuda11-archive.tar.xz
  4. 进入cudnn-linux-x86_64-8.6.0.163_cuda11-archive目录(cd cudnn-linux-x86_64-8.6.0.163_cuda11-archive)解压有点慢。
sudo cp ./lib/* /usr/local/cuda-11.3/lib64/
sudo cp ./include/* /usr/local/cuda-11.3/include/
sudo chmod a+r /usr/local/cuda/include/cudnn*.h /usr/local/cuda/lib64/libcudnn*
  1. cudnn环境变量设置vim ~/.bashrc添加下面的内容
export LD_LIBRARY_PATH=/usr/local/cuda-11.3/targets/x86_64-linux/lib${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
  1. source ~/.bashrc
  2. 有两种命令:如果你的cudnn版本较旧,就用第一条。否则就用第二条。
cat /usr/local/cuda/include/cudnn.h | grep CUDNN_MAJOR -A 2
cat /usr/local/cuda/include/cudnn_version.h | grep CUDNN_MAJOR -A 2

多版本cuda和cudnn切换

  • vim ~/.bashrc修改cuda-11.3的版本号
export PATH=/usr/local/cuda-11.3/bin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda-11.3/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda-11.3/targets/x86_64-linux/lib${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
  • source ~/.bashrc

你可能感兴趣的:(Ubuntu相关,linux,深度学习,人工智能)