How to install CUDA and TensorFlow GPU on Ubuntu 16.04

I remember when 4 years ago, I was trying to configure CUDA on a laptop with Ubuntu 14.04 and Nvidia Optimus technology - it was a quite tough process. Several times I messed up so much that it was easier to reinstall the whole OS to have a fresh start than trying to undo everything.

Luckily, time changed and nowadays, it's much easier and better documented. I was really positively surprised how much all CUDA-related tools matured over the time. So... let's start.

Initial checks

Make few initial checks to ensure that everything is in place:

GPU device

Execute in a terminal:

lspci | grep -i nvidia

And you should see something similar to this:

01:00.0 VGA compatible controller: NVIDIA Corporation Device 1c20 (rev a1)

Linux version

Run:

uname -m && cat /etc/*release | head -n0

And the expected version is:

x86_64

GCC

Run:

gcc --version

And check if your system has GCC installed. In my case, the output looked like:

gcc (Ubuntu 5.4.0-6ubuntu1~16.04.9) 5.4.0 20160609
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Preparation

Kernel headers

Install kernel headers:

sudo apt-get update
sudo apt-get install -y linux-headers-$(uname -r)

Remove old Nvidia's stuff

First, locate what you have already installed:

dpkg --get-selections | grep cuda | egrep 'install