1. TensorFlow安装教程(Ubuntu系统)

2. 新建Linux用户

caffe框架使用的是python2,,TensorFlow使用的是python3,为了防止和caffe的环境干扰,新建一个用户

2.1. 添加用户

命令

sudo adduser tfuser

连续输入两次密码后一直回车;

2.2. 给用户添加管理员权限

在对应文件中添加下面一行,命令:

sudo chmod 740 /etc/sudoers
sudo vim /etc/sudoers

找到# Allow members of group sudo to execute any command
在下面添加一行,如下

tfuser ALL=(ALL:ALL) ALL

2.3. 切换到新用户

命令

su tfuser

详细操作过程和输出的内容:

# 创建用户
mashangxue123@cnn:~$ adduser tfuser
adduser: Only root may add a user or group to the system.
mashangxue123@cnn:~$ sudo adduser tfuser
[sudo] password for mashangxue123:
Adding user `tfuser' ...
Adding new group `tfuser' (1003) ...
Adding new user `tfuser' (1003) with group `tfuser' ...
Creating home directory `/home/tfuser' ...
Copying files from `/etc/skel' ...
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Changing the user information for tfuser
Enter the new value, or press ENTER for the default
    Full Name []:
    Room Number []:
    Work Phone []:
    Home Phone []:
    Other []:
Is the information correct? [Y/n] y
mashangxue123@cnn:~$
# 切换用户
mashangxue123@cnn:~$ su tfuser
Password:
tfuser@cnn:/home/mashangxue123$

3. 安装python环境

建议使用Anaconda安装python,它附带了一大批数据科学包和依赖项,后期不用再手动安装,可以节省很多时间。

3.1. 下载

地址: https://www.anaconda.com/download

找到对应Linux平台的版本,下载python3的最新版本,

3.2. 安装

使用bash命令安装下载的文件

bash ./Anaconda3-5.1.0-Linux-x86_64.sh

一直按回车键开始安装,注意!安装进行一会后,自动暂停了,提示下面内容,不要回车,输入yes,这是自动添加python环境变量,否则就只能自己手动添加了。

Do you wish the installer to prepend the Anaconda3 install location
to PATH in your /home/tfuser/.bashrc ? [yes|no]
[no] >>>

详细安装内容:

tfuser@cnn:~/Downloads$ ls
Anaconda3-5.1.0-Linux-x86_64.sh
tfuser@cnn:~/Downloads$ bash ./Anaconda3-5.1.0-Linux-x86_64.sh
Welcome to Anaconda3 5.1.0
.......

installation finished.
Do you wish the installer to prepend the Anaconda3 install location
to PATH in your /home/tfuser/.bashrc ? [yes|no]
[no] >>> yes

Appending source /home/tfuser/anaconda3/bin/activate to /home/tfuser/.bashrc
A backup will be made to: /home/tfuser/.bashrc-anaconda3.bak
Visual Studio Code License: https://code.visualstudio.com/license

Do you wish to proceed with the installation of Microsoft VSCode? [yes|no]
>>> no

3.3. 配置环境变量

如果在上面安装过程中选择了不在bash shell环境变量中加入Anaconda路径PATH, 那么你需要在/home/tfuser/.bashrc文件中加入下面一行

export PATH="/home/tfuser/anaconda3/bin:$PATH"

3.4. 激活python环境

重启终端,或者使用source命令,可以看到python环境已经更新,目前是python3.6版本。

