Ubuntu16.04下CUDA的安装和卸载

1.下载 cuda.xxx.run 文件

https://developer.nvidia.com/cuda-downloads,下载 cuda_9.1.85_387.26_linux.run文件

2.在终端运行该条指令即可:

$ sudo sh cuda_9.1.85_387.26_linux.run --no-opengl-libs

之后是一些提示信息,ctrl+c 直接结束后输入 accept。 接下来很重要的地方是在提示是否安装显卡驱动时,一定选择 no(之前安装过对应显卡版本的驱动) 其他各项提示选择是,并默认安装路径即可。提示有 y 的输入 y,没有则按 enter 键。安装完毕。 之后声明一下环境变量,并将其写入到 ~/.bashrc 文件(在用户目录下)的尾部,输入内容如下

export PATH=/usr/local/cuda-9.1/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda-9.1/lib64:$LD_LIBRARY_PATH

保存退出,并输入下面指令使环境变量立刻生效:

$source ~/.bashrc

3.设置环境变量和动态链接库,在命令行输入:

$sudo vim /etc/profile

在打开的文件末尾加入:

export PATH=/usr/local/cuda/bin:$PATH

4.创建链接文件

$ sudo vim /etc/ld.so.conf.d/cuda.conf

在打开的文件中添加如下语句:

/usr/local/cuda/lib64

保存退出,然后执行

$ sudo ldconfig 

使链接立即生效。

5.测试 cuda 的 Samples

切换到 CUDA 9.1 Samples 默认安装路径(即在/home/用户/ NVIDIA_CUDA-9.1_Samples 目录下), 终端下输入

$ cd NVIDIA_CUDA-9.1_Samples
$ sudo make all –j8
$ cd bin/x86_64/linux/release
$ ./deviceQuery

报错

$ ./deviceQuery
./deviceQuery Starting...

 CUDA Device Query (Runtime API) version (CUDART static linking)

cudaGetDeviceCount returned 30
-> unknown error
Result = FAIL

查看显卡是否安装

$ nvidia-smi
NVIDIA-SMI has failed because it couldn't communicate with the NVIDIA driver. Make sure that the latest NVIDIA driver is installed and running.

发现显卡驱动没有安装成功 而且nouveau禁用失败

$ lsmod | grep nouveau
nouveau              1601536  1
mxm_wmi                16384  1 nouveau
ttm                    98304  1 nouveau
i2c_algo_bit           16384  2 nouveau,i915
drm_kms_helper        151552  2 nouveau,i915
drm                   352256  14 nouveau,i915,ttm,drm_kms_helper
wmi                    16384  2 mxm_wmi,nouveau
video                  40960  3 thinkpad_acpi,nouveau,i915

这是需要重新禁用nouveau和安装显卡驱动

如果 CUDA 安装成功,则有:

$ ./deviceQuery
./deviceQuery Starting...

 CUDA Device Query (Runtime API) version (CUDART static linking)

Detected 1 CUDA Capable device(s)

Device 0: "GeForce GT 635M"
  CUDA Driver Version / Runtime Version          9.0 / 8.0
  CUDA Capability Major/Minor version number:    2.1
  Total amount of global memory:                 1985 MBytes (2081619968 bytes)
  ( 2) Multiprocessors, ( 48) CUDA Cores/MP:     96 CUDA Cores
  GPU Max Clock rate:                            950 MHz (0.95 GHz)
  Memory Clock rate:                             900 Mhz
  Memory Bus Width:                              128-bit
  L2 Cache Size:                                 131072 bytes
  Maximum Texture Dimension Size (x,y,z)         1D=(65536), 2D=(65536, 65535), 3D=(2048, 2048, 2048)
  Maximum Layered 1D Texture Size, (num) layers  1D=(16384), 2048 layers
  Maximum Layered 2D Texture Size, (num) layers  2D=(16384, 16384), 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: 32768
  Warp size:                                     32
  Maximum number of threads per multiprocessor:  1536
  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): (65535, 65535, 65535)
  Maximum memory pitch:                          2147483647 bytes
  Texture alignment:                             512 bytes
  Concurrent copy and kernel execution:          Yes with 1 copy engine(s)
  Run time limit on kernels:                     No
  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
  Device PCI Domain ID / Bus ID / location ID:   0 / 1 / 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 = 8.0, NumDevs = 1, Device0 = GeForce GT 635M
Result = PASS

6.卸载CUDA

在/usr/local/cuda/bin 目录下,有cuda 自带的卸载工具uninstall_cuda_9.1.pl

$ cd /usr/local/cuda/bin
$ sudo ./uninstall_cuda_9.1.pl

7.其他问题

查看NVIDIA GPU版本信息

$ lspci | grep -i nvidia

01:00.0 VGA compatible controller: NVIDIA Corporation GF108M [GeForce GT 635M] (rev a1)

验证自己的Linux版本是否支持 CUDA

$ uname -m && cat /etc/*release
x86_64
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=16.04
DISTRIB_CODENAME=xenial
DISTRIB_DESCRIPTION="Ubuntu 16.04.3 LTS"
NAME="Ubuntu"
VERSION="16.04.3 LTS (Xenial Xerus)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 16.04.3 LTS"
VERSION_ID="16.04"
HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
VERSION_CODENAME=xenial
UBUNTU_CODENAME=xenial

查看正在运行的系统内核版本:

 $  uname –r
4.10.0-28-generic

安装对应kernel版本的kernel header和package development

sudo apt-get install linux-headers-$(uname -r)

查看是否禁用了 nouveau驱动

lsmod | grep nouveau

如果有输出则代表nouveau正在加载。需要我们手动禁nouveau,Ubuntu的nouveau禁用方法前面提到过

参看链接: http://blog.csdn.net/qlulibin/article/details/78714596

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券