Painless GPU and tensorflow/PyTorch integration on Ubuntu 18.04

There are numerous posts about setting up your GPU on the internet. I tried to follow so many of them to set up my machine, but was somehow not able to do so. There are so many variables in this process which span from nvidia driver version, cuda tool kit version, CUDnn version, python version, tensorflow version etc. The list goes on. It is also a good idea to see if your GPU supports CUDA integration here.

Photo by Christian Erfurt on Unsplash

I assume the following:

  1. There are no previous nvidia drivers on the machine. If there are, I recommend to uninstall them first otherwise following my instructions may or may not work.
  2. Python is already installed on the machine. I recommend using anaconda since it makes the job of tracking library versions and virtual environments very easy.

Here’s what I did:

Open the terminal and run the following commands.

$ sudo add-apt-repository ppa:graphics-drivers/ppa 
$ sudo apt update
$ sudo apt install nvidia-390
$ sudo reboot
$ nvidia-smi

If everything goes according to plan you would be able to see a similar looking output on the screen.

Go here and download the toolkit for linux, x86_64, ubuntu, 17.04, deb.

Once the the download is complete, open a terminal in the directory of the base installer and run the following commands:

$ sudo dpkg -i cuda-repo-ubuntu1704-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

Also download and install some patches (optional) which are some improvements on cuBLAS.

Add these two lines in the .bashrc file located in the home directory

export PATH=/usr/local/cuda-9.0/bin${PATH:+:${PATH}}$export LD_LIBRARY_PATH=/usr/local/cuda- 9.0/lib64${LD_LIBRARY_PATH:+${LD_LIBRARY_PATH}}

Don’t forget to source the .bashrc file after making changes in it.

Go here and select CUDNN 7.0.5 (December 5, 2017) for CUDA 9.0. You need to make an account here if you already don’t have one. Download the tar file and navigate to where it is located via the terminal. Then type he following commands.

$ tar -xzvf cuda-9.0-linux-x64-v7.tgz
$ sudo cp cuda/include/cudnn.h /ur/local/cuda/include
$ sudo cp cuda cuda/lib64/libcudnn* /usr/local/cuda/lib64
$ sudo chmod a+r /usr/local/cuda/include/cudnn.h /usr/local/cuda/lib64/libcudnn*

I assume you have anaconda already installed on your system. Unfortunately the newest python 3.7 version has some compatibility issues with the cuda integration. To bypass this I make a new python 3.6 environment.

$ conda create -n tfgpu python=3.6 
$ conda activate tfgpu

To come out of this environment simply type conda deactivate.

Now comes the final part of installing the tensorflow GPU version.

$ conda install tensorflow-gpu==1.12 cudatoolkit==9.0 cudnn==7.1.2 h5py

Install all the dependencies that come with this installation. If everything goes well, essentially we are done! Time to check the installation.

$ python
>>> import tensorflow as tf
>>> sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
2019-04-24 21:57:54.300645: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX AVX2 FMA
2019-04-24 21:57:54.516014: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:964] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2019-04-24 21:57:54.516685: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1432] Found device 0 with properties:
name: Quadro P1000 major: 6 minor: 1 memoryClockRate(GHz): 1.5185
pciBusID: 0000:01:00.0
totalMemory: 3.94GiB freeMemory: 2.67GiB
2019-04-24 21:57:54.516704: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1511] Adding visible gpu devices: 0
2019-04-24 21:57:56.372624: I tensorflow/core/common_runtime/gpu/gpu_device.cc:982] Device interconnect StreamExecutor with strength 1 edge matrix:
2019-04-24 21:57:56.372706: I tensorflow/core/common_runtime/gpu/gpu_device.cc:988] 0
2019-04-24 21:57:56.372732: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1001] 0: N
2019-04-24 21:57:56.373124: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1115] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 2370 MB memory) -> physical GPU (device: 0, name: Quadro P1000, pci bus id: 0000:01:00.0, compute capability: 6.1)
Device mapping:
/job:localhost/replica:0/task:0/device:XLA_CPU:0 -> device: XLA_CPU device
/job:localhost/replica:0/task:0/device:XLA_GPU:0 -> device: XLA_GPU device
/job:localhost/replica:0/task:0/device:GPU:0 -> device: 0, name: Quadro P1000, pci bus id: 0000:01:00.0, compute capability: 6.1
2019-04-24 21:57:56.376584: I tensorflow/core/common_runtime/direct_session.cc:307] Device mapping:
/job:localhost/replica:0/task:0/device:XLA_CPU:0 -> device: XLA_CPU device
/job:localhost/replica:0/task:0/device:XLA_GPU:0 -> device: XLA_GPU device
/job:localhost/replica:0/task:0/device:GPU:0 -> device: 0, name: Quadro P1000, pci bus id: 0000:01:00.0, compute capability: 6.1

Since we have already done the heavy lifting by installing the inter compatible versions of cuda toolkit, CUDNN and python, installing PyTorch becomes a one line task.

$ conda install pytorch torchvision cuda90 -c pytorch
$ python
>>> import torch
>>> torch.cuda.get_device_name(0)
'Quadro P1000'
>>> torch.cuda.is_available()
True
>>> torch.cuda.device_count()
1
>>>

Data Scientist@ThoughtWorks

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