Install Tensorflow GPU (CUDA 10.0) on Ubuntu 18.04

This article is mainly used to record my own installation process. It is not recommended to use in a production environment. For your convenience, please use those mature methods.

 

Please make sure that the 64-bit version of python is installed. TensorFlow does not support the 32-bit version.

 

1,Update and Upgrade your system:

sudo apt-get update 
sudo apt-get upgrade

2,Verify You Have a CUDA-Capable GPU:

lspci | grep -i nvidia

Please verify on Nvidia official website.

3,Verify You Have a Supported Version of Linux:

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

4,Install Dependencies:

sudo apt-get install build-essential 
sudo apt-get install cmake git unzip zip
sudo apt-get install python-dev python3-dev python-pip python3-pip

5,Install linux kernel header:

sudo apt-get install linux-headers-$(uname -r)

6,Install CUDA 10.0

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 :

Recommended reference to Nvidia official installation method

Official method:Nvidia official website (recommended use deb(network))

 

For Ubuntu 18.04:

sudo apt-key adv --fetch-keys http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/7fa2af80.pub
echo "deb https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64 /" | sudo tee /etc/apt/sources.list.d/cuda.list

or

sudo apt-get update 
sudo apt-get -o Dpkg::Options::="--force-overwrite" install cuda-10-0 cuda-drivers

 

7,Reboot the system to load the NVIDIA drivers.

8,Open terminal

echo 'export PATH=/usr/local/cuda-10.0/bin${PATH:+:${PATH}}' >> ~/.bashrc
echo 'export LD_LIBRARY_PATH=/usr/local/cuda-10.0/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}' >> ~/.bashr
source ~/.bashrc
sudo ldconfig
nvidia-smi

Check driver version

Probably Driver Version:396.26

 

If you got nvidia-smi is not found then you have unsupported linux kernel installed.

 

You can check your cuda installation using following sample:

cuda-install-samples-10.0.sh ~
cd ~/NVIDIA_CUDA-10.0_Samples/5_Simulations/nbody
make
./nbody

9, Install cuDNN

Goto https://developer.nvidia.com/cudnn and download Login and agreement required.

I use this version: cuDNN v7.4.2 Library for Linux [ cuda 10.0]

Goto downloaded folder and in terminal perform following:

tar -xf cudnn-10.0-linux-x64-v7.4.2.20.tgz 
sudo cp -R cuda/include/* /usr/local/cuda-10.0/include
sudo cp -R cuda/lib64/* /usr/local/cuda-10.0/lib64

10,Install NCCL 2.3.5

Goto https://developer.nvidia.com/nccl/nccl-download and download Login and agreement required.

Goto downloaded folder and in terminal perform following:

tar -xf nccl_2.3.5-2+cuda10.0_x86_64.txz
cd nccl_2.3.5-2+cuda10.0_x86_64
sudo cp -R * /usr/local/cuda-10.0/targets/x86_64-linux/
sudo ldconfig

11,Install Dependencies

 

Use following if not in active virtual environment.

pip install -U --user pip six numpy wheel mock

pip3 install -U --user pip six numpy wheel mock
pip install -U --user keras_applications==1.0.5 --no-deps

pip3 install -U --user keras_applications==1.0.5 --no-deps
pip install -U --user keras_preprocessing==1.0.3 --no-deps

pip3 install -U --user keras_preprocessing==1.0.3 --no-deps

Use following if  in active virtual environment.

pip install -U pip six numpy wheel mock
pip install -U keras_applications==1.0.5 --no-deps
pip install -U keras_preprocessing==1.0.3 --no-deps
12,Configure Tensorflow from source:
Download bazel:
cd ~/
wget https://github.com/bazelbuild/bazel/releases/download/0.17.2/bazel-0.17.2-installer-linux-x86_64.sh
chmod +x bazel-0.17.2-installer-linux-x86_64.sh
./bazel-0.17.2-installer-linux-x86_64.sh --user
echo 'export PATH="$PATH:$HOME/bin"' >> ~/.bashrc

note:Only 0.17.2 version works in my enviorment.

Reload environment variables
source ~/.bashrc
sudo ldconfig
Start the process of building TensorFlow
cd ~/
git clone https://github.com/tensorflow/tensorflow.git
cd tensorflow
git checkout r1.12
./configure
Give python path in
Please specify the location of python. [Default is /usr/bin/python]: /usr/bin/python3
Do you wish to build TensorFlow with Apache Ignite support? [Y/n]: Y

Do you wish to build TensorFlow with XLA JIT support? [Y/n]: Y

Do you wish to build TensorFlow with OpenCL SYCL support? [y/N]: N

Do you wish to build TensorFlow with ROCm 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. [Leave empty to default to CUDA 9.0]: 10.0
Please specify the location where CUDA 10.0 toolkit is installed. Refer to README.md for more details. [Default is /usr/local/cuda]: /usr/local/cuda-10.0
Please specify the cuDNN version you want to use. [Leave empty to default to cuDNN 7]: 7.4.2
Please specify the location where cuDNN 7 library is installed. Refer to README.md for more details. [Default is /usr/local/cuda-10.0]: /usr/local/cuda-10.0
Do you wish to build TensorFlow with TensorRT support? [y/N]: N
Please specify the NCCL version you want to use. If NCCL 2.2 is not installed, then you can use version 1.3 that can be fetched automatically but it may have worse performance with multiple GPUs. [Default is 2.2]: 2.3.5
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

13, Build Tensorflow using bazel

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

 

bazel-bin/tensorflow/tools/pip_package/build_pip_package tensorflow_pkg
Install TensorFlow
cd tensorflow_pkg

Use following if  in active virtual environment.

pip install tensorflow*.whl
Or you need to creative a virtual enviorment

 

14,Verify Tensorflow installation

python

import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))

 

ReferenceHow to install Tensorflow GPU with CUDA 10.0 for python on Ubuntu