Installing Tensorflow GPU on Ubuntu 20.04, This Is the Way

Artiya
BOOTLEGSOFT
Published in
2 min readJan 6, 2021

--

This guide is the best way to install Tensorflow GPU when other methods are not successful on my Ubuntu 20.04 machine.

  1. Removing the old package.
sudo apt-get clean
sudo apt-get update
sudo apt-get purge cuda
sudo apt-get purge nvidia-*
sudo apt-get autoremove

2. Install Nvidia package repositories

wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/cuda-ubuntu2004.pinsudo mv cuda-ubuntu2004.pin /etc/apt/preferences.d/cuda-repository-pin-600sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/7fa2af80.pubsudo add-apt-repository "deb https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/ /"sudo apt-get update

3. Install Nvidia cudnn package repositories

wget http://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu2004/x86_64/nvidia-machine-learning-repo-ubuntu2004_1.0.0-1_amd64.deb

sudo apt install ./nvidia-machine-learning-repo-ubuntu2004_1.0.0-1_amd64.deb
sudo apt-get update

4. Install Nvidia cuda and cudnn

sudo apt-get install cuda-11-2 nvidia-cuda-toolkit libcudnn8 libcudnn8-devsudo reboot # Reboot your machine

5. Install the Tensorflow on the system Python3.8 from Ubuntu

sudo apt install python3 python3-pipsudo pip3 install tensorflow

6. Test in Python

python3 -c 'import tensorflow as tf
print("Num GPUs Available: ", len(tf.config.experimental.list_physical_devices("GPU")))'

The result should be like this:

If found error “Failed to call ThenRnnBackward with model config” use this code to allow memory growth.

gpu_devices = tf.config.experimental.list_physical_devices('GPU')
for device in gpu_devices:
tf.config.experimental.set_memory_growth(device, True)

--

--