Tutorial : Installing Tensorflow in Ubuntu 16.04

This tutorial will try to guide you to install and deploy tensorflow and all of it's required package in Ubuntu 16.04.
if you encounter any trouble, you could also refer to the installation video below

Step 1 - Installing Anaconda


To get tensorflow running, the first thing we need is to install python because tensorflow is running above python. This time we will install python 3.6 together with many scientific computing library that has been nicely packed in anaconda package.

To install anaconda, first open your terminal by pressing CTRL+ALTt+T or by typing terminal on the dash menu.
after the terminal is opened enter command below.

wget https://repo.continuum.io/archive/Anaconda3-5.0.0.1-Linux-x86_64.sh

after the download is finished, run the anaconda installer script by entering the command below.

sh Anaconda3-5.0.0.1-Linux-x86_64.sh

after the install finished, test whether the python is running without problem by simply entering

python3

in the terminal. If it's running then step 1 is finished.

Step 2 and 3 - Installing and Checking NVIDIA Driver


By default, the ubuntu uses noveau drive which is not compatible for running tensorflow. In order to run tensorflow correctly we must firs install the NVIDIA open source driver.
first, we must make sure there is no old NVIDIA driver installed in the system by removing it using

sudo apt-get purge nvidia*

next we will add the apt source for the nvidia driver by entering the command below in the terminal

sudo add-apt-repository ppa:graphics-drivers
sudo apt-get update

finally we could install the driver by using

sudo apt-get install nvidia-375

make sure the driver you install is at least version 375 as the tensorflow need a minimal version of 375 to run correctly.after the install is finished, reboot the computer and run the command below to check if the driver is succesfully installed

lsmod | grep nvidia

if this command output some text, it means the driver is succesfully installed. Finally apply the driver change by going to "software & updates" in the dash menu

  • click on the "additional drivers" tab
  • check the nvidia driver (in this case, version 375)
  • click on apply changes and wait for it to complete
  • reboot one more tiem

and the driver installation is finished.

Step 4 - Installing NVIDIA CUDA


to run tensorflow which is based on cuda computation, we need to install NVIDIA CUDA library simply by entering the below command in the terminal
sudo apt-get install nvidia-cuda-dev

when the installation finished, the library is ready to use

Step 5 - Installing libcupti and CUDA Toolkit


libcupti is another library that is required to run tensorflow, similar to installing nvidia-cuda it is straightforward and easy to install just by running
sudo apt-get install libcupti-dev

after installing libcupti, we also need to install CUDA toolkit that is provided by NVIDIA. For the CUDA toolkit we could download the toolkit by running the command below

wget https://developer.nvidia.com/compute/cuda/8.0/Prod2/local_installers/cuda_8.0.61_375.26_linux-run

after the download finished, run the installer script using

sudo sh cuda_8.0.61_375.26_linux-run

follow the displayed instructiona nd finish the installation.

Step 6 - Installing cuDNN


cuDNN is additional library to improve the calculation for deep neural network model. To install cuDNN, download the library package with command below.
wget gpu.hicc.cs.kumamoto-u.ac.jp/shared/cudnn.tgz

next, extract the content of the package to a folder by running

tar xvf cudnn.tgz

finally copy all the content of the extracted folder to the cuda folder

sudo cp cuda/include/* /usr/local/cuda-8.0/include/
sudo cp cuda/lib64/* /usr/local/cuda-8.0/lib64/

and it is finished.

Step 7 - Installing Tensorflow Package


finally we could install the tensorflow package in the python. First, make sure that pip for python3 is installed by running

sudo apt-get install python3-pip

if the pip is already installed we could proceed to install tensorflow package by running

pip3 install tensorflow-gpu

we choose the gpu version of the tensorflow so it could utilize the graphic card for the calculation process. when the installation is finished, we could check if the tensorflow running correctly by entering below command

python3
>> import tensorflow as tf
>> 

if there are no error displayed then the tensorflow is successfully installed and is ready to be used.

Extra - Library Error


there is a case when the tensorflow failed to detect the path of CUDA library, in that case when we try to import tensorflow the error below will appear.

Screenshot-from-2017-10-05-19-08-37

to fix this problem we must add the path manually to the ~/.bashrc file. first we open the file by running.

sudo gedit ~/.bashrc

scroll to the last line and add these line.

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda/lib64/

and save it. after that open python again in the different terminal and try to import the tensorflow again.