Ubuntu 18下安装tensorflow(基于python3.6)

#配置如下
Ubuntu 18.04 LTS

python 3.6

nvidia driver 390

cuda 9.0

cudnn 7.1

tensorflow 1.5

安装显卡驱动

查看NVIDIA driver 推荐。

$ ubuntu-drivers devices
== /sys/devices/pci0000:00/0000:00:01.0/0000:01:00.0 ==
modalias : pci:v000010DEd00001C81sv00001028sd000011C0bc03sc00i00
vendor   : NVIDIA Corporation
model    : GP107 [GeForce GTX 1050]
driver   : nvidia-driver-396 - third-party free recommended
driver   : nvidia-driver-390 - third-party free
driver   : xserver-xorg-video-nouveau - distro free builtin

安装:

$ sudo ubuntu-drivers autoinstall

按照推荐的驱动安装,如果失败,请添加NVIDIA driver源仓库然后在安装,安装命令如下:

$ sudo add-apt-repository ppa:graphics-drivers/ppa

测试安装是否成功(提示无法找到的时候,可以先试试重启机器再测试)

$ nvidia-smi

Tue May 22 12:14:56 2018
±----------------------------------------------------------------------------+
| NVIDIA-SMI 390.59 Driver Version: 390.59 |
|-------------------------------±---------------------±---------------------+
| 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 1050 Off | 00000000:01:00.0 Off | N/A |
| 20% 39C P0 N/A / 75W | 444MiB / 2000MiB | 0% Default |
±------------------------------±---------------------±---------------------+

±----------------------------------------------------------------------------+
| Processes: GPU Memory |
| GPU PID Type Process name Usage |
|=============================================================================|
| 0 1155 G /usr/lib/xorg/Xorg 28MiB |
| 0 1252 G /usr/bin/gnome-shell 48MiB |
| 0 1539 G /usr/lib/xorg/Xorg 163MiB |
| 0 1713 G /usr/bin/gnome-shell 107MiB |
| 0 2442 G …-token=755D9B9E74B9E412D1116412B89F4DC2 93MiB |
±----------------------------------------------------------------------------+

出现上述信息则说明驱动安装成功

安装CUDA

官网下载cuda-9.0下载如下三个文件:

cuda_9.0.176_384.81_linux.run
cuda_9.0.176.1_linux.run
cuda_9.0.176.2_linux.run

下载完成之后加入执行权限并开始安装

注意:
1.安装cuda的时候并没有降级gcc,g++;说明cuda9.0已经支持gcc7.0安装,所谓降级是后面要编译cuda测试例子的时候用到6.0以下的g++,和gcc版本。

2.安装过程中注意看以下询问是否安装NVIDIA driver的时候原则no 其他yes即可(按住回车使0%涨到100%)。

$ sudo chmod +x cuda_9.0.176_384.81_linux.run
$ sudo chmod +x cuda_9.0.176.1_linux.run
$ sudo chmod +x cuda_9.0.176.2_linux.run
$ ./cuda_9.0.176_384.81_linux.run --override
$ ./cuda_9.0.176.1_linux.run
$ ./cuda_9.0.176.2_linux.run

测试cuda

注意选择该例子进行测试需要g++,gcc版本为6.0以下否则编译会出错。而ubuntu18.04 LST自带的g++,和gcc

版本为7.3,因此我们选择安装一个低版本的g++ 5.0,和gcc 5.0

并设置为版本控制,如下:

$ sudo apt install gcc-5 g+±5
$ sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g+±5 50
$ sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-5 50
$ sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g+±7 70
$ sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 70

这样就可以通过以下命令来切换使用的gcc和g++版本

$ sudo update-alternatives --config g++
There are 2 choices for the alternative g++ (providing /usr/bin/g++).

Selection Path Priority Status

0 /usr/bin/g+±7 70 auto mode

  • 1 /usr/bin/g+±5 50 manual mode
    2 /usr/bin/g+±7 70 manual mode

