ubuntu18.04配置cuda开发环境
1.显卡驱动
请在下载安装cuda前检查是否有匹配的cudnn(目前支持到10.2)
卸载现有的nvidia驱动
$ sudo apt-get remove --purge nvidia*
1.1、添加Graphic Drivers PPA
$ sudo add-apt-repository ppa:graphics-drivers/ppa
$ sudo apt-get update
1.2、查询驱动型号,最好就安装recommended的驱动版本型号
$ ubuntu-drivers devices
输出应如下:
== /sys/devices/pci0000:00/0000:00:03.1/0000:09:00.0 ==
modalias : pci:v000010DEd00001F08sv00007377sd00000000bc03sc00i00
vendor : NVIDIA Corporation
driver : nvidia-driver-415 - third-party free
driver : nvidia-driver-440 - third-party free recommended
driver : nvidia-driver-435 - distro non-free
driver : xserver-xorg-video-nouveau - distro free builtin
此处可以看出440为推荐驱动(请根据情况选择)
1.3、安装nvidia-driver
$ sudo apt-get install nvidia-driver-440
1.4、安装成功之后,请重新启动电脑
$ sudo reboot
1.5、检查驱动是否安装成功
$ nvidia-smi
输出应类似如下:
Sat Apr 11 17:24:10 2020
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 440.82 Driver Version: 440.82 CUDA Version: 10.2 |
|-------------------------------+----------------------+----------------------+
| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
|===============================+======================+======================|
| 0 GeForce RTX 2060 Off | 00000000:09:00.0 On | N/A |
| 43% 34C P8 19W / 160W | 377MiB / 5926MiB | 4% Default |
+-------------------------------+----------------------+----------------------+
+-----------------------------------------------------------------------------+
| Processes: GPU Memory |
| GPU PID Type Process name Usage |
|=============================================================================|
| 0 1402 G /usr/lib/xorg/Xorg 18MiB |
| 0 1445 G /usr/bin/gnome-shell 71MiB |
| 0 4584 G /usr/lib/xorg/Xorg 126MiB |
| 0 4677 G /usr/bin/gnome-shell 125MiB |
| 0 5834 G /usr/lib/firefox/firefox 3MiB |
| 0 6179 G ...quest-channel-token=7094970825308615276 28MiB |
+-----------------------------------------------------------------------------+
2.CUDA
2.1、CUDA下载
在下载链接中找到适合的版本下载
在命令行执行wget命令(10.2开始使用)
2.2、文件改名
下载完成后在Downloads文件夹中(cuda10.2在home文件夹中)找到找到cuda_版本号_linux.run的文件
将其重命名为cuda.run
将其移动至home文件夹中
2.3、CUDA安装
$ sudo sh cuda.run
之后输入
accept
在driver选项上按回车将driver选项的x
项去掉
在install选项上按回车开始安装
2.4、环境配置
$ sudo gedit ~/.bashrc
在最后另起一行添加:
export PATH=/usr/local/cuda/bin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
2.5、检查安装
CUDA版本信息查看
$ nvcc --version
应正确输出CUDA版本信息类似如下:
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2019 NVIDIA Corporation
Built on Wed_Oct_23_19:24:38_PDT_2019
Cuda compilation tools, release 10.2, V10.2.89
显卡驱动信息查看
$ cat /proc/driver/nvidia/version
应正确输出显卡驱动信息类似如下:
NVRM version: NVIDIA UNIX x86_64 Kernel Module 440.82 Wed Apr 1 20:04:33 UTC 2020
GCC version: gcc version 7.5.0 (Ubuntu 7.5.0-3ubuntu1~18.04)
2.6、测试Samples
$ cd /usr/local/cuda/samples/1_Utilities/deviceQuery
$ sudo make
$ ./deviceQuery
如果输出Result = PASS
表示安装成功
3.cuDNN
3.1、cuDNN下载
在下载链接(需要注册后下载)中找到适合的版本下载
下载并解压后将内部的cuda
文件夹移动到home文件夹内
3.2、cuDNN安装
$ sudo cp cuda/include/cudnn.h /usr/local/cuda/include/
$ sudo cp cuda/lib64/libcudnn* /usr/local/cuda/lib64/
$ sudo chmod a+r /usr/local/cuda/include/cudnn.h
$ sudo chmod a+r /usr/local/cuda/lib64/libcudnn*
3.3、验证
$ cat /usr/local/cuda/include/cudnn.h | grep CUDNN_MAJOR -A 2
输出应类似如下:
#define CUDNN_MAJOR 7
#define CUDNN_MINOR 6
#define CUDNN_PATCHLEVEL 5
--
#define CUDNN_VERSION (CUDNN_MAJOR * 1000 + CUDNN_MINOR * 100 + CUDNN_PATCHLEVEL)
#include "driver_types.h"