In this tutorial I will be going through the process of building TensorFlow 0.10 from sources for Ubuntu 14.04.  If you would prefer to use Ubuntu 16.04 please follow my other tutorial here.  I prefer to use Python 3 but I have included options for Python 2 as well.

In order to use TensorFlow with GPU support you must have a Nvidia graphic card with a minimum compute capability of 3.0.  You must also have the 352.63 NVidia drivers installed, this can easily be done from Ubuntu’s built in additional drivers.  If you experience any troubles booting linux, try disabling fast & safe boot in your bios and modifying your grub boot options to enable nomodeset.

Getting started I am going to assume you know some of the basics of using a terminal in Linux.


Install Required Packages

Open a terminal by pressing Ctrl + Alt + T
Paste each line one at a time (without the $) using Shift + Ctrl + V

$ sudo add-apt-repository ppa:webupd8team/java
$ sudo apt-get update
$ sudo apt-get install oracle-java8-installer git python-dev python3-dev build-essential python-pip python3-pip python-virtualenv swig python-wheel libcurl3-dev


Install Bazel

Instructions also on Bazel website

$ 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


Install Nvidia Toolkit 7.5 & CudNN

Skip if not installing with GPU support

To install the Nvidia Toolkit download .deb file from Nvidia website

$ cd ~/Downloads # or directory to where you downloaded file
$ sudo dpkg -i cuda-repo-ubuntu1404-7-5-local_7.5-18_amd64.deb
$ sudo apt-get update
$ sudo apt-get install cuda

This will install cuda into: /usr/local/cuda

To install CudNN download cudNN v5 for Cuda 7.5 from Nvidia website and extract into /usr/local/cuda via:

$ tar xvzf cudnn-7.5-linux-x64-v5.0-ga.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*

Then update your bash file:

$ gedit ~/.bashrc

This will open your bash file in a text editor which you will scroll to the bottom and add these lines:

export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/local/cuda/lib64"
export CUDA_HOME=/usr/local/cuda
export DYLD_LIBRARY_PATH="$DYLD_LIBRARY_PATH:$CUDA_HOME/lib"
export PATH="$CUDA_HOME/bin:$PATH"

Once you save and close the text file you can return to your original terminal and type this command to reload your .bashrc file:

$ source ~/.bashrc


Clone TensorFlow

$ cd ~
$ git clone https://github.com/tensorflow/tensorflow


Configure TensorFlow Installation

$ cd ~/tensorflow
$ ./configure

Use defaults by pressing enter for all except:

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

For Python 2 use default or If you wish to build for Python 3 enter:

$ /usr/bin/python3

Please input the desired Python library path to use.  Default is [/usr/local/lib/python2.7/dist-packages]:

For Python 2 use default or If you wish to build for Python 3 enter:

$ /usr/local/lib/python3.4/dist-packages

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 Cudnn version you want to use. [Leave empty to use system default]:

$ 5

You can find the compute capability of your device at: https://developer.nvidia.com/cuda-gpus

If all was done correctly you should see:

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


Build TensorFlow 

Warning Resource Intensive I recommend having at least 8GB of computer memory.

If you want to build TensorFlow with GPU support enter:

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

For CPU only enter:

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


Build & Install Pip Package

This will build the pip package required for installing TensorFlow in your /tmp/ folder

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

To Install Using Python 3

$ sudo pip3 install /tmp/tensorflow_pkg/tensorflow

# with no spaces after tensorflow hit tab before hitting enter to fill in blanks

For Python 2

$ sudo pip install /tmp/tensorflow_pkg/tensorflow

# with no spaces after tensorflow hit tab before hitting enter to fill in blanks


Test Your Installation

Close all terminals and open a new terminal.

$ python # or python3
$ import tensorflow as tf
$ sess = tf.InteractiveSession()
$ sess.close()

If you get a import or library error you can try opening a new terminal or restarting your computer.  TensorFlow also has instructions on how to do a basic test and a list of common installation problems.

There you have it, you should now have TensorFlow installed on your computer.  This tutorial was tested on a fresh install of Ubuntu 14.04 with a GeForce GTX 780 and a GTX 970m.

Advertisements