Install TensorFlow gpu on ubuntu18.04


I wrote this post to simplify the installation process of gpu version of TensorFlow on Ubuntu 18.04. I have described installation steps below and hope that it will be helpful.

NVIDIA driver

Check the nvidia driver is installed by running the command ‘nvidia-smi’

Normally, the command will be installed as part of the driver and expected to get the result below.

If you are not able to see the result like above picture, it may be due to either nvidia driver is not installed or nvidia-smi utility is missing. I had to run the below to install the nvidia driver and was successful with installation.

Only follow below steps if you can’t get information like above.

  1. Run the below code to uninstall the existing nvidia driver and restart.
sudo apt-get purge nvidia*  
sudo reboot

2. Run the below code to install the nvidia driver 390 at the time of this post writing.

sudo apt-get install nvidia-390

After these steps, you have installed the nvidia driver and ‘nvidia-smi’ commad should give you some information about the GPU.

CUDA 9.0 installation

  1. Download cuda9.0 installation file from nvidia site.

2. Change the permission of the downloaded file

sudo chmod +x cuda_9.0.176_384.81_linux.run

3. The run the file

./cuda_9.0.176_384.81_linux.run

Accept the terms and condition and make sure you type ‘no’ to the accelerated graphics driver installation question. Also make sure type ‘yes’ to symoblic link question.

CUDANN 7.2.1 installation

Next, Download the file from nvidia site.

Please make sure you have downloaded the first option cuDNN v7.2.1 Library for Linux.

Once you have downloaded, follow the below steps:

  1. Extract and copy
sudo tar -xzvf 
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
sudo chmod a+r /usr/local/cuda/lib64/libcudnn*

2) Copy below commands to ~/.bashrc

export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/local/cuda/lib64:/usr/local/cuda/extras/CUPTI/lib64"
export CUDA_HOME=/usr/local/cuda
export PATH="$PATH:/usr/local/cuda/bin"

3) Run the below command to source

source ~/.bashrc

Miniconda installation

At this stage we have installed all the required software for tensorflow-gpu version to work. Now, we need to install the miniconda software that will enable us to create python virtual environments.

wget https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh
bash Miniconda3-latest-Linux-x86_64.sh

During the installation, it will be asked to append the path to ~/.bahrc and type yes to append them.

Tensorflow-gpu installation

Finally, we are ready to install the gpu version of TensorFlow. We need to run below steps to get there:

  1. Create virtual environment.
conda create -n tensorflow_gpu

2. Get into the environment

source activate tensorflow_gpu

3. Install tensorflow-gpu

pip install tensorflow-gpu

4. Check the installation by create a file called ‘test.py’ and add below commands to it

import tensorflow as tf   

hello = tf.constant('Hello there!')

print(sess.run(hello))

5. Run ‘python test. py’ and you should be able to get ‘Hello there!’ message that is he indication gpu version TensorFlow has been installed successfully. Otherwise, Installation failed.

You can see similar write up in Github repo.

If you like my write up, follow me on Github, Linkedin, and/or Medium profile.

Source: Deep Learning on Medium