欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Ubuntu16.04安装CUDA

程序员文章站 2022-07-13 13:31:48
...

CUDA安装步骤

1. 查看GPU是否支持CUDA
$ lspci | grep -i nvida

如果未出现任何信息,输入update-pciids升级

出现这些信息就是正常的:

01:00.0 VGA compatible controller: NVIDIA Corporation GP107M [GeForce GTX 1050 Ti Mobile] (rev a1)
01:00.1 Audio device: NVIDIA Corporation GP107GL High Definition Audio Controller (rev a1)
2. 更新显卡驱动(若适配则跳过)

查看你电脑的显卡驱动

$ ubuntu-drivers devices
== /sys/devices/pci0000:00/0000:00:01.0/0000:01:00.0 ==
modalias : pci:v000010DEd00001C8Csv000017AAsd000039FDbc03sc00i00
vendor   : NVIDIA Corporation
driver   : xserver-xorg-video-nouveau - distro free builtin
driver   : nvidia-384 - distro non-free recommended
#从最后一行可以看出我的电脑的推荐驱动是nvidia-384

安装推荐的驱动

$ apt-get update
$ apt-get remove nvidia-*	#先删除原来的
$ apt-get install nvidia-384	#对应的是我的驱动型号

检查是否安装成功

$ nvidia-smi	#有以下结果代表安装成功
Thu Oct 29 13:48:33 2020       
+-----------------------------------------------------------------------------+
| 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 105...  Off  | 00000000:01:00.0 Off |                  N/A |
| N/A   43C    P8    N/A /  N/A |    250MiB /  4038MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+
                                                                               
+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID   Type   Process name                             Usage      |
|=============================================================================|
|    0      1146      G   /usr/lib/xorg/Xorg                           204MiB |
|    0      2161      G   compiz                                        17MiB |
|    0      3107      G   /usr/lib/firefox/firefox                       1MiB |
|    0      6134      G   ...AAAAAAAAAAAACAAAAAAAAAA= --shared-files    24MiB |
+-----------------------------------------------------------------------------+
3. 查看系统信息
$ uname -m && cat /etc/*release
x86_64										#有用
DISTRIB_ID=Ubuntu							#有用
DISTRIB_RELEASE=16.04						#有用
DISTRIB_CODENAME=xenial
DISTRIB_DESCRIPTION="Ubuntu 16.04.7 LTS"
NAME="Ubuntu"
VERSION="16.04.7 LTS (Xenial Xerus)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 16.04.7 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
4. 下载安装包:
  • 官网找到你想下载的版本(我选择的是CUDA Toolkit 10.0 [Sept 2018]),并点击进去

  • 按照上一步中得到的你的系统的信息选择对应的选项,注意最后一个选择runfile(local)
    Ubuntu16.04安装CUDA

  • 下载Base Installer即可

5. 安装GCC(略)
6. 确定内核安装完整
$ uname -r
4.15.0-118-generic
$ sudo apt-get install linux-headers-$(uname -r)
#一般来说没有需要安装的
7. 使用bash安装CUDA
$ sudo bash cuda_8.0.61_375.26_linux.run
#开始进入安装流程
#依次输入:
#: n
#: y
#: y
#: y
#: y
#结束

最后会有一个warning,提示你驱动没有安装,但是我们事先是已经安装了的,所以不必管他

8. 配置环境
$ sudo gedit /etc/bash.bashrc

在文末输入:

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda-8.0/lib64
export PATH=$PATH:/usr/local/cuda-8.0/bin
export CUDA_HOME=$CUDA_HOME:/usr/local/cuda-8.0
#其中cuda的文件夹名需要对应你的cuda版本

然后

$ source ~/.bashrc

结束

测试1
$ nvcc --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2016 NVIDIA Corporation
Built on Tue_Jan_10_13:22:03_CST_2017
Cuda compilation tools, release 8.0, V8.0.61
#安装成功
测试2
$ cd /usr/local/cuda/samples
$ sudo make
$ ./1_Utilities/deviceQuery/deviceQuery #可以看到如下输出
./1_Utilities/deviceQuery/deviceQuery Starting...

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

Detected 1 CUDA Capable device(s)

Device 0: "GeForce GTX 1050 Ti"
  CUDA Driver Version / Runtime Version          9.0 / 8.0
  CUDA Capability Major/Minor version number:    6.1
  Total amount of global memory:                 4039 MBytes (4235001856 bytes)
  ( 6) Multiprocessors, (128) CUDA Cores/MP:     768 CUDA Cores
  GPU Max Clock rate:                            1620 MHz (1.62 GHz)
  Memory Clock rate:                             3504 Mhz
  Memory Bus Width:                              128-bit
  L2 Cache Size:                                 1048576 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
	......