Ubuntu 20.04LTS系统安装CUDA11.8、cuDNN,可进行CUDA版本切换
- 1. 更改为清华源并更新软件列表和依赖项
- 2. 安装CUDA
- 3. 安装cuDNN
- 4. CUDA版本切换
1. 更改为清华源并更新软件列表和依赖项
https://mirrors.tuna.tsinghua.edu.cn/help/ubuntu/
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse
# # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse
deb http://security.ubuntu.com/ubuntu/ focal-security main restricted universe multiverse
# deb-src http://security.ubuntu.com/ubuntu/ focal-security main restricted universe multiverse
更新软件列表和必要的依赖项
sudo apt-get update # 更新软件列表
sudo apt-get install g++ # 下载g++编译器
sudo apt-get install gcc # 下载gcc编译器
sudo apt-get install make # 下载GNU Make编译器
sudo apt-get install initramfs-tools # 下载安装initramfs-tools
2. 安装CUDA
步骤一: 下载CUDA安装包
- 进行CUDA和cuDNN的选择,也可以直接根据官方推荐进行下载安装。
- 从Nvidia官网下载CUDA https://developer.nvidia.com/cuda-downloads
下方链接,选择更多版本 https://developer.nvidia.com/cuda-toolkit-archive
- 接下来根据提示进行下载、安装
wget https://developer.download.nvidia.com/compute/cuda/11.8.0/local_installers/cuda_11.8.0_520.61.05_linux.run
sudo sh cuda_11.8.0_520.61.05_linux.run
步骤二: 安装CUDA安装包过程中的选择
-
Do you accept the above EULA? (accept / decline / quit):
是否接受最终用户许可协议,输入accept
-
回车键进行勾选,X就是选中,没有X就是没有选中,把安装驱动进行取消。之后向下键,回车确认
-
最后点击 install
-
出现了警告信息,对安装无影响
截图日志:
wit622@622:~/cuda$ sudo sh cuda_11.8.0_520.61.05_linux.run
[sudo] wit622 的密码:
===========
= Summary =
===========
Driver: Not Selected
Toolkit: Installed in /usr/local/cuda-11.8/
Please make sure that
- PATH includes /usr/local/cuda-11.8/bin
- LD_LIBRARY_PATH includes /usr/local/cuda-11.8/lib64, or, add /usr/local/cuda-11.8/lib64 to /etc/ld.so.conf and run ldconfig as root
To uninstall the CUDA Toolkit, run cuda-uninstaller in /usr/local/cuda-11.8/bin
***WARNING: Incomplete installation! This installation did not install the CUDA Driver. A driver of version at least 520.00 is required for CUDA 11.8 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
也可以进入日志进行查看(位置如下):
/var/log/cuda-installer.log
步骤三: 配置CUDA环境
sudo vim ~/.bashrc
- 在bashrc文件最下方,添加下入代码
- (ps:这边需要注意cuda的版本,版本不同,路径的命名需修改)
export PATH=$PATH:/usr/local/cuda-11.8/bin
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda-11.8/lib64
- 更新环境
source ~/.bashrc
- 测试CUDA是否安装成功
nvcc -V
输出下述结果,表示安装成功:
wit622@622:~/cuda$ nvcc -V
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2022 NVIDIA Corporation
Built on Wed_Sep_21_10:33:58_PDT_2022
Cuda compilation tools, release 11.8, V11.8.89
Build cuda_11.8.r11.8/compiler.31833905_0
3. 安装cuDNN
步骤一:下载cuDNN包
https://developer.nvidia.com/rdp/cudnn-archive
- 例如这边选择了Download cuDNN v8.8.1 (March 8th, 2023), for CUDA 11.x
- 点击Local Installer for Linux x86_64 (Tar)即可下载压缩包
- 将压缩包,放入自定义路径后,输入命令进行解压
tar -xvf cudnn-linux-x86_64-8.8.1.3_cuda11-archive.tar.xz
- 解压后,输入命令,讲cuDNN对应文件拷贝至CUDA指定路径。
wit622@622:~/下载$ cd cudnn-linux-x86_64-8.8.1.3_cuda11-archive/
wit622@622:~/下载/cudnn-linux-x86_64-8.8.1.3_cuda11-archive$ ls
include lib LICENSE
sudo cp include/cudnn*.h /usr/local/cuda-11.8/include
sudo cp lib/libcudnn* /usr/local/cuda-11.8/lib64
sudo chmod a+r /usr/local/cuda-11.8/include/cudnn*.h /usr/local/cuda-11.8/lib64/libcudnn*
4. CUDA版本切换
因为后续部分项目,用的库对应的CUDA不同,无需进行修改,修改环境CUDA路径即可。例如需要11.1的CUDA,可通过修改bashrc进行修改
sudo vim ~/.bashrc
将原先的cuda-11.8注释掉,添加cuda-11.1新的环境设置,即可
# cuda-11.8
# export PATH=$PATH:/usr/local/cuda-11.8/bin
# export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda-11.8/lib64
# cuda-11.1
export PATH=$PATH:/usr/local/cuda-11.1/bin
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda-11.1/lib64