Press to keep the current choice[*], or type selection number:

$ sudo update-alternatives --config gcc
There are 2 choices for the alternative gcc (providing /usr/bin/gcc).

Selection Path Priority Status

0 /usr/bin/gcc-7 70 auto mode

  • 1 /usr/bin/gcc-5 50 manual mode
    2 /usr/bin/gcc-7 70 manual mode

Press to keep the current choice[*], or type selection number:

如图所示按1,选择5.0版本。

环境变量设置:
在测试之前还有一步就是要做一个路径的添加,否则会报错。同时把下面三条指令放在自己的bash配置文件里/.bashrc或者/.zshrc里面。

我的是gedit ~/.bashrc(记得要source ~/.bashrc)

$ export PATH=/usr/local/cuda-9.0/binKaTeX parse error: Expected '}', got 'EOF' at end of input: {PATH:+:PATH}}
$ export LD_LIBRARY_PATH=/usr/local/cuda-9.0/lib64KaTeX parse error: Expected '}', got 'EOF' at end of input: …LIBRARY_PATH:+:{LD_LIBRARY_PATH}}
$ export LD_LIBRARY_PATH=LD_LIBRARY_PATH:/usr/local/cuda-9.0/lib64/

检查:nvcc –version,如果显示下面的文子就说明安装成功了。

在这里插入图片描述

测试CUDA

$ cd ~/NVIDIA_CUDA-9.0_Samples/5_Simulations/fluidsGL
$ sudo make clean && make
$ ./fluidsGL

如果出现找不到libcufft,则使用如下指令安装,然后重启Terminal即可。当然可能你的缺失依赖可能和我的不一样
则需要按照你的缺失进行安装,方法都一样。

$ sudo apt-get install libcufft9.1

出现如下则代表cuda安装成功。

在这里插入图片描述

安装cudnn7.1

在这里插入图片描述
下载cudnn-9.0-linux-x64-v7.1.tgz

1.解压文件

$ tar -zxvf cudnn-9.0-linux-x64-v7.1.tgz

2.把文件移到cuda目录下

$ sudo cp -P cuda/lib64/libcudnn* /usr/local/cuda-9.0/lib64/
$ sudo cp cuda/include/cudnn.h /usr/local/cuda-9.0/include/

3.给文件读取的权限

$ sudo chmod a+r /usr/local/cuda-9.0/include/cudnn.h /usr/local/cuda/lib64/libcudnn*

至此cudnn配置完成。

安装tensorflow

pip install tensorflow-gpu==1.5

PS:之前安装的是1.8,装完之后CUDNN和CUDA总是出错,改成1.5就完美运行,原因未知,望诸位能答疑解惑……

测试安装成功与否

安装完成之后终端进入python输入

Python 3.6rc1 (default, Apr 15 2018, 21:51:34)
[GCC 7.3.0] on linux2
Type “help”, “copyright”, “credits” or “license” for more information.

from tensorflow.python.client import device_lib
device_lib.list_local_devices()

如果出现下图所示则安装成功,可以看到GPU已经被列出来。

[name: “/device:CPU:0”
device_type: “CPU”
memory_limit: 268435456
locality {
}
incarnation: 8754742150252655686
, name: “/device:GPU:0”
device_type: “GPU”
memory_limit: 1276313600
locality {
bus_id: 1
links {
}
}
incarnation: 17576461877219777618
physical_device_desc: “device: 0, name: GeForce GTX 1050, pci bus id: 0000:01:00.0, compute capability: 6.1”
]

至此tensorflow1.5 完成安装

参考链接:
https://blog.csdn.net/sinat_40276791/article/details/80403784

https://blossomnoodles.github.io/cnBlogs/2018/04/30/Ubuntu18.04-Tensorlow-install.html
https://blog.csdn.net/aiolia86/article/details/80342240

猜你喜欢

转载自blog.csdn.net/weixin_43367430/article/details/88253539