This guide explains how to install all the required tools to start using
TensorFlow GPU version 1.2 on a fresh Ubuntu 16.04.2 install. If you are new to Linux or Deep-Learning or just looking for a simple guide to get TensorFlow working, this is the guide for you.
Why do we need to install TensorFlow from source? Why can’t we just do
pip3 install --upgrade tensorflow-gpu
?In the TensorFlow Performance Guide, it is recommended to
Build and install from source.
Quoting the guide, “Building from source with compiler optimizations for the target hardware and ensuring the latest CUDA platform and cuDNN libraries are installed results in the highest performing installs.” I haven’t measured the performance gains myself, I just trust the TensorFlow site.
The following major tools are installed in this guide:
NVIDIA Graphics Drivers v375.66
NVIDIA CUDA Toolkit v8.0
NVIDIA cuDNN v5.1
Miniconda 64-bit
Python3 v3.5.3
Tensorflow v1.2
Disclaimer: I am assuming you have a system with a CUDA-Enabled NVIDIA GPU installed. Also assuming you just completed installing the OS. I tested everything myself but the results may vary on your system. I am not responsible for any unforeseen consequences you may face by following the steps below. Follow at your own risk. Please leave a comment if something doesn’t work, i’ll try to help.
1. Install OS Updates
Let’s get started by install all the OS updates. Open a terminal window by typing Ctrl+Alt+T.
$ sudo apt update
$ sudo apt upgrade
$ sudo reboot
2. Install NVIDIA Graphics Drivers
2.1 Add the graphics drivers repository
$ sudo apt-add-repository ppa:graphics-drivers/ppa
$ sudo apt update
2.2 Update the graphics driver version
Open System Settings and click on Software & Updates.
Navigate to Additional Drivers tab. Under NVIDIA Corporation, select the 375.66 radio button. Click Apply Changes.
Caution: Do not select 381.22 or other driver version. It is not compatible with CUDA toolkit that we are going to install.
Please reboot the system once the installation is complete.
2.3 Fix some errors!
After installing the graphics driver, when I tried installing some other software like screenfetch, I got this error message:
/sbin/ldconfig.real: /usr/lib/nvidia-375/libEGL.so.1 is not a symbolic link
/sbin/ldconfig.real: /usr/lib32/nvidia-375/libEGL.so.1 is not a symbolic link
Execute the following lines to avoid the error shown above.
$ cd /usr/lib/nvidia-375
$ sudo mv libEGL.so.1 libEGL.so.1.org
$ sudo ln -s libEGL.so.375.66 libEGL.so.1
$ cd /usr/lib32/nvidia-375
$ sudo mv libEGL.so.1 libEGL.so.1.org
$ sudo ln -s libEGL.so.375.66 libEGL.so.1
3. Install NVIDIA CUDA Toolkit and cuDNN
Let’s go to NVIDIA Developer site. There are many interesting things here. You may want to look around and see what is new.
3.1 Download the tools
please go to CUDA Toolkit page.
Once you click download on the CUDA Toolkit page, you will be asked to make several choices regarding your OS=Linux, Architecture=x86_64, Distribution=Ubuntu, Version=16.04, and Installer type=deb(local).
Please go to cuDNN page. Here you need a developer login to continue. If you don’t have one, it is free to join.
After you login, please select cuDNN v5.1 (Jan 20, 2017), for CUDA 8.0 and cuDNN v5.1 Library for Linux as shown above.
3.2 Install CUDA Toolkit
Download from the developer site as shown above or by clicking this Direct link to the toolkit from NVIDIA Developer Site
go to the directory where the CUDA Toolkit is downloaded.
$ cd ~/Downloads
Install the toolkit
$ sudo dpkg -i cuda-repo-ubuntu1604–8–0-local-ga2_8.0.61–1_amd64.deb
$ sudo apt update
$ sudo apt install cuda
3.3 Add the CUDA Toolkit to $PATH
Open ~/.bashrc
in your favorite editor.
$ gedit ~/.bashrc
Add these three export statements to the end of ~/.bashrc
export PATH=/usr/local/cuda/bin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda/lib64:/usr/local/cuda/extras/CUPTI/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
export CUDA_HOME=/usr/local/cuda
Below is my ~/.bashrc
Save the file and quit the editor.
Reload the ~/.bashrc
file.
$ source ~/.bashrc
3.4 Install cuDNN
Please download the cuDNN v5.1 library as shown above in section 3.1
go to the directory where the cuDNN is downloaded.
$ cd ~/Downloads
Extract the contents
$ tar -xzvf cudnn-8.0-linux-x64-v5.1.tgz
Copy files from cuda folder to /usr/local/cuda
$ sudo cp -P cuda/include/cudnn.h /usr/local/cuda/include
$ sudo cp -P cuda/lib64/libcudnn* /usr/local/cuda/lib64
Change permissions for the copied files
$ sudo chmod a+r /usr/local/cuda/include/cudnn.h /usr/local/cuda/lib64/libcudnn*
3.5 Test the CUDA Toolkit and cuDNN installation
Let's check some version numbers before continuing.
$ nvcc --version
$ nvidia-smi
Let’s make a Software directory in the home folder. We will also download TensorFlow in this directory later.
$ mkdir ~/Software
$ cd ~/Software
Copy a set of test scripts to local directory
$ cuda-install-samples-8.0.sh ./
$ cd NVIDIA_CUDA-8.0_Samples/
Build them
$ make
Run them to make sure they work. My output is included below.
$ cd bin/x86_64/linux/release
$ ./deviceQuery
$ ./bandwidthTest
4. Build TensorFlow GPU 1.2 from Sources
4.1 Install Dependencies
$ sudo apt install openjdk-8-jdk
$ sudo apt install git
$ sudo apt install build-essential curl libcurl3-dev
$ sudo apt install python3-numpy python3-dev python3-pip python3-wheel
4.2 Install Bazel
Add Bazel distribution URI as a package source
$ echo "deb [arch=amd64] http://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.list
$ curl https://bazel.build/bazel-release.pub.gpg | sudo apt-key add -
Install and update Bazel
$ sudo apt update
$ sudo apt install bazel
$ sudo apt upgrade bazel
4.3 Build TensorFlow from Sources
$ cd ~/Software
Clone TensorFlow Git repository
$ git clone https://github.com/tensorflow/tensorflow
$ cd tensorflow
Move back to release 1.2
$ git checkout r1.2
Configure the install.
$ ./configure
Use the exact responses as shown below in Bold.
Please specify the location of python. [Default is /usr/bin/python]: /usr/bin/python3.5Found possible Python library paths:
/usr/lib/python3/dist-packages
/usr/local/lib/python3.5/dist-packages
Please input the desired Python library path to use. Default is [/usr/lib/python3/dist-packages]
/usr/local/lib/python3.5/dist-packagesDo you wish to build TensorFlow with MKL support? [y/N] n
No MKL support will be enabled for TensorFlowPlease specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is -march=native]: no input just press enterDo you wish to use jemalloc as the malloc implementation? [Y/n] y
jemalloc enabledDo you wish to build TensorFlow with Google Cloud Platform support? [y/N] n
No Google Cloud Platform support will be enabled for TensorFlowDo you wish to build TensorFlow with Hadoop File System support? [y/N] n
No Hadoop File System support will be enabled for TensorFlowDo you wish to build TensorFlow with the XLA just-in-time compiler (experimental)? [y/N] n
No XLA JIT support will be enabled for TensorFlowDo you wish to build TensorFlow with VERBS support? [y/N] n
No VERBS support will be enabled for TensorFlowDo you wish to build TensorFlow with OpenCL support? [y/N] n
No OpenCL support will be enabled for TensorFlowDo you wish to build TensorFlow with CUDA support? [y/N] y
CUDA support will be enabled for TensorFlowDo you want to use clang as CUDA compiler? [y/N] n
nvcc will be used as CUDA compilerPlease specify the CUDA SDK version you want to use, e.g. 7.0. [Leave empty to use system default]: 8.0Please specify the location where CUDA 8.0 toolkit is installed. Refer to README.md for more details. [Default is /usr/local/cuda]: no input just press enterPlease specify which gcc should be used by nvcc as the host compiler. [Default is /usr/bin/gcc]: no input just press enterPlease specify the cuDNN version you want to use. [Leave empty to use system default]: 5Please specify the location where cuDNN 5 library is installed. Refer to README.md for more details. [Default is /usr/local/cuda]: no input just press enterPlease specify a list of comma-separated Cuda compute capabilities you want to build with.
You can find the compute capability of your device at: https://developer.nvidia.com/cuda-gpus.
Please note that each additional compute capability significantly increases your build time and binary size.
[Default is: "3.5,5.2"]: 3.0
Extracting Bazel installation...
.....
INFO: Starting clean (this may take a while). Consider using --async if the clean takes more than several minutes.
Configuration finished
Build
$ bazel build --config=opt --config=cuda //tensorflow/tools/pip_package:build_pip_package
$ bazel-bin/tensorflow/tools/pip_package/build_pip_package ~/Software/tensorflow_pkg
The TensorFlow GPU 1.2 installer will be saved in ~/Software/tensorflow_pkg
5. Install Miniconda
5.1 Download Miniconda
Download Miniconda from the conda.io site. Select the Python 3.6 64-bit Installer for linux.
5.2 Install Miniconda
Use the command below to install Miniconda.
$ bash Miniconda3-latest-Linux-x86_64.sh
select yes when asked to add miniconda to the path by modifying ~/.bashrc
5.3 Create a new conda environment
Open a new terminal window by typing Ctrl+Alt+T. Create a new conda environment called tf1.2 with python 3.5 and numpy.
$ conda create -n tf1.2 python=3.5 numpy
6. Install TensorFlow GPU v1.2
6.1 Install TensorFlow
Activate the newly created conda environment.
$ source activate tf1.2
Install TensorFlow using the installer we built from sources.
(tf1.2)$ pip install ~/Software/tensorflow_pkg/tensorflow-1.2.0-cp35-cp35m-linux_x86_64.whl
6.2 Test the TensorFlow installation
Launch python
(tf1.2)$ python
Use the following python code to test if TensorFlow is installed properly. I included my output below.
>>> import tensorflow as tf
>>> hello = tf.constant('Hello, TensorFlow!')
>>> sess = tf.Session()
>>> print(sess.run(hello))
Updates!
Anaconda installation has been replaced by Miniconda.
Why replace Anaconda with Miniconda?
Anaconda includes conda, over 150 python packages and their dependencies. As part of these dependencies Anaconda installs glib
package that is incompatible with our Ubuntu version causing the following error while using some applications.
GLib-GIO-Message: Using the 'memory' GSettings backend. Your settings will not be saved or shared with other applications.
[[ ! ]] GLib-GIO-Message: Using the 'memory' GSettings backend. Your settings will not be saved or shared with other applications.
Since Anaconda will be our default python installation, we get to see this error more often than we like.
Miniconda is a smaller, basic version of Anaconda that has only conda and python. So, this avoids causing the above error.
References:
- NVIDIA CUDA Toolkit Install Guide by NVIDIA
- TensorFlow Install Guide from TensorFlow