Installing TensorFlow on Ubuntu 20.04

Bojan Tunguz
3 min readJun 4, 2020

--

Recently I’ve installed Ubuntu 20.04 on couple of my systems. On one of them it was a preference, while for the other one it was a necessity — that computer was having lots of issues, especially with graphical drivers. Ubuntu 20.04 is awesome, and I was really pleased with the ease of use for DS/ML work — it comes with Python 3.8 as the default, lets you easily install almost all Python packages, and the overall user experience feels really nice and smooth.

There is one exception, though, when it comes to the DS modeling tools stack: TensorFlow. At the time of this writing (Early June 2020) TensorFlow does not have a native out-of-the box install that supports Ubuntu 20.04, Cuda 10.2 and Python 3.8. Nonetheless, I believed it would still be possible to get it installed somehow, and after a couple of days of intense frustration and heartbreak (someone ought to write a Country song about TF installation), I finally managed to install it on two of my machines: a desktop with RTX 1080 Ti and a laptop with Quadro RTX 5000. So before I forget how I did it, and help others who may want to install it as well, let me commit my process to this fine online parchment.

  1. Install TensorFlow with pip:
pip3 install tensorflow

This will actually install TensorFlow, but only the CPU version. You need to separately install CUDA 10.1 and CUDNN 7.

2. Download CUDA 10.1 You can go to the following link and get the Ubuntu 18.04 version:

I have downloaded the following .deb file for instance:

cuda-repo-ubuntu1804-10-1-local-10.1.105-418.39_1.0-1_amd64.deb

3. Execute the following commands:

sudo dpkg -i cuda-repo-ubuntu1804–10–1-local-10.1.105–418.39_1.0–1_amd64.debsudo apt-key add /var/cuda-repo-<version>/7fa2af80.pubsudo apt-get updatesudo apt-get install cuda-10-1

4. Download CUDNN. I have downloaded it from the following link (you may need to sign up for a free developer account):

https://developer.nvidia.com/rdp/cudnn-download

Choose the 10.1 version. The file that I downloaded is:

cudnn-10.1-linux-x64-v7.6.4.38.tgz

5. Extract it in your local folder:

tar -xzvf cudnn-10.1-linux-x64-v7.6.4.38.tgz

6. Move the following files:

sudo cp cuda/include/cudnn.h /usr/local/cuda-10.1/includesudo cp cuda/lib64/libcudnn* /usr/local/cuda-10.1/lib64sudo chmod a+r /usr/local/cuda-10.1/include/cudnn.h /usr/local/cuda-10.1/lib64/libcudnn*

7. Add the following two lines to the .bashrc file:

export LD_LIBRARY_PATH=/usr/local/cuda-10.1/lib64:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=/usr/local/cuda-10.1/include:$LD_LIBRARY_PATH

And that’s it! To make sure that your TensorFlow is running properly and can access the GPU, start python3 in terminal and execute the following two lines:

import tensorflow as tf
tf.config.list_physical_devices('GPU')

If the two lines execute correctly, and you get a non-empty list of GOU devices, then you should be able to run all your TF code on GPU without any issues.

--

--

Bojan Tunguz

Machine Learning at Nvidia. Physicist. Writer. Data scientist. Catholic. Husband. Father. #technology #gadgets #datascience #bigdata #AI #MachineLearning