Instantly share code, notes, and snippets.

Embed
What would you like to do?
Quick install Cuda 10.0 and Tensorflow on Ubuntu 18.04
title author date output
Install for Cuda 10.0 Tensorflow on Ubuntu 18.04
Mourad Veeneman
November 26, 2018
html_document

Install for Cuda 10.0 Tensorflow on Ubuntu 18.04

This will show you how to install tensorflow with NVIDIA's Cuda 10.0 GPU environment on Ubuntu 18.04. It will install all required packages and then compile Tensorflow from source. Note that building tensorflow from source is a lenghty process and can take up to 60 minutes depending on the speed of your machine.

These steps also work with a completely fresh Ubuntu 18.04 install.

Installing NVIDIA video driver and Cuda 10.0

Always first update to the latest versions of your packages:

sudo apt update
sudo apt upgrade

First uninstall Nouveau video driver:

Create a blacklist configuration file with:

sudo nano /etc/modprobe.d/blacklist-nouveau.conf

with the following contents:

blacklist nouveau
options nouveau modeset=0

Regenerate the kernel with initramfs:

sudo update-initramfs -u

and finally reboot machine:

sudo reboot

Don't worry, since you have now disabled the video driver the the login screen will be low resolution VGA. This will get fixed in the next section after you've rebooted at the end.

Installing Cuda from NVIDIA repo

The quickest way to install all nvidia software is from the NVIDIA repo:

sudo apt-key adv --fetch-keys  http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/7fa2af80.pub
sudo bash -c 'echo "deb http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64 /" > /etc/apt/sources.list.d/cuda.list'
sudo apt update
sudo apt install nvidia-driver-410
sudo apt install cuda-10-0
echo "/usr/local/cuda/lib64" | sudo tee -a /etc/ld.so.conf
echo "/usr/local/cuda-10.0/extras/CUPTI/lib64" | sudo tee -a /etc/ld.so.conf
sudo ldconfig

reboot the machine. The machine will come online using the NVIDIA video driver.

Installing CuDNN 7.4.1

To download this library, you will need to have a registered NVIDIA developer account. Login with your credentials on developer.nvidia.com, find CuDnn and download these packages:

sudo dpkg -i libcudnn7_7.4.1.5-1+cuda10.0_amd64.deb
sudo dpkg -i libcudnn7-dev_7.4.1.5-1+cuda10.0_amd64.deb
sudo dpkg -i libcudnn7-doc_7.4.1.5-1+cuda10.0_amd64.deb

Building Tensorflow from source

Installing Bazel before compiling Tensorflow

To build Tensorflow you will first need Bazel. The easiest way to install, is to add the Bazel repo and install the package.

sudo apt --yes install curl
# echo "deb [arch=amd64] http://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.list
# curl https://bazel.build/bazel-release.pub.gpg | sudo apt-key add -
# sudo apt --yes update
# sudo apt --yes install bazel
# --Removed because of issues with bazel 0.19/0.20
curl -O http://storage.googleapis.com/bazel-apt/pool/jdk1.8/b/bazel/bazel_0.18.0_amd64.deb
sudo dpkg -i bazel_0.18.0_amd64.deb

Configuring and compiling Tensorflow

Building Tensorflow from source is very well explained on the Tensorflow website. Here are the steps:

sudo apt install python-dev python-pip
sudo -H pip install -U pip six numpy wheel mock
sudo -H pip install -U keras_applications==1.0.5 --no-deps
sudo -H pip install -U keras_preprocessing==1.0.3 --no-deps
sudo apt --yes install git
git clone https://github.com/tensorflow/tensorflow.git
cd tensorflow
./configure

Don't forget to type Y for for Cuda, Cuda version 10.0 and CuDNN version 7.4.1 and CuDNN library location /usr/lib. For all others you can leave the default values.

Building Tensorflow from source will take a while, so prepare for a long build process.

bazel build --config=opt --config=cuda //tensorflow/tools/pip_package:build_pip_package

Once you have built Tensorflow, you will need to build the Python package and install it:

./bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
sudo -H pip install /tmp/tensorflow_pkg/tensorflow-<version-tags>.whl

All done!

Please help improve this document by reporting any issues.

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