Tensorflow GPU installation made easy: Ubuntu Version | by Harveen Singh Chadha | Towards Data Science

Installing Tensorflow GPU on ubuntu is a challenge with the correct versions of cuda and cudnn. A year back, I wrote an article that discussed about installation of Tensorflow GPU with conda instead of pip with a single line command. Here is a link to that article.
towardsdatascience.com
This article has been read more than 250k times, but still some Ubuntu users are facing problems running Tensorflow GPU with correct versions of CUDA and CUDNN. I can understand the title of the article can be misleading but believe me if you follow step by step then proper installation on ubuntu even is easy.
Check GPU compatibility with Tensorflow GPU!Switching on to Graphic CardChecking CUDA compatibility with Graphic CardDetermine Version of Tensorflow according to CUDADownload CUDA toolkitInstall CUDADownload cudnn and installVerify cudnn installationInstall Tensorflow-gpu using pip in a new enviroment using CondaVerify tensorflow is using GPU.
Check GPU compatibility with Tensorflow GPU!
Switching on to Graphic Card
Checking CUDA compatibility with Graphic Card
Determine Version of Tensorflow according to CUDA
Download CUDA toolkit
Install CUDA
Download cudnn and install
Verify cudnn installation
Install Tensorflow-gpu using pip in a new enviroment using Conda
Verify tensorflow is using GPU.
Check GPU compatibility with Tensorflow GPU (From Here)Switching on to Graphic CardGo to Menu > Search for “Driver Settings” > Click on Additional Drivers
Check GPU compatibility with Tensorflow GPU (From Here)
Switching on to Graphic CardGo to Menu > Search for “Driver Settings” > Click on Additional Drivers
Select NVIDIA Binary Driver which is Version 384 in my case. This is very important to note down because this driver version will help us in determining the CUDA version.
Now if you will go to a terminal and type nvidia-smi you will get the driver version also.
3. Check the CUDA compatibility with your driver version from here.
In our case since driver version was 384.130 so we can install CUDA 9.0 only
4. Determine version of Tensorflow according to CUDA
After this step you should be very clear in your for 3 things:i) What Tensorflow Version you are going to install (tensorflow-gpu 1.12.0)ii) What CUDA version you are going to install (9.0)iii) What cudnn version you are going to install. (7)
NOTE: The answers to these questions will change based on what driver version you are using currently.
If you know answer to these 3 questions then you can already smell victory.
5. Download CUDA toolkitGo to this link and download the specific version of CUDA you are looking for you found in step 4 (CUDA 9.0 in this case)
6. CUDA installation
You will get software licence, long press Enter. Now accept everything except the driver install.
After installation you will recieve message:
Now you have to enter certain paths in bashrc file so that you can access cuda. Open a terminal and run the commands
sudo susudo gedit ~/.bashrc
(Add following lines to end of the file)
export PATH=/usr/local/cuda/bin:/usr/lib/:$PATHexport LD_LIBRARY_PATH=/usr/local/cuda/lib64:/usr/lib/nvidia-384:$LD_LIBRARY_PATHexport CUDA_HOME=/usr/local/cuda-9.0
Save and close the file and source the bashrc file for changes to take place.
source ~/.bashrc
Now open a terminal and write the command
nvcc -V
You will get the version of CUDA running.
6. Verify CUDA installation (optional): If you are happy with nvcc version then you can skip this step.
Open a terminal and run these commands:
cd /usr/local/cuda/samples/1_Utilities/deviceQuerysudo make./deviceQuery
7. Download cudnn compatible with CUDA and install
Go to Link and install cudnn. Remember here download the version of cudnn which is compatible with CUDA which is 7 in this case. You will need a signup to NVIDIA site to download this.
Install the cudnn packages in the following order runtime > developer > docs. Use the command
sudo dpkg -i <package_name>
8. Verify cudnn installation
To verify we cudnn installation, we will test one of the cudnn samples. Open a terminal and fire these commands:
cp -r /usr/src/cudnn_samples_v7/ $HOMEcd  $HOME/cudnn_samples_v7/mnistCUDNNmake clean && make./mnistCUDNN
If cudnn is installed properly you will see a message like: Tests Passed
9. Download Miniconda and install. Remember to install it without root.
Create a environment now where our tensorflow-gpu will be installed.
conda create  --name tf1 python=3.6
After executing this command a new environment named tf1 will be installed with python version of 3.6. Now activate the environment and execute the command to install tensorflow-gpu of the specific version we found out in step 4.
source activate tf1pip install tensorflow-gpu==1.12
10. To test your tensorflow installation follow these steps:
Open Terminal and activate environment using ‘source activate tf1’.Go to python console using ‘python’
Open Terminal and activate environment using ‘source activate tf1’.
Go to python console using ‘python’
import tensorflow as tfsess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
Now if you are able to see your GPU information in the terminal then CONGRATULATIONS, you have finally done it!
Thanks for your time. Please connect with me on LinkedIn for further updates and articles on Data Science.