1、下载TensorFlow安装文件(源代码)
来源渠道有以下
(1)tensorflow
#输入以下命令获取$git clone --recurse-submodules https://github.com/tensorflow/tensorflow1
2
(2)tensorflow
#输入以下命令获取$wget http://ci.tensorflow.org/job/tensorflow-master-gpu_pip/lastSuccessfulBuild/artifact/pip_test/whl/tensorflow-0.8.0rc0-cp27-none-linux_x86_64.whl1
2
2、安装依赖,首先安装bazel
\#安装相关的包:$ sudo apt-get install pkg-config zip g++ zlib1g-dev unzip$ sudo apt-get install python-numpy swig python-dev1
2
3
在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-installer1
2
3
<2>ubuntu(15.10)
安装OpenJDK 8:
$ sudo apt-get install openjdk-8-jdk
然后使用以下命令安装
$ chmod +x bazel-version-installer-os.sh$ ./bazel-version-installer-os.sh --user1
2
其中version需要替换下载的版本,即获取的安装文件为.sh。使用 --user后,bazel将会被安装在$HOME/bin,以及.bazelrc 会保存在 $HOME/.bazelrc
设置运行环境:执行文件bazel将被安装在$HOME/bin路径下,所以加入PATH路径中:
$ export PATH='$PATH:$HOME/bin'1
3、安装protobuf(tensorflow依赖)
下载解压(如果是压缩包)后,执行
$./autogen.sh1
将会生成configure文件
接下来有两种安装配置
(1)默认安装步骤(需root权限):
$./configure$make$make check$make install1
2
3
4
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 install1
2
3
4
到此步还没有安装完毕,在/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/######################################1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
(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 *1
2
3
4
5
6
7
(4)查看是否安装成功$ protoc –version
libprotoc 2.6.1
显示安装版本为2.6.1
4、源码安装tensorflow
(1)下载源码文件:
$git clone --recurse-submodules https://github.com/tensorflow/tensorflow1
其中–recurse-submodules是获取tensorflow所需要的protobuf库(加该选项运行成功的话,可以不单独安装protobuf)
进入tensorflow文件跟目录中
$ ./configurePlease specify the location of python. [Default is /usr/bin/python]:1
2
(2)开始安装
安装过程中需要不断输入y/n进行配置确认,如需安装支持GPU版本,需要安装Cuda7.0,cuDNN6.5(据说是写死的版本,对于Cuda7.5,cuDNN7.0没有支持,但可以手动配置)
下载和安装
$ 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*1
2
3
4
添加cuda路径PATH=$PATH:/usr/local/cuda (CUDA安装路径)添加lib路径LD_LIBRARY_PATH=$LA_LIBRARY_PATH:/usr/local/cuda/lib641
2
3
4
5
tensorflow 总体安装过程如下
$ ./configurePlease specify the location of python. [Default is /usr/bin/python]:Do you wish to build TensorFlow with GPU support? [y/N] yGPU support will be enabled for TensorFlowPlease specify the Cuda SDK version you want to use, e.g. 7.0. [Leaveempty to use system default]: 7.5Please specify the location where CUDA 7.5 toolkit is installed. Refer toREADME.md for more details. [default is: /usr/local/cuda]: /usr/local/cudaPlease specify the Cudnn version you want to use. [Leave empty to use systemdefault]: 4.0.4Please specify the location where the cuDNN 4.0.4 library is installed. Refer toREADME.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 tobuild 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 yourbuild time and binary size. [Default is: \'3.5,5.2\']: 3.5Setting up Cuda includeSetting up Cuda lib64Setting up Cuda binSetting up Cuda nvvmConfiguration finished1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
至此,完成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.whlGPU版本,需要CUDA toolkit 7.5 和 CuDNN v4.:wget https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.8.0-cp27-none-linux_x86_64.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.whl1
2
(2)或者使用bazel从源码创建
$bazel build -c opt //tensorflow/tools/pip_package:build_pip_package1
若建立支持GPU的安装
$bazel build -c opt --config=cuda //tensorflow/tools/pip_package:build_pip_package1
生成whl至/tmp/tensorflow_pkg
$bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg1
(3)最后使用与平台匹配的whl安装tensorflow
$ pip install tensorflow-0.8.0rc0-py2-none-linux_x86_64.whl1
6、为项目开发对tensorflow进行相关设置对相关所有文件建立与系统目录的link
$bazel build -c opt //tensorflow/tools/pip_package:build_pip_package1若建立GPU支持:
$bazel build -c opt --config=cuda //tensorflow/tools/pip_package:build_pip_package1然后建立链接:
$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 develop1
2
3
4
5
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>>>1
2
3
4
5
6
7
8
9
10
11
12
(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]1
2
3
4
5
6
7
8
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包可以从
# Ubuntu/Linux 64-bit:$ pip install --upgrade https://storage.googleapis.com/tensorflow/linux/cpu/protobuf-3.0.0b2.post2-cp27-none-linux_x86_64.whl1
2
可以修复以下问题:
[libprotobuf ERROR google/protobuf/src/google/protobuf/io/coded_stream.cc:207] Aprotocol message was rejected because it was too big (more than 67108864 bytes).To increase the limit (or to disable these warnings), seeCodedInputStream::SetTotalBytesLimit() in google/protobuf/io/coded_stream.h.1
2
3
4
(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 from google.protobuf import descriptor as _descriptorImportError: cannot import name 'descriptor'1
2
3
4
如果在升级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'1
2
解决方法为:
pip install --upgrade pip1
<3>SSLError: SSL_VERIFY_FAILED
当从URL中使用pip install时候,出现以下问题:
...SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed2
解决办法: 使用curl或者wget下载wheel到本地,再进行安装