安装Tensorflow(Linux ubuntu)

目录目录1下载TensorFlow安装文件源代码1tensorflow 源码地址2tensorflow whl文件地址2安装依赖首先安装bazel1安装JDK81ubuntu14042ubuntu15103安装protobuftensorflow依赖1默认安装步骤需root权限2修改安装路径非root用户需要修改安装路径3还需要重新配置Python否则可能会在import tensorflow时候

目录

1、下载TensorFlow安装文件(源代码)

来源渠道有以下

(1)tensorflow 源码地址

#输入以下命令获取
$ git clone --recurse-submodules https://github.com/tensorflow/tensorflow

(2)tensorflow whl文件地址

如tensorflow0.8分布式whl文件地址为:tensorflow-0.8.0rc0-cp27-none-linux_x86_64.whl

#输入以下命令获取
$ wget http://ci.tensorflow.org/job/tensorflow-master-gpu_pip/lastSuccessfulBuild/artifact/pip_test/whl/tensorflow-0.8.0rc0-cp27-none-linux_x86_64.whl

或者查看官方下载安装说明http://tensorflow.org/get_started/os_setup.md

2、安装依赖,首先安装bazel

\#安装相关的包:
$ sudo apt-get install pkg-config zip g++ zlib1g-dev unzip
$ sudo apt-get install python-numpy swig python-dev

在bazel之前需要安装JDK8,官方安装教程

(1)安装JDK8:

<1>ubuntu(14.04)
$ sudo add-apt-repository ppa:webupd8team/java
$ sudo apt-get update
$ sudo apt-get install oracle-java8-installer 
<2>ubuntu(15.10)

安装OpenJDK 8:

$ sudo apt-get install openjdk-8-jdk

下载bazel安装文件:
文件可以到以下地址获取:https://github.com/bazelbuild/bazel/releases

然后使用以下命令安装

$ chmod +x bazel-version-installer-os.sh
$ ./bazel-version-installer-os.sh --user

其中version需要替换下载的版本,即获取的安装文件为.sh。使用 --user后,bazel将会被安装在$HOME/bin,以及.bazelrc 会保存在 $HOME/.bazelrc

设置运行环境:执行文件bazel将被安装在$HOME/bin路径下,所以加入PATH路径中:

$ export PATH="$PATH:$HOME/bin" 

3、安装protobuf(tensorflow依赖)

下载地址为:https://github.com/google/protobuf

下载解压(如果是压缩包)后,执行

$ ./autogen.sh

将会生成configure文件
接下来有两种安装配置

(1)默认安装步骤(需root权限):

$ ./configure
$ make
$ make check
$ make install

usr/local/bin,usr/local/lib,usr/local/include
是也系统默认路径之一,所以到这一步就可以使用protobuf了

(2)修改安装路径(非root用户需要修改安装路径):

protobuf默认安装在 /usr/local 目录
你可以修改安装目录通过 ./configure –prefix=命令
虽然我是root用户但觉得默认安装过于分散,所以统一安装在/usr/local/protobuf下

$./configure --prefix=/usr/local/protobuf
$ make
$ make check
$ make install

到此步还没有安装完毕,在/etc/profile 或者用户目录 ~/.bash_profile
添加下面内容

####### add protobuf lib path ########
#(动态库搜索路径) 程序加载运行期间查找动态链接库时指定除了系统默认路径之外的其他路径
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/protobuf/lib/
#(静态库搜索路径) 程序编译期间查找动态链接库时指定查找共享库的路径
export LIBRARY_PATH=$LIBRARY_PATH:/usr/local/protobuf/lib/
#执行程序搜索路径
export PATH=$PATH:/usr/local/protobuf/bin/
#c程序头文件搜索路径
export C_INCLUDE_PATH=$C_INCLUDE_PATH:/usr/local/protobuf/include/
#c++程序头文件搜索路径
export CPLUS_INCLUDE_PATH=$CPLUS_INCLUDE_PATH:/usr/local/protobuf/include/
#pkg-config 路径
export PKG_CONFIG_PATH=/usr/local/protobuf/lib/pkgconfig/
######################################

(3)还需要重新配置Python,否则可能会在import tensorflow时候出现No module named google.protobuf,其中不知道是否一定与ldconfig有关系,http://blog.csdn.net/littlestream9527/article/details/38734001里介绍了ldconfig的功能

$ ldconfig
$ cd ~~/protobuf/python    ##进入解压protobuf后中的python文件夹,里面会有buile文件夹与setup.py文件等
$ python setup.py build
$ python setup.py install
$ cd /usr/local/lib/python2.7/site-packages   #进入tensorflow想关联的python的目录下修改
$ chmod -R 755 *

(4)查看是否安装成功

$ protoc –version
libprotoc 2.6.1

显示安装版本为2.6.1

4、源码安装tensorflow

(1)下载源码文件:

$ git clone --recurse-submodules https://github.com/tensorflow/tensorflow

其中–recurse-submodules是获取tensorflow所需要的protobuf库(加该选项运行成功的话,可以不单独安装protobuf)
进入tensorflow文件跟目录中

$ ./configure
Please specify the location of python. [Default is /usr/bin/python]:

(2)开始安装

安装过程中需要不断输入y/n进行配置确认,如需安装支持GPU版本,需要安装Cuda7.0,cuDNN6.5(据说是写死的版本,对于Cuda7.5,cuDNN7.0没有支持,但可以手动配置)
下载和安装Cuda Toolkit
下载和安装cuDNN

$ tar xvzf cudnn-6.5-linux-x64-v2.tgz
$ sudo cp cudnn-6.5-linux-x64-v2/cudnn.h /usr/local/cuda/include
$ sudo cp cudnn-6.5-linux-x64-v2/libcudnn* /usr/local/cuda/lib64
$ sudo chmod a+r /usr/local/cuda/lib64/libcudnn*
添加cuda路径
PATH=$PATH:/usr/local/cuda (CUDA安装路径)
添加lib路径
LD_LIBRARY_PATH=$LA_LIBRARY_PATH:/usr/local/cuda/lib64

tensorflow 总体安装过程如下

$ ./configure
Please specify the location of python. [Default is /usr/bin/python]:
Do you wish to build TensorFlow with GPU support? [y/N] y
GPU support will be enabled for TensorFlow

Please specify the Cuda SDK version you want to use, e.g. 7.0. [Leave
empty to use system default]: 7.5

Please specify the location where CUDA 7.5 toolkit is installed. Refer to
README.md for more details. [default is: /usr/local/cuda]: /usr/local/cuda

Please specify the Cudnn version you want to use. [Leave empty to use system
default]: 4.0.4

Please specify the location where the cuDNN 4.0.4 library is installed. Refer to
README.md for more details. [default is: /usr/local/cuda]: /usr/local/cudnn-r4-rc/

