在这里,把在Ubuntu 16.04系统上安装CUDA 9.0的步骤记录下来,以备查阅。
安装CUDA一定要参考官方的安装手册:
准备工作
1. 检查自己的GPU是否支持CUDA(即是否为CUDA GPUs)
lspci | grep -i nvidia
输出如下:
07:00.0 VGA compatible controller: NVIDIA Corporation GP104 [GeForce GTX 1080] (rev a1)
07:00.1 Audio device: NVIDIA Corporation GP104 High Definition Audio Controller (rev a1)
2. 查看GPU驱动版本
sudo ubuntu-drivers devices
输出如下:
== /sys/devices/pci0000:00/0000:00:03.1/0000:07:00.0 ==
vendor : NVIDIA Corporation
modalias : pci:v000010DEd00001B80sv000010DEsd0000119Ebc03sc00i00
driver : nvidia-384 - distro non-free recommended
driver : xserver-xorg-video-nouveau - distro free builtin
注:这里看到的驱动版本为nvidia-384,是Ubuntu 16.04系统安装包自带的驱动,是经过测试的,我们应该使用此版本的驱动。如果要升级到更高版本,可能会引起问题(如循环登录等问题,可从网上搜到)。
3. 查看Linux版本
uname -m && cat /etc/*release
输出如下:
x86_64
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=16.04
DISTRIB_CODENAME=xenial
DISTRIB_DESCRIPTION="Ubuntu 16.04.6 LTS"
...
4. 检查自己的驱动版本与哪个版本的CUDA兼容
(版本兼容问题,可参考:NVIDIA CUDA Toolkit Release Notes)
CUDA Toolkit | Linux x86_64 Driver Version | Windows x86_64 Driver Version |
---|---|---|
CUDA 10.1 Update 1 | >= 418.67 | >= 425.25 |
CUDA 10.1.105 | >= 418.39 | >= 418.96 |
CUDA 10.0.130 | >= 410.48 | >= 411.31 |
CUDA 9.2 (9.2.148 Update 1) | >= 396.37 | >= 398.26 |
CUDA 9.2 (9.2.88) | >= 396.26 | >= 397.44 |
CUDA 9.1 (9.1.85) | >= 390.46 | >= 391.29 |
CUDA 9.0 (9.0.76) | >= 384.81 | >= 385.54 |
CUDA 8.0 (8.0.61 GA2) | >= 375.26 | >= 376.51 |
CUDA 8.0 (8.0.44) | >= 367.48 | >= 369.30 |
可以看出,我的系统应安装CUDA 9.0.
5. 下载CUDA 9.0安装包及所有补丁包(即Patches):
安装CUDA 9.0
1. 关闭nouveau驱动
1.1 创建并打开文件"blacklist-nouveau.conf":
sudo gedit /etc/modprobe.d/blacklist-nouveau.conf
1.2 添加以下内容到该文件末尾:
blacklist nouveau
options nouveau modeset=0
1.3 重新生成kernel initramfs:
sudo update-initramfs -u
2. 把系统界面切换到命令行模式
2.1 打开文件"grub":
sudo gedit /etc/default/grub
2.2 找到这一行:
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash"
修改为:
GRUB_CMDLINE_LINUX_DEFAULT="text"
2.3 更新GRUB:
sudo update-grub
2.4 打开multi-user模式:
sudo systemctl set-default multi-user.target
(注:该命令创建了一个文件符号链接"/etc/systemd/system/default.target",将其链接到"/lib/systemd/system/multi-user.target")
2.5 重启系统。
3. 安装CUDA 9.0
3.1 安装CUDA 9.0:
sudo sh cuda_9.0.176_384.81_linux.run --no-opengl-libs
(注意不要安装OpenGL Libs,否则会造成循环登录的问题,可从网上搜到该问题。)
如果在安装过程中遇到问题(如安装失败),可打开log查看原因。如有依赖工具(如cmake等)未安装,可尝试安装这些依赖工具,然后再安装一次CUDA。
3.2 参考以上方式安装所有5个下载的补丁包。
4. 恢复系统图形化界面
(将步骤“2. 把系统界面切换到命令行模式”反向做一遍即可。)
4.1 将文件"/etc/default/grub"中"GRUB_CMDLINE_LINUX_DEFAULT"的值恢复为原来的值"quiet splash":
sudo gedit /etc/default/grub
GRUB_CMDLINE_LINUX_DEFAULT="text"
-> GRUB_CMDLINE_LINUX_DEFAULT="quiet splash"
4.2 删除或重命名文件"/etc/systemd/system/default.target":
sudo mv /etc/systemd/system/default.target /etc/systemd/system/default.target.backup
5. 重启系统,进入图形化界面
6. 把CUDA加入到环境变量
6.1 打开"/etc/profile":
sudo gedit /etc/profile
6.2 在文件末尾添加以下内容:
export PATH=/usr/local/cuda-9.0/bin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda-9.0/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
6.3 重启系统,检查上述环境变量是否设置成功
7. 测试CUDA
7.1 查看CUDA的版本:
nvcc -V
输出为:
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2017 NVIDIA Corporation
Built on Fri_Sep__1_21:08:03_CDT_2017
Cuda compilation tools, release 9.0, V9.0.176
还可运行:
nvidia-smi
输出为:
Sun Jun 2 16:16:01 2019
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 384.130 Driver Version: 384.130 |
|-------------------------------+----------------------+----------------------+
| 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 GTX 1080 Off | 00000000:07:00.0 On | N/A |
| 27% 37C P8 15W / 180W | 688MiB / 8112MiB | 0% Default |
+-------------------------------+----------------------+----------------------+
+-----------------------------------------------------------------------------+
| Processes: GPU Memory |
| GPU PID Type Process name Usage |
|=============================================================================|
| 0 1310 G /usr/lib/xorg/Xorg 288MiB |
| 0 1881 G compiz 214MiB |
| 0 2664 G fcitx-qimpanel 8MiB |
| 0 2957 G ...uest-channel-token=15858479812120104221 174MiB |
+-----------------------------------------------------------------------------+
7.2 编译并运行CUDA自带的用例:
cd /usr/local/cuda-9.0/samples/1_Utilities/deviceQuery
sudo make
./deviceQuery
输出为:
./deviceQuery Starting...
CUDA Device Query (Runtime API) version (CUDART static linking)
Detected 1 CUDA Capable device(s)
Device 0: "GeForce GTX 1080"
CUDA Driver Version / Runtime Version 9.0 / 9.0
CUDA Capability Major/Minor version number: 6.1
Total amount of global memory: 8113 MBytes (8506769408 bytes)
(20) Multiprocessors, (128) CUDA Cores/MP: 2560 CUDA Cores
GPU Max Clock rate: 1734 MHz (1.73 GHz)
Memory Clock rate: 5005 Mhz
Memory Bus Width: 256-bit
L2 Cache Size: 2097152 bytes
Maximum Texture Dimension Size (x,y,z) 1D=(131072), 2D=(131072, 65536), 3D=(16384, 16384, 16384)
Maximum Layered 1D Texture Size, (num) layers 1D=(32768), 2048 layers
Maximum Layered 2D Texture Size, (num) layers 2D=(32768, 32768), 2048 layers
Total amount of constant memory: 65536 bytes
Total amount of shared memory per block: 49152 bytes
Total number of registers available per block: 65536
Warp size: 32
Maximum number of threads per multiprocessor: 2048
Maximum number of threads per block: 1024
Max dimension size of a thread block (x,y,z): (1024, 1024, 64)
Max dimension size of a grid size (x,y,z): (2147483647, 65535, 65535)
Maximum memory pitch: 2147483647 bytes
Texture alignment: 512 bytes
Concurrent copy and kernel execution: Yes with 2 copy engine(s)
Run time limit on kernels: Yes
Integrated GPU sharing Host Memory: No
Support host page-locked memory mapping: Yes
Alignment requirement for Surfaces: Yes
Device has ECC support: Disabled
Device supports Unified Addressing (UVA): Yes
Supports Cooperative Kernel Launch: Yes
Supports MultiDevice Co-op Kernel Launch: Yes
Device PCI Domain ID / Bus ID / location ID: 0 / 7 / 0
Compute Mode:
< Default (multiple host threads can use ::cudaSetDevice() with device simultaneously) >
deviceQuery, CUDA Driver = CUDART, CUDA Driver Version = 9.0, CUDA Runtime Version = 9.0, NumDevs = 1
Result = PASS
其他
关于如何安装Ubuntu 16.04系统,可参考: