This is going to be a tutorial on how to install tensorflow 1.8.0 GPU version. We will also be installing CUDA 9.2 and cuDNN 7.1.4 along with the GPU version of tensorflow 1.8.0. At the time of writing this blog post, the latest version of tensorflow is 1.8.0.This tutorial is for building tensorflow from source. If you want to use the official pre-built pip package instead, I recommend another post, How to install Tensorflow 1.7.0 using official pip package.
Tensorflow is an open source software library developed and used by Google that is fairly common among students, researchers, and developers for deep learning applications such as neural networks. It has both the CPU as well as GPU version available and although the CPU version works quite well, realistically, if you are going for deep learning, you will need GPU. In order to use the GPU version of TensorFlow, you will need an NVIDIA GPU with a compute capability > 3.0. While it is technically possible to install tensorflow GPU version in a virtual machine, you cannot access the full power of your GPU via a virtual machine. So, I recommend doing a fresh install of Ubuntu if you don’t have Ubuntu before starting with the tutorial.
Tensorflow 1.8 with CUDA 9.2 and cuDNN 7.1.4 performs up to 37% faster when compared to earlier versions of Tensorflow. View full results here.
There must be 64-bit python installed tensorflow does not work on 32-bit python installation.
Step 1: Update and Upgrade your system:
sudo apt-get update sudo apt-get upgrade
Step 2: Verify You Have a CUDA-Capable GPU:
lspci | grep -i nvidia
Note GPU model. eg. GeForce 840M
If you do not see any settings, update the PCI hardware database that Linux maintains by entering update-pciids (generally found in /sbin) at the command line and rerun the previous lspci command.
If your graphics card is from NVIDIA then goto http://developer.nvidia.com/cuda-gpus and verify if listed in CUDA enabled gpu list.
Note down its Compute Capability. eg. GeForce 840M 5.0
Step 3: Verify You Have a Supported Version of Linux:
To determine which distribution and release number you’re running, type the following at the command line:
uname -m && cat /etc/*release
The x86_64 line indicates you are running on a 64-bit system which is supported by cuda 9.1
Step 4: Install Dependencies:
Required to compile from source:
sudo apt-get install build-essential sudo apt-get install cmake git unzip zip sudo apt-get install python2.7-dev python3.5-dev python3.6-dev pylint
Step 5: Install linux kernel header:
Goto terminal and type:
uname -r
You can get like “4.10.0-42-generic”. Note down linux kernel version.
To install linux header supported by your linux kernel do following:
sudo apt-get install linux-headers-$(uname -r)
Step 6: Download the NVIDIA CUDA 9.2:
Go to https://developer.nvidia.com/cuda-downloads and download Installer for Linux Ubuntu 16.04 x86_64 deb[network]. I highly recommend network installer to get updated gpu driver supported by your linux kernel.
For, direct download
wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/cuda-repo-ubuntu1604_9.2.88-1_amd64.deb
Installation Instructions:
remove previous cuda installation:
sudo apt-get purge nvidia* sudo apt-get autoremove sudo apt-get autoclean sudo rm -rf /usr/local/cuda*
Install cuda :
sudo apt-key adv --fetch-keys http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/7fa2af80.pub sudo dpkg -i cuda-repo-ubuntu1604_9.2.88-1_amd64.deb sudo apt-get update sudo apt-get install cuda-9.2
Step 7: Reboot the system to load the NVIDIA drivers.
Step 8: Go to terminal and type:
nano ~/.bashrc
In the end of the file, add:
export PATH=/usr/local/cuda-9.2/bin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda-9.2/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
ctrl+x then y to save and exit
source ~/.bashrc sudo ldconfig nvidia-smi
Check driver version probably Driver Version: 396.26
(not likely) If you got nvidia-smi is not found then you have unsupported linux kernel installed. Comment your linux kernel version noted in step 5.
Step 9: Install cuDNN 7.1.4:
Goto https://developer.nvidia.com/cudnn and download Login and agreement required
After login and accepting agreement.
Download the following:
cuDNN v7.1.4 Runtime Library for Ubuntu16.04 (Deb)
cuDNN v7.1.4 Developer Library for Ubuntu16.04 (Deb)
cuDNN v7.1.4 Code Samples and User Guide for Ubuntu16.04 (Deb)
Goto downloaded folder and in terminal perform following:
sudo dpkg -i libcudnn7_7.1.4.18-1+cuda9.2_amd64.deb sudo dpkg -i libcudnn7-dev_7.1.4.18-1+cuda9.2_amd64.deb sudo dpkg -i libcudnn7-doc_7.1.4.18-1+cuda9.2_amd64.deb
Verifying cuDNN installation:
cp -r /usr/src/cudnn_samples_v7/ $HOME cd $HOME/cudnn_samples_v7/mnistCUDNN make clean && make ./mnistCUDNN
If cuDNN is properly installed and running on your Linux system, you will see a message similar to the following:
Test passed!
Step 10: Install NCCL 2.2.12:
NVIDIA Collective Communications Library (NCCL) implements multi-GPU and multi-node collective communication primitives that are performance optimized for NVIDIA GPUs
Go to https://developer.nvidia.com/nccl and attend survey to download Nvidia NCCL.
Download following after completing survey.
Download NCCL v2.2.12, for CUDA 9.2 -> NCCL 2.2.12 O/S agnostic and CUDA 9.2
Goto downloaded folder and in terminal perform following:
tar -xf nccl_2.2.12-1+cuda9.2_x86_64.txz cd nccl_2.2.12-1+cuda9.2_x86_64 sudo cp -R * /usr/local/cuda-9.2/targets/x86_64-linux/ sudo ldconfig
Step 11: Install Dependencies
libcupti (required)
sudo apt-get install libcupti-dev echo 'export LD_LIBRARY_PATH=/usr/local/cuda/extras/CUPTI/lib64:$LD_LIBRARY_PATH' >> ~/.bashrc
Bazel (required)
sudo apt-get install openjdk-8-jdk wget https://github.com/bazelbuild/bazel/releases/download/0.13.1/bazel_0.13.1-linux-x86_64.deb sudo dpkg -i bazel_0.13.1-linux-x86_64.deb
To install these packages for Python 2.7, issue the following command:
sudo apt-get install python-numpy python-dev python-pip python-wheel
To install these packages for Python 3.n, issue the following command:
sudo apt-get install python3-numpy python3-dev python3-pip python3-wheel
Step 12: Configure Tensorflow from source:
Reload environment variables
source ~/.bashrc sudo ldconfig
Start the process of building TensorFlow by downloading latest tensorflow 1.8.0 .
git clone https://github.com/tensorflow/tensorflow.git cd tensorflow git pull git checkout r1.8 ./configure
Give python path in
Please specify the location of python. [Default is /usr/bin/python]: /usr/bin/python3
Press enter two times
Do you wish to build TensorFlow with jemalloc as malloc support? [Y/n]: Y Do you wish to build TensorFlow with Google Cloud Platform support? [Y/n]: Y Do you wish to build TensorFlow with Hadoop File System support? [Y/n]: Y Do you wish to build TensorFlow with Amazon S3 File System support? [Y/n]: Y Do you wish to build TensorFlow with Apache Kafka Platform support? [Y/n]: Y Do you wish to build TensorFlow with XLA JIT support? [y/N]: N Do you wish to build TensorFlow with GDR support? [y/N]: N Do you wish to build TensorFlow with VERBS support? [y/N]: N Do you wish to build TensorFlow with OpenCL SYCL support? [y/N]: N Do you wish to build TensorFlow with CUDA support? [y/N]: Y Please specify the CUDA SDK version you want to use, e.g. 7.0. [Leave empty to default to CUDA 9.0]: 9.2 Please specify the location where CUDA 9.2 toolkit is installed. Refer to README.md for more details. [Default is /usr/local/cuda]: /usr/local/cuda-9.2 Please specify the cuDNN version you want to use. [Leave empty to default to cuDNN 7.0]: 7.1.4 Please specify the location where cuDNN 7 library is installed. Refer to README.md for more details. [Default is /usr/local/cuda-9.2]: /usr/lib/x86_64-linux-gnu Do you wish to build TensorFlow with TensorRT support? [y/N]: N Please specify the NCCL version you want to use. [Leave empty to default to NCCL 1.3]: 2.2 Please specify the location where NCCL 2 library is installed. Refer to README.md for more details. [Default is /usr/local/cuda-9.2]: /usr/local/cuda-9.2/targets/x86_64-linux
Now we need compute capability which we have noted at step 1 eg. 5.0
Please note that each additional compute capability significantly increases your build time and binary size. [Default is: 5.0] 5.0
Do you want to use clang as CUDA compiler? [y/N]: N Please specify which gcc should be used by nvcc as the host compiler. [Default is /usr/bin/gcc]: /usr/bin/gcc Do you wish to build TensorFlow with MPI support? [y/N]: N Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is -march=native]: -march=native Would you like to interactively configure ./WORKSPACE for Android builds? [y/N]:N
Configuration finished
Step 13: Build Tensorflow using bazel
The next step in the process to install tensorflow GPU version will be to build tensorflow using bazel. This process takes a fairly long time.
To build a pip package for TensorFlow you would typically invoke the following command:
bazel build --config=opt //tensorflow/tools/pip_package:build_pip_package
Note:-
add "--config=mkl" if you want Intel MKL support for newer intel cpu for faster training on cpu add "--config=monolithic" if you want static monolithic build (try this if build failed)
This process will take a lot of time. It may take 1 - 2 hours or maybe even more.
The bazel build command builds a script named build_pip_package. Running this script as follows will build a .whl file within the tensorflow_pkg directory:
To build whl file issue following command:
bazel-bin/tensorflow/tools/pip_package/build_pip_package tensorflow_pkg
Activate your virtual environment here if you use.
To install tensorflow with pip:
cd tensorflow_pkg
for python 2:
pip2 install tensorflow*.whl
for python 3:
pip3 install tensorflow*.whl
Step 14: Verify Tensorflow installation
Run in terminal
python
import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))
If the system outputs the following, then you are ready to begin writing TensorFlow programs:
Hello, TensorFlow!
Success! You have now successfully installed tensorflow 1.8.0 on your machine. If you are on Windows OS, you might want to check out our other post here, How to install Tensorflow 1.7.0 GPU with CUDA 9.1 and cuDNN 7.1.2 for Python 3 on Windows OS. Cheers!!
For prebuilt wheel with optimization for AVX2 and cuda 9.2, cudnn 7.1.4, compute capability 5.0 go to this link .
Nice guide thank you.
Using Ubuntu 16.04 I also needed to add a PPA for Python3.6-dev.
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update
Thank you ! Sol for information.
Great ‘How to’!
It helps me so much!
Thanks Leonardo.
Thank you very much.. a very nice guide.
For my specific case, 3 libraries were not found during tensorflow bazel command.
I had to do: sudo ln -s /usr/local/cuda-9.2/lib64/libcurand.so.9.2 /usr/lib/libcurand.so.9.2
Thankyou for useful information Aditya.
Hey,
I installed a fresh copy of Ubuntu 16.04 and I have an Nvidia 860M GPU. The current graphics card driver shows as - 396.26. My 860M has 640 cuda cores with Maxwell architecture.
My kernel version is 4.13.0-43-generic
I am getting the ‘nvidia-smi is not found’ error at Step - 8.
Could you please help me out?
Restart your pc and select advanced option in boot menu. Then select kernel 4.10 then start from first step. If it worked then reply me.