Please specify a list of comma-separated Cuda compute capabilities you want to
build with. You can find the compute capability of your device at: 
https://developer.nvidia.com/cuda-gpus.
Please note that each additional compute capability significantly increases your
build time and binary size. [Default is: \"3.5,5.2\"]: 3.5

Setting up Cuda include
Setting up Cuda lib64
Setting up Cuda bin
Setting up Cuda nvvm
Configuration finished

至此,完成tensorflow安装

5、whl方式安装tensorflow

(1)下载whl安装文件

官方whl文件下载方法

CPU版本:
wget https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.8.0-cp27-none-linux_x86_64.whl
GPU版本,需要CUDA toolkit 7.5 和 CuDNN v4.:
wget https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.8.0-cp27-none-linux_x86_64.whl

其他whl文件下载方法

#以按照以下方法直接下载0.8版本
$ wget http://ci.tensorflow.org/job/tensorflow-master-gpu_pip/lastSuccessfulBuild/artifact/pip_test/whl/tensorflow-0.8.0rc0-cp27-none-linux_x86_64.whl

(2)或者使用bazel从源码创建

$ bazel build -c opt //tensorflow/tools/pip_package:build_pip_package

若建立支持GPU的安装

$ bazel build -c opt --config=cuda //tensorflow/tools/pip_package:build_pip_package

生成whl至/tmp/tensorflow_pkg

$ bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg

(3)最后使用与平台匹配的whl安装tensorflow

$ pip install tensorflow-0.8.0rc0-py2-none-linux_x86_64.whl

6、为项目开发对tensorflow进行相关设置

  • 对相关所有文件建立与系统目录的link
$ bazel build -c opt //tensorflow/tools/pip_package:build_pip_package
  • 若建立GPU支持:
$ bazel build -c opt --config=cuda //tensorflow/tools/pip_package:build_pip_package
  • 然后建立链接:
$ mkdir _python_build
$ cd _python_build
$ ln -s ../bazel-bin/tensorflow/tools/pip_package/build_pip_package.runfiles/* .
$ ln -s ../tensorflow/tools/pip_package/* .
$ python setup.py develop

7、测试安装成功

(1)检查tensorflow是否可以正常使用

$ python
...
>>> import tensorflow as tf
>>> hello = tf.constant('Hello, TensorFlow!')
>>> sess = tf.Session()
>>> print(sess.run(hello))
Hello, TensorFlow!
>>> a = tf.constant(10)
>>> b = tf.constant(32)
>>> print(sess.run(a + b))
42
>>>

(2)测试是否支持GPU

$ bazel build -c opt --config=cuda //tensorflow/cc:tutorials_example_trainer

$ bazel-bin/tensorflow/cc/tutorials_example_trainer --use_gpu
# Lots of output. This tutorial iteratively calculates the major eigenvalue of
# a 2x2 matrix, on GPU. The last few lines look like this.
000009/000005 lambda = 2.000000 x = [0.894427 -0.447214] y = [1.788854 -0.894427]
000006/000001 lambda = 2.000000 x = [0.894427 -0.447214] y = [1.788854 -0.894427]
000009/000009 lambda = 2.000000 x = [0.894427 -0.447214] y = [1.788854 -0.894427]

8、安装中常见的问题:

(1)GPU有关的问题

ImportError: libcudart.so.7.0: cannot open shared object file: No such file or directory

确认GPU是否妥善安装,版本是否正确

(2)Protobuf库有关的问题

tensorflow pip包依赖protobuf pip包的版本为version 3.0.0b2,Protobuf的pip包可以从PyPI下载(当运行pip install protobuf),或者可以升级版本,通过以下命令(python2.7)

# Ubuntu/Linux 64-bit:
$ pip install --upgrade https://storage.googleapis.com/tensorflow/linux/cpu/protobuf-3.0.0b2.post2-cp27-none-linux_x86_64.whl

可以修复以下问题:

[libprotobuf ERROR google/protobuf/src/google/protobuf/io/coded_stream.cc:207] A
protocol message was rejected because it was too big (more than 67108864 bytes).
To increase the limit (or to disable these warnings), see
CodedInputStream::SetTotalBytesLimit() in google/protobuf/io/coded_stream.h.

(3)pip安装问题:

<1>Cannot import name ‘descriptor’
ImportError: Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/tensorflow/core/framework/graph_pb2.py", line 6, in <module>
    from google.protobuf import descriptor as _descriptor
ImportError: cannot import name 'descriptor'

如果在升级tensorflow时遇到以上问题时,尝试卸载tensorflow和protobuf ,并重装rensorflow(同样需要合适的protobuf)

<2>Can’t find setup.py

在pip install是遇见如下错误:

...
IOError: [Errno 2] No such file or directory: '/tmp/pip-o6Tpui-build/setup.py'

解决方法为:

pip install --upgrade pip
<3>SSLError: SSL_VERIFY_FAILED

当从URL中使用pip install时候,出现以下问题:

...
SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed

解决办法: 使用curl或者wget下载wheel到本地,再进行安装



[1] : https://github.com/tensorflow/tensorflow/
[2] : http://ci.tensorflow.org/buildStatus/icon?job=tensorflow-master
[3] : http://tensorflow.org/get_started/os_setup.md
[4] : http://bazel.io/docs/install.html
[5] : https://github.com/bazelbuild/bazel/releases
[6] : https://github.com/google/protobuf
[7] : https://developer.nvidia.com/cuda-downloads
[8] : https://developer.nvidia.com/cudnn


作者: 林海山波
原文链接:https://blog.csdn.net/lenbow/article/details/51203526

  • 发表于 2019-11-26 12:43
  • 阅读 ( 35 )
  • 分类:tensorflow

0 条评论

请先 登录 后评论
不写代码的码农
林海山波 _csdn

0 篇文章

作家榜 »

  1. AI君 10 文章
  2. Tzung-Wen Liau 0 文章
  3. blairan 0 文章
  4. rookie 0 文章
  5. 陈凯 0 文章
  6. huanxue 0 文章
  7. admin 0 文章
  8. Lzs1998_csdn 0 文章