Multi-version CUDA and Tensorflow(GPU) on Ubuntu 18.04 LTS

Ritesh Gangnani
TheCyPhy
Published in
5 min readMay 24, 2020

--

If you are here, then you already want to know how to set up multiple CUDA versions on the same machine. Most of the Deep Learning researchers come around various codes that use different versions of the frameworks that require specific versions of CUDA to be installed. And as most of the latest versions of frameworks don’t have backward compatibility, the only solution for us is to get the right versions installed.

In this blog, I’ll show how to easily set up CUDA 10.0 and CUDA 9.0 along with their cuDNN libraries on ubuntu 18.04 from scratch. The process would be the same for other Linux distributions and CUDA versions as well. We will install the following on a freshly installed Ubuntu 18.04:

  1. Nvidia Drivers 440
  2. CUDA 10.0 and cuDNN 7.6.5(specific for CUDA 10.0)
  3. CUDA 9.0 and cuDNN 7.6.5(specific for CUDA 9.0)
  4. TensorFlow GPU version 1.14 (CUDA 10) and 1.12 (CUDA 9)

Prerequisite: For the installation of the CUDA, we need to get the latest Nvidia drivers i.e. Nvidia-drivers-440 (required for CUDA 10.0 and higher). You can find the compatibility version on https://docs.nvidia.com/deploy/cuda-compatibility/index.html, and as of now the latest Nvidia drivers are backward compatible to CUDA 7.0.

Step 1. Installing/Upgrading Nvidia drivers :

If you already have Nvidia-drivers 440 installed, you can skip this step. Just run the following commands to install the required Nvidia drivers.

$ sudo add-apt-repository ppa:graphics-drivers/ppa
$ sudo apt-get install ubuntu-drivers-common
$ sudo ubuntu-drivers autoinstall
$ sudo apt install nvidia-driver-440
$ sudo apt-get update

Verify the Nvidia drivers installation using $ nvidia-smi. The output should be as follow.

Step 2. Install CUDA 10.0 and cuDNN 7.6.5

2.1. Run the following commands to install CUDA-10.0.

$ wget https://developer.nvidia.com/compute/cuda/10.0/Prod/local_installers/cuda-repo-ubuntu1804-10-0-local-10.0.130-410.48_1.0-1_amd64
$ sudo dpkg -i cuda-repo-ubuntu1804-10-0-local-10.0.130-410.48_1.0-1_amd64
$ sudo apt-key add /var/cuda-repo-10-0-local-10.0.130-410.48/7fa2af80.pub
$ sudo apt-get update
$ sudo apt-get install cuda-10.0

2.2 Now, run following commands to install cuDNN 7.5.6 for CUDA-10.0

$ wget http://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1604/x86_64/nvidia-machine-learning-repo-ubuntu1604_1.0.0-1_amd64.deb
$ sudo dpkg -i nvidia-machine-learning-repo-ubuntu1604_1.0.0-1_amd64.deb
$ sudo apt-get update
$ sudo apt-get install libcudnn7=7.6.5.32-1+cuda10.0
$ sudo apt-get install libcudnn7-dev=7.6.5.32-1+cuda10.0

2.3 Move the cuDNN libraries corresponding to CUDA 10.0 path.

$ sudo cp usr/include/cudnn.h /usr/local/cuda-10.0/include
$ sudo cp usr/lib/x86_64-linux-gnu/libcudnn* /usr/local/cuda-10.0/lib64
$ sudo chmod a+r /usr/local/cuda-10.0/include/cudnn.h /usr/local/cuda-10.0/lib64/libcudnn*

This step is done in order to keep the specific cuDNN libraries in the specific CUDA folder, else the installation of other versions overwrites the previous one.

Step 3. Install CUDA 9.0 and cuDNN 7.6.5

3.1. Run the following commands to install CUDA-9.0.

$ wget https://developer.nvidia.com/compute/cuda/9.0/Prod/local_installers/cuda-repo-ubuntu1604-9-0-local_9.0.176-1_amd64-deb
$ sudo dpkg -i cuda-repo-ubuntu1604-9-0-local_9.0.176-1_amd64-deb
$ sudo apt-key add /var/cuda-repo-9-0-local/7fa2af80.pub
$ sudo apt-get update
$ sudo apt-get install cuda-9.0

3.2 Now, run following commands to install cuDNN 7.5.6 for CUDA-9.0

$ wget http://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1604/x86_64/nvidia-machine-learning-repo-ubuntu1604_1.0.0-1_amd64.deb
$ sudo dpkg -i nvidia-machine-learning-repo-ubuntu1604_1.0.0-1_amd64.deb
$ sudo apt-get update
$ sudo apt-get install libcudnn7=7.6.5.32-1+cuda9.0
$ sudo apt-get install libcudnn7-dev=7.6.5.32-1+cuda9.0

3.3 Copy the cuDNN libraries corresponding to CUDA 9.0 path.

$ sudo cp usr/include/cudnn.h /usr/local/cuda-9.0/include
$ sudo cp usr/lib/x86_64-linux-gnu/libcudnn* /usr/local/cuda-9.0/lib64
$ sudo chmod a+r /usr/local/cuda-9.0/include/cudnn.h /usr/local/cuda-9.0/lib64/libcudnn*

Step 4. Reinstall Nvidia drivers

During the installations, CUDA downgrade the current Nvidia drivers to some specific versions. So, to avoid any unmet or broken packages we need to upgrade our Nvidia drivers back to version 440.

$ sudo apt-get install -y libnvidia-compute-440
$ sudo apt install nvidia-driver-440

Step 5. Export the PATH variables

$ export PATH=$PATH:/usr/local/cuda-10.0/bin:/usr/local/cuda-9.0/bin
$ export CUDADIR=/usr/local/cuda-10.0:/usr/local/cuda-9.0
$ export LD_LIBRARY_PATH=/usr/local/cuda/lib64:/usr/local/cuda-10.0/lib64:/usr/local/cuda-9.0/lib64

Step 6. Reboot your system.

This ends our installation of both the CUDA versions.

Step 7. Testing the versions

For testing the installations of both the versions, we require to install TensorFlow-GPU version 1.14 and 1.12 that require CUDA 10.0 and CUDA 9.0 respectively. Thus, we would require 2 different python environments for that. Assuming you have virtualenv installed.

7.1 Testing CUDA 10.0

Just run the following commands to create the environments and installing TensorFlow.

$ virtualenv tf1
$ source tf1/bin/activate
$ pip install tensorflow-gpu==1.14.0

Run the following python command to get the GPU’s available using TensorFlow 1.14.

$ python -c 'from tensorflow.python.client import device_lib;
print(device_lib.list_local_devices());'

This will print the list of available GPUs in your system using CUDA 10.0.

7.2 Testing CUDA 9.0

Just run the following commands to create the environments and installing TensorFlow.

$ virtualenv tf2
$ source tf1/bin/activate
$ pip install tensorflow-gpu==1.12.0

Run the following python command to get the GPU’s available using TensorFlow 1.12.

$ python -c 'from tensorflow.python.client import device_lib;
print(device_lib.list_local_devices());'

This will print the list of available GPUs in your system using CUDA 9.0, similar to the following output.

Enjoy Multi-CUDA coding!

Disclaimer: This seems to work well enough for us, but it is very likely that I have missed some details about the configuration that makes certain cases fail. Feel free to leave a comment if you notice any.

--

--

Ritesh Gangnani
TheCyPhy

I am a AI/ML Researcher, working in medical research areas using computer vision.