tfuser@cnn:~$ source ~/.bashrc
tfuser@cnn:~$ python
Python 3.6.4 |Anaconda, Inc.| (default, Jan 16 2018, 18:10:19)
[GCC 7.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

4. 安装TensorFlow

5. 查看TensorFlow最新版本

查看TensorFlow版本地址:
https://tensorflow.google.cn/install/install_linux#the_url_of_the_tensorflow_python_package

离线安装下载地址:
https://pypi.python.org/pypi/tensorflow-gpu/1.7.0

发布介绍地址 : https://github.com/tensorflow/tensorflow/tags

详细可以参考官方安装路径:

https://tensorflow.google.cn/install/install_linux#installing_with_anaconda

5.1. 安装方法

TensorFlow分CPU和GPU两个版本,如果你的电脑有NVIDIA的GPU,最好安装GPU版本。

如果安装了GPU版本,后面还要安装CUDA和cuDNN,如果你只是想了解一下TensorFlow,数据很少,那么安装CPU版本是最快的,安装完成后就能直接体验TensorFlow了。

方法1: 先升级pip,然后安装GPU版本tensorflow-gpu

# 先升级pip,否则可能会报错
pip install --upgrade pip

# GPU版本
pip install  tensorflow-gpu

# CPU版本
pip install  tensorflow

方法2: 使用pip安装我们下载好的whl文件

pip install --ignore-installed --upgrade  tensorflow_gpu-1.7.0-cp36-cp36m-linux_x86_64.whl

方法3: 直接指定地址安装,需要翻墙

pip install --ignore-installed --upgrade https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-1.7.0-cp36-cp36m-linux_x86_64.whl

方法4: 使用清华大学镜像加速

https://mirrors.tuna.tsinghua.edu.cn/help/tensorflow/

pip install  -i https://pypi.tuna.tsinghua.edu.cn/simple/     https://mirrors.tuna.tsinghua.edu.cn/tensorflow/linux/gpu/

最好联网安装,TensorFlow安装过程会自动安装大量的依赖包。详情可看下面的安装过程。

安装过程的完整输出:

cd Downloads/
tfuser@cnn:~/Downloads$ ls
Anaconda3-5.1.0-Linux-x86_64.sh
tensorflow_gpu-1.7.0-cp36-cp36m-linux_x86_64.whl
tensorflow_gpu-1.7.0-cp36-cp36m-manylinux1_x86_64.whl
tfuser@cnn:~/Downloads$ pip install tensorflow_gpu-1.7.0-cp36-cp36m-m
Collecting tensorflow_gpu-1.7.0-cp36-cp36m-m
  Could not find a version that satisfies the requirement tensorflow_gpu-1.7.0-cp36-cp36m-m (from versions: )
No matching distribution found for tensorflow_gpu-1.7.0-cp36-cp36m-m
^[You are using pip version 9.0.1, however version 9.0.3 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
tfuser@cnn:~/Downloads$ pip install tensorflow_gpu-1.7.0-cp36-cp36m-manylinux1_x86_64.whl 
Processing ./tensorflow_gpu-1.7.0-cp36-cp36m-manylinux1_x86_64.whl
Collecting gast>=0.2.0 (from tensorflow-gpu==1.7.0)
  Downloading gast-0.2.0.tar.gz
Collecting tensorboard<1.8.0,>=1.7.0 (from tensorflow-gpu==1.7.0)
  Downloading tensorboard-1.7.0-py3-none-any.whl (3.1MB)
    100% |████████████████████████████████| 3.1MB 80kB/s 
Collecting grpcio>=1.8.6 (from tensorflow-gpu==1.7.0)
  Downloading grpcio-1.11.0-cp36-cp36m-manylinux1_x86_64.whl (8.8MB)
    100% |████████████████████████████████| 8.8MB 76kB/s 
Collecting termcolor>=1.1.0 (from tensorflow-gpu==1.7.0)
  Downloading termcolor-1.1.0.tar.gz
Collecting protobuf>=3.4.0 (from tensorflow-gpu==1.7.0)
  Downloading protobuf-3.5.2.post1-cp36-cp36m-manylinux1_x86_64.whl (6.4MB)
    100% |████████████████████████████████| 6.4MB 78kB/s 
Collecting astor>=0.6.0 (from tensorflow-gpu==1.7.0)
  Downloading astor-0.6.2-py2.py3-none-any.whl
Requirement already satisfied: six>=1.10.0 in /home/tfuser/anaconda3/lib/python3.6/site-packages (from tensorflow-gpu==1.7.0)
Requirement already satisfied: wheel>=0.26 in /home/tfuser/anaconda3/lib/python3.6/site-packages (from tensorflow-gpu==1.7.0)
Collecting absl-py>=0.1.6 (from tensorflow-gpu==1.7.0)
  Downloading absl-py-0.1.13.tar.gz (80kB)
    100% |████████████████████████████████| 81kB 133kB/s 
Requirement already satisfied: numpy>=1.13.3 in /home/tfuser/anaconda3/lib/python3.6/site-packages (from tensorflow-gpu==1.7.0)
Collecting markdown>=2.6.8 (from tensorboard<1.8.0,>=1.7.0->tensorflow-gpu==1.7.0)
  Downloading Markdown-2.6.11-py2.py3-none-any.whl (78kB)
    100% |████████████████████████████████| 81kB 112kB/s 
Collecting bleach==1.5.0 (from tensorboard<1.8.0,>=1.7.0->tensorflow-gpu==1.7.0)
  Downloading bleach-1.5.0-py2.py3-none-any.whl
Collecting html5lib==0.9999999 (from tensorboard<1.8.0,>=1.7.0->tensorflow-gpu==1.7.0)
  Downloading html5lib-0.9999999.tar.gz (889kB)
    100% |████████████████████████████████| 890kB 138kB/s 
Requirement already satisfied: werkzeug>=0.11.10 in /home/tfuser/anaconda3/lib/python3.6/site-packages (from tensorboard<1.8.0,>=1.7.0->tensorflow-gpu==1.7.0)
Requirement already satisfied: setuptools in /home/tfuser/anaconda3/lib/python3.6/site-packages (from protobuf>=3.4.0->tensorflow-gpu==1.7.0)
Building wheels for collected packages: gast, termcolor, absl-py, html5lib
  Running setup.py bdist_wheel for gast ... done
  Stored in directory: /home/tfuser/.cache/pip/wheels/8e/fa/d6/77dd17d18ea23fd7b860e02623d27c1be451521af40dd4a13e
  Running setup.py bdist_wheel for termcolor ... done
  Stored in directory: /home/tfuser/.cache/pip/wheels/de/f7/bf/1bcac7bf30549e6a4957382e2ecab04c88e513117207067b03
  Running setup.py bdist_wheel for absl-py ... done
  Stored in directory: /home/tfuser/.cache/pip/wheels/76/f7/0c/88796d7212af59bb2f496b12267e0605f205170781e9b86479
  Running setup.py bdist_wheel for html5lib ... done
  Stored in directory: /home/tfuser/.cache/pip/wheels/6f/85/6c/56b8e1292c6214c4eb73b9dda50f53e8e977bf65989373c962
Successfully built gast termcolor absl-py html5lib
Installing collected packages: gast, markdown, html5lib, bleach, protobuf, tensorboard, grpcio, termcolor, astor, absl-py, tensorflow-gpu
  Found existing installation: html5lib 1.0.1
    Uninstalling html5lib-1.0.1:
      Successfully uninstalled html5lib-1.0.1
  Found existing installation: bleach 2.1.2
    Uninstalling bleach-2.1.2:
      Successfully uninstalled bleach-2.1.2
Successfully installed absl-py-0.1.13 astor-0.6.2 bleach-1.5.0 gast-0.2.0 grpcio-1.11.0 html5lib-0.9999999 markdown-2.6.11 protobuf-3.5.2.post1 tensorboard-1.7.0 tensorflow-gpu-1.7.0 termcolor-1.1.0
You are using pip version 9.0.1, however version 9.0.3 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
tfuser@cnn:~/Downloads$ ^C

6. 安装CUDA

你的TensorFlow需要哪个版本的CUDA,可以通过下面地址查看

https://github.com/tensorflow/tensorflow/tags

我们看到1.7的要求描述:CUDA可以使用8.0,cuDNN库可以使用6.0

tensorflow 1.7 可能是我们最后一次支持低于8.0的cuda版本。从tensorflow 1.8 版本开始,8.0 将是最低支持

tensorflow 1.7 可能是我们最后一次支持低于6.0的cudnn版本。从tensorflow 1.8 版本开始,6.0 将是最低支持

6.1. 下载

CUDA官方下载路径,https://developer.nvidia.com/cuda-toolkit-archive
考虑到可能显卡不支持太高版本,我们下载8.0的最新版本,

安装方法

sudo dpkg -i cuda-repo-ubuntu1604-8-0-local-ga2_8.0.61-1_amd64.deb
sudo apt-get update
sudo apt-get install cuda

cuDNN下载路径:(需要注册才能下载)

https://developer.nvidia.com/rdp/cudnn-download

6.2. 配置CUDA环境变量

如果GPU环境下加载tensorflow时,遇上找不到cuda库的问题时,很简单,需要配置LD_LIBRARY_PATH环境变量。

~/.bashrc中添加:

CUDA_HOME=/usr/local/cuda-8.0
export PATH=/usr/local/cuda-8.0/bin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda-8.0/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}

6.3. 测试是否安装成功

重启终端后输入:

nvcc -V

如果你能看到相应信息,说明CUDA已经配置成功

tfuser@cnn:~$ nvcc -V
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2016 NVIDIA Corporation
Built on Tue_Jan_10_13:22:03_CST_2017
Cuda compilation tools, release 8.0, V8.0.61

7. 安装cuDNN 加速库

cuDNN是cuda的深度神经网络包

7.1. 下载

官方下载链接网址 https://developer.nvidia.com/cudnn

这个软件需要注册成为用户后才能下载。点击“Download”后,点击“Join now”,完成注册。登录后下载。

7.2. 安装配置

解压cuDNN压缩文件,输入下面的命令复制cudnn.h文件和lib64文件夹下的链接库文件到CUDA文件夹相应的位置,通过复制的方式,共用CUDA的环境变量配置路径,不用再单独配置

sudo cp include/cudnn.h /usr/local/cuda/include/
sudo cp lib64/* /usr/local/cuda/lib64/

8. 验证TensorFlow是否正常运行

从终端中启动python,复制下面的代码回车运行.

代码:

import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))

详细操作过程:

tfuser@cnn:~$ python
Python 3.5.2 |Anaconda custom (64-bit)| (default, Jul  2 2016, 17:53:06)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> hello = tf.constant('Hello, TensorFlow!')
>>> sess = tf.Session()
>>> print(sess.run(hello))
b'Hello, TensorFlow!'

技术交流学习,请加QQ微信:631531977
目录