Installing TensorFlow GPU in Ubuntu 20.04 | by Abien Fred Agarap | Towards Data Science

Also published at https://afagarap.works/2020/07/26/installing-tf-gpu-ubuntu2004.html
When Ubuntu publishes a long-term support (LTS) release, I usually wait for a while before upgrading, mainly because I’m waiting for CUDA and cuDNN support for the new release. This time, it only took me three months to migrate from Ubuntu 18.04 to Ubuntu 20.04 — well, technically an Ubuntu-based distro, i.e. Regolith Linux. My decision to do so was simply because I upgraded my SSD from 120GB to 1TB, and so I migrated to a different OS as well— albeit just an Ubuntu derivative.
As I expected, it took me a while to work things out in my new system. Fortunately, I saw some helpful answers online, and now I’m expanding on their answers by adding a bit more explanations. So, this post is actually based on the answers given by meetnick and singrium in this related question posted in Ask Ubuntu.
The installation of TensorFlow GPU in Ubuntu 20.04 can be summarized in the following points,
Install CUDA 10.1 by installing nvidia-cuda-toolkit.
Install the cuDNN version compatible with CUDA 10.1.
Export CUDA environment variables.
Install TensorFlow 2.0 with GPU support.
First, ensure that you are using the NVIDIA proprietary driver by going to “Additional Drivers”, and then choosing the appropriate driver, i.e. for CUDA 10.1, the required driver version is ≥ 418.39. We use the proprietary version over the open source one since CUDA can only operate with the proprietary driver.
We are installing CUDA 10.1 because it is the compatible version with TensorFlow GPU.
At the time of this writing, there is no available CUDA 10.1 for Ubuntu 20.04, but as meetnick points out in the referenced Ask Ubuntu post, installing nvidia-cuda-toolkit also installs CUDA 10.1.
For the sake of being verbose, do not try to use 18.10 or 18.04 CUDA 10.1 for Ubuntu 20.04. I learned that the hard way, lol!
So, you can install CUDA 10.1 in Ubuntu 20.04 by running,
$ sudo apt install nvidia-cuda-toolkit
After installing CUDA 10.1, run nvcc -V. Then you will get an output similar to the following to verify if you had a successful installation,
nvcc: NVIDIA (R) Cuda compiler driverCopyright (c) 2005-2019 NVIDIA CorporationBuilt on Sun_Jul_28_19:07:16_PDT_2019Cuda compilation tools, release 10.1, V10.1.243
Unlike in Ubuntu 18.04 (where I was from), CUDA is installed in a different path in 20.04, i.e. /usr/lib/cuda — which you can verify by running,
$ whereis cudacuda: /usr/lib/cuda /usr/include/cuda.h
In Ubuntu 18.04, as you might know, CUDA is installed in /usr/local/cuda or in /usr/local/cuda-10.1.
After installing CUDA 10.1, you can now install cuDNN 7.6.5 by downloading it from this link. Then, choose “Download cuDNN”, and you’ll be asked to login or create an NVIDIA account. After logging in and accepting the terms of cuDNN software license agreement, you will see a list of available cuDNN software.
Click “Download cuDNN v7.6.5 (November 5th, 2019) for CUDA 10.1”, then choose “cuDNN Library for Linux” to download cuDNN 7.6.5 for CUDA 10.1. After downloading cuDNN, extract the files by running,
$ tar -xvzf cudnn-10.1-linux-x64-v7.6.5.32.tgz
Next, copy the extracted files to the CUDA installation folder,
$ sudo cp cuda/include/cudnn.h /usr/lib/cuda/include/$ sudo cp cuda/lib64/libcudnn* /usr/lib/cuda/lib64/
Set the file permissions of cuDNN,
$ sudo chmod a+r /usr/lib/cuda/include/cudnn.h /usr/lib/cuda/lib64/libcudnn*
The CUDA environment variables are needed by TensorFlow for GPU support. To set them, we need to append them to ~/.bashrc file by running,
$ echo 'export LD_LIBRARY_PATH=/usr/lib/cuda/lib64:$LD_LIBRARY_PATH' >> ~/.bashrc$ echo 'export LD_LIBRARY_PATH=/usr/lib/cuda/include:$LD_LIBRARY_PATH' >> ~/.bashrc
Load the exported environment variables by running,
$ source ~/.bashrc
After installing the prerequisite packages, you can finally install TensorFlow 2.0,
$ pip install tensorflow==2.2.0
The tensorflow package now includes GPU support by default as opposed to the old days that we need to install tensorflow-gpu specifically.
Verify that TensorFlow can detect your GPU by running,
>>> import tensorflow as tf>>> tf.config.list_physical_devices("GPU")[PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]
If things went smoothly, you should have a similar output.
You can now enjoy using TensorFlow for your deep learning projects! Hooray!
If you are looking for a TensorFlow project to work on, perhaps you will find my blog on Implementing Autoencoder in TensorFlow 2.0 enjoyable!
Also, if you enjoyed this article, perhaps you will enjoy my other blogs as well!