当前位置: 代码迷 >> 综合 >> ubuntu 18.04 安装CUDA + CUDNN + tensorflow-gpu
  详细解决方案

ubuntu 18.04 安装CUDA + CUDNN + tensorflow-gpu

热度:84   发布时间:2023-10-25 07:49:23.0

预备条件: 安装2080Ti的驱动

直接参考这个Ubuntu 18.04安装NVIDIA(英伟达) RTX2080Ti显卡。但是请注意,里边有个地方命令中包含了中文,

#原文中这里的-no-opengl-files -no-x-check的 - 是中文的,我这边改成了英文,其他的没啥错误
sudo bash NVIDIA-Linux-x86_64-430.26.run -no-opengl-files -no-x-check

一. 查看tensorflow-gpu对应的python,CUDA以及CUDNN的版本

通过tensorflow-gpu配置,得到如下信息:
tensorflow-gpu对应的配置
这里本人打算安装tensorflow-gpu 2.3版本的,所以采用的CUDA版本为10.1,CUDNN版本为7.6

二. 安装CUDA

首先去官网地址
CUDA
因为我们想要下载的CUDA的版本是10.1,截止目前2021/12/22最新版本是11.5。所以需要下载历史版本。你可以点击图中的历史版本的链接,也可以点击此链接去下载:
10.1
接着点击左侧的CUDA Toolkit 10.1 update2
点击顺序
按照上面的点击顺序可以得到下面这个图片:
安装步骤
图片中的文字就是安装步骤:

$ wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/cuda-ubuntu1804.pin
$ sudo mv cuda-ubuntu1804.pin /etc/apt/preferences.d/cuda-repository-pin-600
$ wget https://developer.download.nvidia.com/compute/cuda/10.1/Prod/local_installers/cuda-repo-ubuntu1804-10-1-local-10.1.243-418.87.00_1.0-1_amd64.deb
$ sudo dpkg -i cuda-repo-ubuntu1804-10-1-local-10.1.243-418.87.00_1.0-1_amd64.deb
$ sudo apt-key add /var/cuda-repo-10-1-local-10.1.243-418.87.00/7fa2af80.pub
$ sudo apt-get update
$ sudo apt-get -y install cuda

你如果想要安装不同于10.1版本的CUDA的话,你就按照上面图片点击后产生的安装步骤来(不要局限于我这个版本的)。

2.1 如何判断CUDA是否成功安装成功

这里介绍两种方式:

方法1:
$ cd /usr/local
$ ls #你会发现出现了cuda-10.1,切记因为我安装的是10.1版本的,所以你ls以后不一定是cuda-10.1
bin   cuda-10.1  etc    go       lib  sbin   src
cuda  doc        games  include  man  share  sunlogin
$ cd cuda-10.1
$ ls 
bin     include    libnvvp           nvml    samples  targets
doc     lib64      LICENSE           nvvm    share    tools
extras  libnsight  nsightee_plugins  README  src      version.txt
$ cat version.txt #这里就表示你安装成功了
CUDA Version 10.1.243方法2: #还是在这个目录下
$ cd bin 
$ ./nvcc -V #出现下面这个也表示成功了
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2019 NVIDIA Corporation
Built on Sun_Jul_28_19:07:16_PDT_2019
Cuda compilation tools, release 10.1, V10.1.243

这样就表示我们安装成功了,当然可能见过如下测试方式:
$ nvcc -V
其实这个方法就是方法二,为啥我们这里不直接使用nvcc -V呢?因为,我们没有把nvcc进行环境配置,就是在别的目录下不能识别到这个可执行文件。下面是这个nvcc的环境变量配置:

