The easy way: Install Nvidia drivers, CUDA, CUDNN and Tensorflow GPU on Ubuntu 18.04

How can I easily get tensorflow gpu running?


Solution 1:

I just installed Tensorflow GPU on Ubuntu 18.04. There are a lot instructions for it, however I think the fastest and easiest way is usually not used and I want to share it:

NVIDIA DRIVER:

ubuntu-drivers devices
sudo ubuntu-drivers autoinstall
nvidia-smi

CUDA:

Normally: "sudo apt install nvidia-cuda-toolkit" However this installs version 9.1, too new at the moment and tensorflow will not run. Instead download CUDA 9.0: https://developer.nvidia.com/cuda-90-download-archive?target_os=Linux&target_arch=x86_64&target_distro=Ubuntu&target_version=1704&target_type=runfilelocal Then run (Additional Details on the first line can be found at: How can I install CUDA 9 on Ubuntu 17.10):

sudo sh cuda_9.0.176_384.81_linux.run --override
sudo ln -s /usr/bin/gcc-6 /usr/local/cuda/bin/gcc
sudo ln -s /usr/bin/g++-6 /usr/local/cuda/bin/g++
cd ~
sudo nano .bashrc

add at the end of the file:
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}}
CTL+X to save and exit

CuDNN:

register at nvidia developers https://developer.nvidia.com/cudnn Download 9.1 runtime & developer library for 16.04 (Files cuDNN v7.1.3 Runtime Library for Ubuntu16.04 (Deb) & cuDNN v7.1.3 Developer Library for Ubuntu16.04 (Deb)) Open the files with software manager and install them. Check with:

cat /usr/include/x86_64-linux-gnu/cudnn_v*.h | grep CUDNN_MAJOR -A 2

Install libraries and tensorflow:

sudo apt-get install libcupti-dev
pip3 install tensorflow-gpu

Check:

in tensorflow check for GPU support 
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))

Note: Start tensorflow or your development environment from terminal, otherwise for me it does not load the PATH variables.

Solution 2:

Just to extend Jonny's answer: when following Jonny's method for installing CUDA, I had to choose "No" for "Install NVIDIA Accelerated Graphics Driver for Linux-x86_64 384.81?" (yes or default for everything else). Otherwise the CUDA driver installation would fail saying "ERROR: An NVIDIA kernel module 'nvidia-drm' appears to already be loaded in your kernel"

Then you can test your CUDA installation by running the samples.

First install their dependencies:

sudo apt-get install freeglut3-dev build-essential libx11-dev libxmu-dev libxi-dev libgl1-mesa-glx libglu1-mesa libglu1-mesa-dev libglfw3-dev libgles2-mesa-dev

These won't compile with the new compilers on Ubuntu 18, so you'll need to downgrade to gcc/g++ 6:

sudo apt-get install gcc-6 g++-6
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-6 10
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-6 10

Compile the samples by navigating to:

~/NVIDIA_CUDA-9.0_Samples/bin/x86_64/linux/release

And compile using:

make -k

Run one of the samples like:

./nbody