ubuntu18.04安装Tensorflow GPU版本全流程

ubuntu18.04安装Tensorflow GPU版本全流程

1安装显卡驱动

本文为本人根据自己电脑在ubuntu18.04上安装Tensorflow GPU版本的全部过程,部分图片摘自别的文章,链接已在文末给出.

1.1选择正确的显卡驱动

  1. 我们需要到英伟达的官网上选择符合自己电脑的显卡驱动。
    官网网址:https://www.nvidia.com/Download/index.aspx
    我的电脑的显卡是GEFORCE RTX 2070 SUPER,完成选择型号后点击SEARCH。
    ubuntu18.04安装Tensorflow GPU版本全流程
    ubuntu18.04安装Tensorflow GPU版本全流程

; 1.2显卡驱动安装

在安装显卡驱动之前,需要先安装一些 NVIDIA 显卡依赖的软件,
在终端依次执行以下命令:

[En]

Execute the following commands at the terminal in turn:

sudo dpkg –add-architecture i386

sudo apt update

sudo apt install build-essential libc6:i386

ubuntu18.04安装Tensorflow GPU版本全流程

sudo bash -c “echo blacklist nouveau > /etc/modprobe.d/blacklist-nvidia-nouveau.conf”

sudo bash -c “echo options nouveau modeset=0 >> /etc/modprobe.d/blacklist-nvidia-nouveau.conf”

执行完上述两条指令后,我们使用以下命令查看是否已成功禁用开源驱动:

[En]

After executing the above two instructions, we use the following command to see if the open source driver has been successfully disabled:

cat /etc/modprobe.d/blacklist-nvidia-nouveau.conf。

如果和下面一样,那就是成功了。

[En]

If it is the same as below, it is a success.

ubuntu18.04安装Tensorflow GPU版本全流程
可以在终端上输入:

sudo reboot

先在终端进入下载

cd /home/(自己的用户名)/下载

运行.run,在.run之前的文件名替换成自己的文件名

chmod +x NVIDIA-Linux-x86_64-470.82.00.run

sudo ./NVIDIA-Linux-x86_64-470.82.00.run

选择Continue installation

ubuntu18.04安装Tensorflow GPU版本全流程

选择No

ubuntu18.04安装Tensorflow GPU版本全流程

选择No

ubuntu18.04安装Tensorflow GPU版本全流程

选择Install and overwrite existing files

ubuntu18.04安装Tensorflow GPU版本全流程

选择Yes

ubuntu18.04安装Tensorflow GPU版本全流程

选择Ok

ubuntu18.04安装Tensorflow GPU版本全流程

运行代码以测试显卡驱动程序是否安装成功:

[En]

Run the code to test to see if the video card driver is installed successfully:

nvidia-smi

安装成功,如下图所示。

[En]

The installation is successful as shown in the following figure.

ubuntu18.04安装Tensorflow GPU版本全流程

题主在运行.run文件中遇到的几个错误,下面进行归纳总结。
1.没有gcc编译软件
解决方法:

sudo apt-get update

sudo apt-get install gcc

2.没有make编译软件
解决方法:

sudo apt-get update

sudo apt-get install make

3.出现安装错误
An NVIDIA kernel module ‘nvidia-drm’ appears to already be loaded in your kernel…

解决方法:

sudo systemctl isolate multi-user.target

sudo modprobe -r nvidia-drm

2.安装CUDA

安装CUDA和CUDNN需要对照自己Tensorflow版本选择合适的版本。
可以到Tensorflow官网进行查询。
官网地址:https://tensorflow.google.cn/install/source

ubuntu18.04安装Tensorflow GPU版本全流程
题主的Tensorflow版本是2.6.0的,所以选择的CUDA版本号为11.2,cuDNN的版本号为8.1。

; 2.1 CUDA的下载与安装

进入nvidia开发者网站的CUDA下载页面:
https://developer.nvidia.com/cuda-toolkit-archive
选择runfile格式的CUDA文件下载,

下载完成后,解压,并运行上图中的命令,会有条款,接受即可,注意安装CUDA的时候不要安装驱动(因为在第一步我们已经安装过了)。

然后,

sudo gedit ~/.bashrc

我们将以下两行添加到文件的最后一行:

[En]

We add the following two lines to the last line of the file:

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

export LD_LIBRARY_PATH=/usr/local/cuda-8.1/lib64:$LD_LIBRARY_PATH

最后,使配置生效。

[En]

Finally, make the configuration take effect.

source ~/.bashrc

终端输入:

nvcc -V

出现如下输出,则CUDA安装成功。

ubuntu18.04安装Tensorflow GPU版本全流程

2.2 CUDNN的下载与安装

进入下载页面:
https://developer.nvidia.com/rdp/cudnn-archive

ubuntu18.04安装Tensorflow GPU版本全流程
选择红色框中选择的三个文件并下载它们。
[En]

Select the three files selected in the red box and download them.

cuDNN Runtime Library for Ubuntu18.04 x86_64 (Deb)
cuDNN Developer Library for Ubuntu18.04 x86_64 (Deb)
cuDNN Code Samples and User Guide for Ubuntu18.04 x86_64 (Deb)

这样做的原因是因为:(1)直接下载cuDNN Library for Linux (x86_64)的话是tgz格式,需要解压安装,把头文件,链接库复制到cuda文件夹,还得给这些文件赋予权限,比较麻烦,而且版本多、命令多,容易出错。(2)直接下载的tgz文件之中并没有用来测试cudnn的sample样例,不能测试cudnn是否已经安装完毕,能否运行。

在终端中输入下载文件夹:

[En]

Enter the download folder in the terminal:

cd /home/用户名/下载

sudo dpkg -i libcudnn8_8.1.1.33-1+cuda11.2_amd64.deb

sudo dpkg -i libcudnn8-dev_8.1.1.33-1+cuda11.2_amd64.deb

sudo dpkg -i libcudnn8-samples_8.1.1.33-1+cuda11.2_amd64.deb

通过

cat /usr/local/cuda-10.1/include/cudnn.h | grep CUDNN_MAJOR -A 2

可以查看cuDNN版本。
如果出现下图所示的版本信息,则表示安装成功。

[En]

If the version information shown in the following figure appears, the installation is successful.

ubuntu18.04安装Tensorflow GPU版本全流程

; 3.安装Tensorflow-GPU

在anaconda里面创建一个名为tensorflow环境:

conda create -n tensorflow python==3.9

激活该环境:

conda activate tensorflow

安装tensorflow:

pip install tensorflow_gpu==2.6.0

下载与安装anaconda可参照这篇文章:https://blog.csdn.net/qq_29883591/article/details/78077244

试运行代码验证安装是否成功

[En]

The trial run code verifies that the installation is successful

import tensorflow as tf

print('GPU', tf.test.is_gpu_available())

a = tf.constant(2.)
b = tf.constant(4.)

print(a * b)

参考文章:
1.https://blog.csdn.net/u010705932/article/details/108125303?ops_request_misc=&request_id=&biz_id=102&utm_term=ubuntu18.04%E5%AE%89%E8%A3%85%E6%98%BE%E5%8D%A1&utm_medium=distribute.pc_search_result.none-task-blog-2allsobaiduweb~default-3-108125303.first_rank_v2_pc_rank_v29&spm=1018.2226.3001.4187
2.https://zhuanlan.zhihu.com/p/72298520

Original: https://blog.csdn.net/qq_37717532/article/details/121287583
Author: X堃S
Title: ubuntu18.04安装Tensorflow GPU版本全流程

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/509692/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球