$ sudo vim ~/.bashrc
#在末尾加上这两句话
export PATH=/usr/local/cuda-10.1/bin${
    PATH:+:${
    PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda-10.1/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
$ #保存并退出
$ source ~/.bashrc #使配置文件生效
#接着打开/etc/profile
$ sudo vim ~/.profile
#在末尾加上这两句话
PATH=/usr/local/cuda/bin:$PATH
export PATH
$ source ~/.profile #使之生效$ nvcc -V #配置成功
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2019 NVIDIA Corporation
Built on Sun_Jul_28_19:07:16_PDT_2019
Cuda compilation tools, release 10.1, V10.1.243

三. 安装CUDNN

依然是打开CUDNN官网
cudnn历史版本
选择Archived cuDNN Release去下载CUDNN历史版本。因为前面说了我们想要下载的tensorflow-gpu 2.3对应的CUDNN版本为7.6

我选择的是用deb方式安装的
ubuntu 18.04 cudnn

3.1安装CUDNN

$  sudo dpkg -i libcudnn7_7.6.5.32-1+cuda10.1_amd64.deb
Selecting previously unselected package libcudnn7.
(Reading database ... 266986 files and directories currently installed.)
Preparing to unpack libcudnn7_7.6.5.32-1+cuda10.1_amd64.deb ...
Unpacking libcudnn7 (7.6.5.32-1+cuda10.1) ...
Setting up libcudnn7 (7.6.5.32-1+cuda10.1) ...
Processing triggers for libc-bin (2.27-3ubuntu1.4) ...
$ sudo dpkg -i libcudnn7-dev_7.6.5.32-1+cuda10.1_amd64.deb 
Selecting previously unselected package libcudnn7-dev.
(Reading database ... 267056 files and directories currently installed.)
Preparing to unpack libcudnn7-dev_7.6.5.32-1+cuda10.1_amd64.deb ...
Unpacking libcudnn7-dev (7.6.5.32-1+cuda10.1) ...
Setting up libcudnn7-dev (7.6.5.32-1+cuda10.1) ...
update-alternatives: using /usr/include/x86_64-linux-gnu/cudnn_v7.h to provide /usr/include/cudnn.h (libcudnn) in auto mode
$ sudo dpkg -i libcudnn7-doc_7.6.5.32-1+cuda10.1_amd64.deb 
(Reading database ... 267062 files and directories currently installed.)
Preparing to unpack libcudnn7-doc_7.6.5.32-1+cuda10.1_amd64.deb ...
Unpacking libcudnn7-doc (7.6.5.32-1+cuda10.1) over (7.6.5.32-1+cuda10.1) ...
Setting up libcudnn7-doc (7.6.5.32-1+cuda10.1) ...

3.2 测试安装是否成功

$  cp -r /usr/src/cudnn_samples_v7/ $HOME  #请注意,这个cudnn_samples_v7一定要根据你自己的来,你直接去/usr/src中查看即可
$ cd $HOME/cudnn_samples_v7/mnistCUDNN
$ make clean && make
$ ./mnistCUDNN 
cudnnGetVersion() : 7605 , CUDNN_VERSION from cudnn.h : 7605 (7.6.5)
Host compiler version : GCC 7.5.0
There are 1 CUDA capable devices on your machine :
device 0 : sms 68  Capabilities 7.5, SmClock 1545.0 Mhz, MemSize (Mb) 11019, MemClock 7000.0 Mhz, Ecc=0, boardGroupID=0
Using device 0Testing single precision
Loading image data/one_28x28.pgm
Performing forward propagation ...
Testing cudnnGetConvolutionForwardAlgorithm ...
Fastest algorithm is Algo 0
Testing cudnnFindConvolutionForwardAlgorithm ...
^^^^ CUDNN_STATUS_SUCCESS for Algo 1: 0.030304 time requiring 3464 memory
^^^^ CUDNN_STATUS_SUCCESS for Algo 0: 0.035424 time requiring 0 memory
^^^^ CUDNN_STATUS_SUCCESS for Algo 5: 0.044032 time requiring 203008 memory
^^^^ CUDNN_STATUS_SUCCESS for Algo 2: 0.044192 time requiring 57600 memory
^^^^ CUDNN_STATUS_SUCCESS for Algo 7: 0.069472 time requiring 2057744 memory
Resulting weights from Softmax:
0.0000000 0.9999399 0.0000000 0.0000000 0.0000561 0.0000000 0.0000012 0.0000017 0.0000010 0.0000000 
Loading image data/three_28x28.pgm
Performing forward propagation ...
Resulting weights from Softmax:
0.0000000 0.0000000 0.0000000 0.9999288 0.0000000 0.0000711 0.0000000 0.0000000 0.0000000 0.0000000 
Loading image data/five_28x28.pgm
Performing forward propagation ...
Resulting weights from Softmax:
0.0000000 0.0000008 0.0000000 0.0000002 0.0000000 0.9999820 0.0000154 0.0000000 0.0000012 0.0000006 Result of classification: 1 3 5Test passed!Testing half precision (math in single precision)
Loading image data/one_28x28.pgm
Performing forward propagation ...
Testing cudnnGetConvolutionForwardAlgorithm ...
Fastest algorithm is Algo 0
Testing cudnnFindConvolutionForwardAlgorithm ...
^^^^ CUDNN_STATUS_SUCCESS for Algo 0: 0.010240 time requiring 0 memory
^^^^ CUDNN_STATUS_SUCCESS for Algo 1: 0.022496 time requiring 3464 memory
^^^^ CUDNN_STATUS_SUCCESS for Algo 2: 0.030304 time requiring 28800 memory
^^^^ CUDNN_STATUS_SUCCESS for Algo 7: 0.044704 time requiring 2057744 memory
^^^^ CUDNN_STATUS_SUCCESS for Algo 5: 0.052928 time requiring 203008 memory
Resulting weights from Softmax:
0.0000001 1.0000000 0.0000001 0.0000000 0.0000563 0.0000001 0.0000012 0.0000017 0.0000010 0.0000001 
Loading image data/three_28x28.pgm
Performing forward propagation ...
Resulting weights from Softmax:
0.0000000 0.0000000 0.0000000 1.0000000 0.0000000 0.0000714 0.0000000 0.0000000 0.0000000 0.0000000 
Loading image data/five_28x28.pgm
Performing forward propagation ...
Resulting weights from Softmax:
0.0000000 0.0000008 0.0000000 0.0000002 0.0000000 1.0000000 0.0000154 0.0000000 0.0000012 0.0000006 Result of classification: 1 3 5Test passed!

四安装 tensorflow-gpu

#首先安装python3,如果你ubuntu18.04 自带python3,就不用安装了(大部分都自动python3.6.9
$ sudo apt-get install python3
#首先安装pip3
$ sudo apt-get install python3-pip
#接着升级下你的pip3
$ pip3 install --upgrade pip
#换下pip的源
$ sudo mkdir ~/.pip
$ cd ~/.pip
$ sudo vim pip.conf  #把下面的内容加上
[global]
index-url = http://mirrors.aliyun.com/pypi/simple
[install]
trusted-host=mirrors.aliyun.com
#最后安装tensorflow-gpu
$ pip3 install tensorflow-gpu==2.3.2
#注意,虽然出现了这个错误,但是没有影响我最后的使用
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
launchpadlib 1.10.6 requires testresources, which is not installed.Successfully installed absl-py-1.0.0 astunparse-1.6.3 cachetools-4.2.4 charset-normalizer-2.0.9 dataclasses-0.8 gast-0.3.3 google-auth-2.3.3 google-auth-oauthlib-0.4.6 google-pasta-0.2.0 grpcio-1.43.0 h5py-2.10.0 importlib-metadata-4.8.3 keras-preprocessing-1.1.2 markdown-3.3.6 numpy-1.18.5 oauthlib-3.1.1 opt-einsum-3.3.0 protobuf-3.19.1 pyasn1-0.4.8 pyasn1-modules-0.2.8 requests-2.26.0 requests-oauthlib-1.3.0 rsa-4.8 setuptools-59.6.0 six-1.16.0 tensorboard-2.7.0 tensorboard-data-server-0.6.1 tensorboard-plugin-wit-1.8.0 tensorflow-estimator-2.3.0 tensorflow-gpu-2.3.2 termcolor-1.1.0 typing-extensions-4.0.1 werkzeug-2.0.2 wrapt-1.13.3 zipp-3.6.0

五.最终测试

>>> import tensorflow as tf
2021-12-22 14:49:15.608668: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library libcudart.so.10.1
>>> print(tf.__version__)
2.3.2
>>> 

至此,本文结束,希望能帮助大家成功入门!