TensorFlow 2.3 on Ubuntu 20.04 LTS with CUDA 11.0 and CUDNN 8.0

Trying to setup a Deep Learning environment can be tricky due to the dependencies of pre-compiled libraries. Different versions of TensorFlow for instance require very specific versions of CUDA and CUDNN in order to work properly. An alternative approach is compiling TensorFlow from source and to build it by using previously installed CUDA and CUDNN versions. This has its own challenges, but luckily there are a lot of good tutorials out there that describe the various steps. I mostly followed (and adjusted where needed) this one which I found very useful.

I recently installed reinstalled Ubuntu 20.04 LTS and decided to upgrade to the latest versions of the NVIDIA drivers, CUDA, and CUDNN libraries and remain hopeful that I can stick to them for a while…

While installing Ubuntu, I opted in for the ‘Additional 3rd party drivers’, so Ubuntu started up with the the NVIDIA graphics drivers version 440.100.

$ nvidia-smi

Install latest NVIDIA drivers

The latest drivers for my GPU from on Nvidia downloads were already at version 450.57 so I got them from there.

Since I decided to go with the proprietary Nvidia drivers, I blocked the open source Nouveau drivers as described here to prevent potential issues with unwanted driver updates.

$ nvidia-smi
Image for post
Image for post

Install latest CUDA

You can get the latest CUDA version directly from Nvidia. I choose the deb (local) install option for CUDA 11.0. They conveniently generate the code for you to download and install CUDA. The code snippet can be pasted directly into a terminal window.

Image for post
Image for post

Afterwards, I had to update my PATH variable accordingly:

$ export PATH=/usr/local/cuda-11.0/bin${PATH:+:${PATH}}
$ export LD_LIBRARY_PATH=/usr/local/cuda-11.0/lib\
${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}

You can quickly test your CUDA installation by checking the version of installed compiler:

$ nvcc --version
Image for post
Image for post

CUDA also ships with samples that are saved at /usr/local/cuda-11.0/samples. You can try to compile and run them to test if CUDA is properly installed.

Install latest CUDNN libraries

Nvidia requires you to create a free developers account in order to download CUDNN. I choose the route of downloading the tarball and to copy the libraries directly into the CUDA folder at /usr/local/cuda-11.0 as described in the CUDNN documentation.

Image for post
Image for post

Build TensorFlow from source

I basically followed this excellent write-up to compile TF 2.3 with CUDA 11 and CUDNN 8 from the sources.

Using the latest version of Basel to build TensorFlow 2.3 gave me an error, but I was able to start the build after downgrading to Bazel version 3.1.0.

$ sudo apt install curl
$ curl https://bazel.build/bazel-release.pub.gpg | sudo apt-key add -
$ echo "deb [arch=amd64] https://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.list
$ sudo apt update && sudo apt install bazel-3.1.0

I set up a virtual environment and installed the necessary dependencies as described in the TensorFlow documentation.

$ pip install -U pip six 'numpy<1.19.0' wheel setuptools mock 'future>=0.17.1'
$ pip install -U keras_applications --no-deps
$ pip install -U keras_preprocessing --no-deps

Afterwards, I pulled the TF 2.3 source files from the official TensorFlow git repository.

$ git clone https://github.com/tensorflow/tensorflow
$ cd tensorflow
$ git checkout v2.3.0

And could start building the TensorFlow pip package… and get a coffee!

$ ./configure
$ bazel build — config=opt -c opt //tensorflow/tools/pip_package:build_pip_package

This took quite a while, and I honestly wasn’t sure what to expect. But almost 90 minutes later, the build successfully finished.

INFO: Elapsed time: 5251.301s, Critical Path: 310.79s
INFO: 24110 processes: 24110 local.
INFO: Build completed successfully, 37962 total action

The final step was to build the pip package and install it.

$ ./bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
$ pip install /tmp/tensorflow_pkg/tensorflow-3.1.0-cp38-cp38m-linux_x86_64.whl

Testing TensorFlow

You can test your new TF 2.3 installation by using a sample CNN from the TensorFlow page and should get a similar out put to the one below.

Epoch 1/10
I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library libcublas.so.11
I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library libcudnn.so.8
1563/1563 [==============================] — 11s 7ms/step — loss: 1.5408 — accuracy: 0.4381 — val_loss: 1.2892 — val_accuracy: 0.5328

Following the steps above should enable you to install the latest version of TensorFlow with CUDA 11 and CUDNN 8 on a clean install of Ubuntu 20.04.

Thanks to everyone else who put in the effort to document how they proceeded to setup their DL environment. Please let me know if you are stuck somewhere, and I can go back and check if I should have missed a step or if something needs additional clarification!

Written by

DS Lead at Pitney Bowes

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store