Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save raulqf/2d5f2b33549e56a6bb7c9f52a7fd471c to your computer and use it in GitHub Desktop.
Save raulqf/2d5f2b33549e56a6bb7c9f52a7fd471c to your computer and use it in GitHub Desktop.
How to install Tensorflow2 with GPU support in Ubuntu 20.04

How to install Tensorflow2 with GPU support in Ubuntu 20.04

Installation of Tensorflow2 with GPU support is easy and the only complication can be arisen from the CUDA compability which in turns depends on the Nvidia driver version. Before going farther, please check if your Nvidia Video Card is compatible with the required versions that are defined in this gist, use this link.

Tensorflow offers in its website a table of the compatibility between libraries for the target OS. You can visit that website in the following COMPATIBILITY TABLE that points to the Tensorflow installation from source for linux. For the time writing this gist, Tensorflow2 v2.5.0 requires CUDA v11.2 and CUDNN v8.2. It is really important to match the exact version, otherwise tensorflow will have problems loading the shared libraries as not finding the correct version.

CUDA version also requires for a minimum Nvidia driver version. In our case, CUDA v11.2 requires Nvidia driver to be superior to 450.80.02 and you can check that information from CUDA website that can be found in this CUDA-COMPATIBILITY TABLE.

Proceed with the installation of the CUDA TOOLKIT

For the CUDA installation we must visit Cuda website and select the exact version. Nowadays, it is even not the latest one so you have to navigate through the legacy releases to find the exact version. If you do not find the correct link, google "cuda archive" to find the website with previous releases. There are several ways to install CUDA, I prefer the runfile(local) option. So, from the form you must choose Linux > x86_64 > Ubuntu > 20.04 > runfile(local). There is a text box below showing the commands to follow for the installation. In our case, we have the following instructions:

$ wget https://developer.download.nvidia.com/compute/cuda/11.2.0/local_installers/cuda_11.2.0_460.27.04_linux.run
$ sudo sh cuda_11.2.0_460.27.04_linux.run

Possible Errors: If the installation fails with the following message ´Installation failed. See log at /var/log/cuda-installer.log for details´, it means that your system is using current nvidia-drivers and this operation cannot be permitted. Remember, that this installation is not only installing the CUDA Toolkit, but also the nvidia drivers. We could manage this error by installing only the toolkit and it can be done using the following arguments in the previous command:

$ sudo sh cuda_11.2.0_460.27.04_linux.run --toolkit --silent --override

Install CUDNN

For the CUDNN installation, we have to start downloading the CUDNN version from Nvidia (an account is required to be logged in the website to proceed with the dowload) through this link. In the website look for the CUDNN version compatible with your CUDA version.

Next steps are really well explained by LearnOpenCV blog in the following post Installing Deep Learning Frameworks on Ubuntu with CUDA support. Basically, we have to uncompress the file and copy the extracted libs to the cuda installation folder.

$ tar xvf cudnn-11.3-linux-x64-v8.2.0.53.tgz
$ sudo cp -P cuda/lib64/* /usr/local/cuda/lib64/
$ sudo cp cuda/include/* /usr/local/cuda/include/

Now, we have to configure some enviroment variables to help the system locate these libraries:

$ echo 'export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/local/cuda/lib64:/usr/local/cuda/extras/CUPTI/lib64"' >> ~/.bashrc
$ echo 'export CUDA_HOME=/usr/local/cuda' >> ~/.bashrc
$ echo 'export PATH="/usr/local/cuda/bin:$PATH"' >> ~/.bashrc

Reload the file:

$ source ~/.bashrc

Install Tensorflow2 with GPU

First of all, it is always recommended to use virtual environments. If you do not know what it is, google it or use my this gist virtual environments but could be outdated.

Once created the virtual environment, activate it and install Tensorflow using PyPy:

$ pip3 install tensorflow-gpu //By default it will install the latest estable version of Tensorflow; in this case version 2.0.

To check the correct installation import the tensorflow package and run a sequence to test if the GPU is available:

$ python3 //To enter in the python interpreter
> import tensorflow as tf
> tf.test.is_gpu_available()

If you sucess you will receive a ´True´ response.

Mnist example

from __future__ import absolute_import, division, print_function, unicode_literals

import tensorflow as tf

mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0

model = tf.keras.models.Sequential([
  tf.keras.layers.Flatten(input_shape=(28, 28)),
  tf.keras.layers.Dense(128, activation='relu'),
  tf.keras.layers.Dropout(0.2),
  tf.keras.layers.Dense(10, activation='softmax')
])

model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

model.fit(x_train, y_train, epochs=5)
model.evaluate(x_test,  y_test, verbose=2)

Source

Appendix

CUDA TOOLKIT INSTALLATION USING *.DEB PACKAGE

Download the .deb file and run the following installation instructions offered by CUDA:

$ sudo dpkg -i cuda-repo-ubuntu1804_10.0.130-1_amd64.deb
$ sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/7fa2af80.pub 
$ sudo apt-get update
$ sudo apt-get install cuda-toolkit-10-0 //Be aware!! I have change this line to only install the respective toolkit.

Important, before installing cuda package make sure that this package is not upgrading your Nvidia drivers, otherwise it would mess up the installation. In fact, we only want to install the CUDA Toolkit not the CUDA package that contains the toolkit and the Nvidia drivers. If you want to do not fail this step launch the proposed sentence: ´sudo apt-get install cuda-toolkit-10-0´

Possible Errors: If you have problems installing cuda-repo-ubuntu...deb because you have previously installed versions of cuda, you can find the previous packages using ´dpkg -l | grep cuda*´ and delete them with ´dpkg -r package_name´.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment