Ubuntu 16.04 安装 TensorFlow(GPU支持)
http://www.linuxidc.com/Linux/2017-04/143115.htm
本文记录Ubuntu 16.04安装Tensorflow步骤,也包括怎么从源码编译安装Tensorflow。
要想安装Tensorflow GPU版本,你需要有一个新一点的Nvidia显卡。
Tensorflow CPU版本的安装
1
2
|
$
sudo
apt
-
get
install
python
-
pip
python
-
dev
python
-
virtualenv
# python 2.7
$
sudo
apt
-
get
install
python3
-
pip
python3
-
dev
python3
-
virtualenv
# python 3.4+
|
使用虚拟环境(可选):Python虚拟环境(pyvenv、virtualenv)
1
2
|
$
virtualenv
--
system
-
site
-
packages
~
/
tensorflow
$
source
~
/
tensorflow
/
bin
/
activate
|
1
2
3
4
5
6
7
8
|
# Ubuntu/Linux 64-bit, CPU only, Python 2.7
$
export
TF_BINARY_URL
=
https
:
/
/
storage
.googleapis
.com
/
tensorflow
/
linux
/
cpu
/
tensorflow
-
0.11.0rc1
-
cp27
-
none
-
linux_x86_64
.whl
# Ubuntu/Linux 64-bit, CPU only, Python 3.4
$
export
TF_BINARY_URL
=
https
:
/
/
storage
.googleapis
.com
/
tensorflow
/
linux
/
cpu
/
tensorflow
-
0.11.0rc1
-
cp34
-
cp34m
-
linux_x86_64
.whl
# Ubuntu/Linux 64-bit, CPU only, Python 3.5
$
export
TF_BINARY_URL
=
https
:
/
/
storage
.googleapis
.com
/
tensorflow
/
linux
/
cpu
/
tensorflow
-
0.11.0rc1
-
cp35
-
cp35m
-
linux_x86_64
.whl
|
安装Tensorflow:
1
2
3
4
5
|
# Python 2
$
sudo
pip
install
--
upgrade
$TF_BINARY_URL
# Python 3
$
sudo
pip3
install
--
upgrade
$TF_BINARY_URL
|
如果要升级Tensorflow,替换新版本的TF_BINARY_URL。https://www.tensorflow.org
编译安装Tensorflow(GPU支持)
安装NVidia显卡驱动,你可以在Ubuntu内置的附加驱动中安装。
1
2
|
$
sudo
add
-
apt
-
repository
ppa
:
graphics
-
drivers
/
ppa
$
sudo
apt
update
|
安装CUDA:
1
2
3
4
5
6
|
#下载安装:
#https://developer.nvidia.com/cuda-toolkit
$
sudo
sh
cuda_8
.
0.44_linux.run
--
override
# 安装位置: /usr/local/cuda
# 默认仓库中的版本较旧
#$ sudo apt install nvidia-cuda-toolkit nvidia-cuda-dev # 安装位置: /usr
|
安装CudNN V5:https://developer.nvidia.com/cudnn
1
2
3
4
5
|
# 下载CudNN 5.1 for Cuda 8.0
$
sudo
tar
-
xzvf
cudnn
-
8.0
-
linux
-
x64
-
v5
.
1.tgz
$
sudo
cp
cuda
/
include
/
cudnn
.
h
/
usr
/
local
/
cuda
/
include
$
sudo
cp
cuda
/
lib64
/
libcudnn*
/
usr
/
local
/
cuda
/
lib64
$
sudo
chmod
a
+
r
/
usr
/
local
/
cuda
/
include
/
cudnn
.
h
/
usr
/
local
/
cuda
/
lib64
/
libcudnn*
|
在~/.bashrc文件中添加环境变量:
1
2
|
export
LD_LIBRARY_PATH
=
"$LD_LIBRARY_PATH:/usr/local/cuda/lib64:/usr/local/cuda/extras/CUPTI/lib64"
export
CUDA_HOME
=
/
usr
/
local
/
cuda
|
使环境变量生效:
1
|
$
source
~
/
.bashrc
|
下载tensorflow源码:
1
2
|
$
cd
~
$
git
clone
https
:
/
/
github
.com
/
tensorflow
/
tensorflow
|
安装一些编译和依赖工具:
1
|
$
sudo
apt
-
get
install
default
-
jdk
python
-
dev
python3
-
dev
python
-
numpy
python3
-
numpy
build
-
essential
python
-
pip
python3
-
pip
python
-
virtualenv
swig
python
-
wheel
libcurl3
-
dev
|
安装Bazel:
1
2
3
4
5
|
$
echo
"deb [arch=amd64] http://storage.googleapis.com/bazel-apt stable jdk1.8"
|
sudo
tee
/
etc
/
apt
/
sources
.list
.d
/
bazel
.list
$
curl
https
:
/
/
storage
.googleapis
.com
/
bazel
-
apt
/
doc
/
apt
-
key
.pub
.gpg
|
sudo
apt
-
key
add
-
$
sudo
apt
-
get
update
$
sudo
apt
-
get
install
bazel
$
sudo
apt
-
get
upgrade
bazel
|
配置编译选项:
1
2
|
$
cd
~
/
tensorflow
$
.
/
configure
|
需要输入Python路径,默认是 /usr/bin/python。如果你使用Python3,输入:/usr/bin/python3.5。
输入Python模块路径,默认是/usr/local/lib/python2.7/dist-packages。如果你使用Python3,输入:/usr/local/lib/python3.5/dist-packages。
输入Cuda SDK版本和Cudnn版本:8.0、5.1.5。
配置完成,输入如下信息:
1
2
|
INFO
:
All
external
dependencies
fetched
successfully
.
Configuration
finished
|
编译tensorflow:
1
2
3
|
$
bazel
build
-
c
opt
--
config
=
cuda
# GPU支持
# CPU支持
#$ bazel build -c opt
|
构建pip包:
1
|
$
bazel
-
bin
/
tensorflow
/
tools
/
pip_package
/
build_pip_package
/
tmp
/
tensorflow_pkg
|
安装pip包:
1
2
|
$
sudo
pip
install
/
tmp
/
tensorflow_pkg
/
tensorflow
# python2
$
sudo
pip3
install
/
tmp
/
tensorflow_pkg
/
tensorflow
# python3
|
参考:
- https://www.tensorflow.org/versions/r0.11/get_started/os_setup.html#download-and-setup
- tensorflow源代码:https://github.com/tensorflow/tensorflow
本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-04/143115.htm