Ubuntu下如何安装TensorFlow
2016年06月26日 15:29:40
标签:ubuntu (http://so.csdn.net/so/search/s.do?q=ubuntu&t=blog)
tensorflow (http://so.csdn.net/so/search/s.do?q=tensorflow&t=blog)
jupyter (http://so.csdn.net/so/search/s.do?q=jupyter&t=blog)
anaconda (http://so.csdn.net/so/search/s.do?q=anaconda&t=blog)
本文目录
• 引言
• 基于Anaconda的tensorflow安装
• 1 下载linux版本的Anaconda安装包
• 2 安装Anaconda
• 利用anaconda安装tensorflow
• 1 建立一个 conda 计算环境
• 2 激活环境使用 conda 安装 TensorFlow
• 3 安装tensorflow
• 4 如何在jupyter中使用tensorflow
• 总结
• 利用Docker安装tensorflow
• 1 安装docker
• 2 创建tensorflow的image
• 安装过程中的一些小问题记录
1.引言
TensorFlow = Tensor(向量)+Flow(流)=”张量在网络图中流动”!
主要参考: 
[1] Tensorflow官网Tutorials (https://www.tensorflow.org/versions/r0.9/get_started/index.html)
[2] 极客学院对Tensorflow官网教程的翻译 (http://wiki.jikexueyuan.com/project/tensorflow-zh/)
官方提供了5种安装tensorflow的方法:
• Pip install: Install TensorFlow on your machine, possibly upgrading previously installed 
Python packages. May impact existing Python programs on your machine.
• Virtualenv install: Install TensorFlow in its own directory, not impacting any existing Python 
programs on your machine.
• Anaconda install: Install TensorFlow in its own environment for those running the 
•
Anaconda Python distribution. Does not impact existing Python programs on your machine.
Docker install: Run TensorFlow in a Docker container isolated from all other  programs on 
your machine.
• Installing from sources: 
Install TensorFlow by building a pip wheel that you then install 
using pip.
几点说明: 
1. 由于在使用Tensorflow时,可以直接python调用,所以,这里决定使用python学习tensorflow 
2. 使用python,jupyter notebook是一个不可或缺的工具,所以,本文还将记录如何在ubuntu下安装
jupyter 
3. 这里主要安装python2.7版本的相关组件
2. 基于Anaconda的tensorflow安装
2.1 下载linux版本的Anaconda安装包
下载地址:https://www.continuum.io/downloads (https://www.continuum.io/downloads)
这里选择64位linux的Python 2.7版本 
点击下载,下载完成后,得到Anaconda2-4.0.0-Linux-x86_64.sh安装文件
2.2 安装Anaconda
• 打开terminal,输入如下命令,然后回车 
bash /home/tingting/Downloads/Anaconda2-4.0.0-Linux-x86_64.sh 
这里的 /home/tingting/Downloads/ 是存放 Anaconda2-4.0.0-Linux-x86_64.sh 的路径
• 阅读license,一步步回车阅读(出现more时通过回车往下看) 
• 输入yes,表示接受license 
• 设置安装路径,这里使用默认安装路径,直接输入回车即可 
•
开始自动安装过程 
• 确认是否将Anaconda的安装路径添加到环境变量中,输入yes 
关于这里一定要注意:
如果在安装过程中,该步没有选择yes,那么Anaconda的安装路径不会被添加到环境变量中,安装
结束后会出现如下信息:
Do you wish the installer to prepend the Anaconda2 install location 
to PATH in your /home/tingting/.bashrc ? [yes|no] 
[no] >>> 
You may wish to edit your .bashrc or prepend the Anaconda2 install location: 
$ export PATH=/home/tingting/anaconda2/bin:$PATH
Thank you for installing Anaconda2!
当时也没有注意这个信息,没有管,结果安装完anaconda后,发现根本不能使用,才注意到
这条信息,原来,Anaconda的bin路径并没有被添加到PAHT环境变量中,所以需要在命令行
中输入如下命令: 
export PATH=/home/tingting/anaconda2/bin:$PATH
即将anaconda的bin路径添加到环境变量PATH中
• 安装完成 
这里可以看到,notebooks和一些python包都被成功安装了,但对于环境变量的更改,必须要新打开一个
terminal才能生效,否则,相关的指令都不会被认识 
•
打开新的terminal,输入 Jupyter notebook ,发现jupyter
被成功安装了 
•
打开浏览器:http://localhost:8888/tree (http://localhost:8888/tree)(可以看到安装了python 2的
kernel) 
3. 利用anaconda安装tensorflow
3.1 建立一个 conda 计算环境
• Create a conda environment called tensorflow:
1
conda create -n tensorflow python
=
2.7
3.2 激活环境,使用 conda 安装 TensorFlow
• Activate the environment and use pip to install TensorFlow inside it.
1
source activate tensorflow
3.3 安装tensorflow
• 安装
在终端或cmd中输入以下命令搜索当前可用的tensorflow版本
$ anaconda search -t conda tensorflow 
Using Anaconda API: https://api.anaconda.org
Run 'anaconda show ' to get more details:
Packages:
     Name                      |  Version | Package Types   | Platforms       
     ------------------------- |   ------ | --------------- | --------------- 
     HCC/tensorflow            |    1.0.0 | conda           | linux-64        
     HCC/tensorflow-cpucompat  |    1.0.0 | conda           | linux-64        
     HCC/tensorflow-fma        |    1.0.0 | conda           | linux-64        
     SentientPrime/tensorflow  |    0.6.0 | conda           | osx-64          
                                          : TensorFlow helps the tensors flow 
     acellera/tensorflow-cuda  |   0.12.1 | conda           | linux-64        
     anaconda/tensorflow       |    1.0.1 | conda           | linux-64        
     anaconda/tensorflow-gpu   |    1.0.1 | conda           | linux-64        
     conda-forge/tensorflow    |    1.0.0 | conda           | linux-64, win-64, osx-64 
                                          : TensorFlow helps the tensors flow 
     creditx/tensorflow        |    0.9.0 | conda           | linux-64        
                                          : TensorFlow helps the tensors flow 
     derickl/tensorflow        |   0.12.1 | conda           | osx-64          
     dhirschfeld/tensorflow    | 0.12.0rc0 | conda           | win-64          
     dseuss/tensorflow         |          | conda           | osx-64          
     guyanhua/tensorflow       |    1.0.0 | conda           | linux-64        
     ijstokes/tensorflow       | 2017.03.03.1349 | conda, ipynb    | linux-64        
     jjh_cio_testing/tensorflow |    1.0.1 | conda           | linux-64        
     jjh_cio_testing/tensorflow-gpu |    1.0.1 | conda           | linux-64        
     jjh_ppc64le/tensorflow    |    1.0.1 | conda           | linux-ppc64le   
     jjh_ppc64le/tensorflow-gpu |    1.0.1 | conda           | linux-ppc64le   
     jjhelmus/tensorflow       | 0.12.0rc0 | conda, pypi     | linux-64, osx-64 
                                          : TensorFlow helps the tensors flow 
     jjhelmus/tensorflow-gpu   |    1.0.1 | conda           | linux-64        
     kevin-keraudren/tensorflow |    0.9.0 | conda           | linux-64        
     lcls-rhel7/tensorflow     |   0.12.1 | conda           | linux-64        
     marta-sd/tensorflow       |    1.0.1 | conda           | linux-64        
                                          : TensorFlow helps the tensors flow 
     memex/tensorflow          |    0.5.0 | conda           | linux-64, osx-64 
                                          : TensorFlow helps the tensors flow 
     mhworth/tensorflow        |    0.7.1 | conda           | osx-64          
                                          : TensorFlow helps the tensors flow 
     miovision/tensorflow      | 0.10.0.gpu | conda           | linux-64, osx-64 
     msarahan/tensorflow       | 1.0.0rc2 | conda           | linux-64        
     mutirri/tensorflow        | 0.10.0rc0 | conda           | linux-64        
     mwojcikowski/tensorflow   |    1.0.1 | conda           | linux-64        
     rdonnelly/tensorflow      |    0.9.0 | conda           | linux-64        
     rdonnellyr/r-tensorflow   |    0.4.0 | conda           | osx-64          
     test_org_002/tensorflow   | 0.10.0rc0 | conda           |                 
Found 32 packages
选择一个较新的CPU或GPU版本,如jjh_cio_testing/tensorflow-gpu的1.0.1版本,输入如下命令查询安装命令
$ anaconda show jjh_cio_testing/tensorflow-gpu 
Using Anaconda API: https://api.anaconda.org
Name:    tensorflow-gpu 
Summary:
Access: public
Package Types:  conda 
Versions:
   + 1.0.1 
To install this package with conda run:
     conda install --channel https://conda.anaconda.org/jjh_cio_testing tensorflow-gpu
使用最后一行的提示命令进行安装
$ conda install --channel https://conda.anaconda.org/jjh_cio_testing tensorflow-gpu
Fetching package metadata ............. 
Solving package specifications: . 
Package plan for installation in environment /home/will/anaconda2:
The following packages will be SUPERSEDED by a higher-priority channel:
    tensorflow-gpu: 1.0.1-py27_4 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free --> 1.0.1-py27_4 jjh_cio_te
sting
Proceed ([y]/n)? 
conda会自动检测安装此版本的Tensorflow所依赖的库,如果你的Anaconda缺少这些依赖库,会提示你安装。因为我之前已经安装过
了,所以这里只提示我安装Tensorflow。输入y并回车之后等待安装结束即可
• 可以选择次高版本的Tensorflow安装,因为最新版本可能清华 TUNA的仓库镜像库没有及时更新,而官方更新连接总是失败,我最
开始选择了jjhelmus/tensorflow-gpu的1.0.1版本,其他依赖库清华 TUNA的仓库镜像有资源,而到最后jjhelmus/tensorflow-
gpu版本的Tensorflow安装包总是下载不下来,尝试20多次之后换了一个1.0.0的版本,终于顺利安装成功
• 测试tensorflow是否安装成功 
(1) 激活conda环境 
(2)进入python 
(3)import tensorflow 
整个过程比较顺利,tensorflow被成功import了 
3.4 如何在jupyter中使用tensorflow
(1) 出现了问题 
安装如上方法安装了jupyter和tensorflow,结果,利用jupyter无法使用tensorflow 
也没有查到特别有针对性的解决这个问题的资料,好像是因为jupyter的安装路径和tensorflow的路径之间的
问题,具体的现在还不是很清楚
(2)如何解决? 
应该是在conda的tensorflow环境下没有jupyter,它无法使用之前anaconda安装的jupyter,那么,简单粗暴
的方法就是在当前的conda-tensorflow环境下,再安装一次jupyter:
• 打开terminal
• 激活conda tensorflow环境: source activate tensorflow
• 安装notebook: conda install ipython
•
• 安装完成,仍在conda tensorflow的环境下,输入
安装jupyter:
conda install 
jupyter
jupyter notebook
打开,
http://localhost:8889/tree#
(http://localhost:8889/tree#)
• import tensorflow ,发现tensorflow
可以使用了
http://stackoverflow.com/questions/35771285/using-tensorflow-through-jupyter-
参考:
python
(http://stackoverflow.com/questions/35771285/using-tensorflow-through-jupyter-python-3)
3-
(3)两个jupyter
•
在正常的ternimal中打开的是之前在安装anacodna时安装的jupyter,在正常的terminal下 查看
jupyter的安装路径 
which ipyhon
•
它在anaconda的路径下的bin文件夹中
在conda tensorflow环境下打开的jupyter是在conda tensorflow环境下新安装的tensorflow,与上面
的jupyter不同 
它在conda的env下的tensorflow的bin文件夹下
• 从下图可以想起地看到,两个jupyter的安装路径完成不同