Install Tensorflow , PyTorch in Ubuntu 18.04 LTS with Cuda 9.0 for NVIDIA 1080 Ti

bala (prasanna)
4 min readJul 9, 2018

--

Before following this tutorial, make sure you have sudo access to run the commands mentioned in this blog.

Step 1: Check for NVIDIA driver version

The first thing you should check is that you have an Nvidia driver installed for your graphics card. Your graphics card must support at least Nvidia compute 3.0 to install tensorflow-gpu

You can check what graphics driver you have installed using nvidia-smi .You should see some output like the following:

nvidia-smi

Step 2: Install CUDA Toolkit 9.0

Head over to https://developer.nvidia.com/cuda-toolkit-archive and grab the the runfile download for Ubuntu 17.04. While this is for a different version of Ubuntu, you can get it to install what you need. You’ll have to go to the legacy downloads archive page to find version 9.0.

Just click on the following link

Once you’ve got that file, navigate to where the file was downloaded in your terminal and check you have the same files as follows

$ ls
cuda_9.0.176.1_linux.run cuda_9.0.176.2_linux.run cuda_9.0.176.3_linux.run cuda_9.0.176_384.81_linux.run

Then run in the following sequence.

sudo chmod +x cuda_9.0.176_384.81_linux.run
./cuda_9.0.176_384.81_linux.run --override

Accept the terms and conditions, say yes to installing with an unsupported configuration,

no to “Install NVIDIA Accelerated Graphics Driver for Linux-x86_64 384.81?”.

Make sure you don’t agree to install the new driver. In my experience, doing so often leads to system instability issues. Follow the prompts to install the toolkit using the default install locations.

Also apply the patches provided.

sudo chmod +x cuda_9.0.176.1_linux.run
sudo chmod +x cuda_9.0.176.2_linux.run
sudo chmod +x cuda_9.0.176.3_linux.run
./cuda_9.0.176.1_linux.run
./cuda_9.0.176.2_linux.run
./cuda_9.0.176.3_linux.run

Step 3: Install CUDNN 7.1.4

You need to login using nvdia account to download this. Please feel free to signup and then download

Next, head to https://developer.nvidia.com/rdp/cudnn-download to get CUDNN 7.1. Go to the downloads archive page again and find version 7.1 for CUDA 9.0 that you just installed. Download the link that says “cuDNN v7.1.4 Library for Linux”. This will download an archive that you can unpack and move the contents the correct locations.

There are lots of options on the archive downloads page for CUDNN. Get the Library for Linux file for CUDA 9.0.

Once downloaded, unpack the archive and move it the contents into the directory where you install CUDA 9.0:

# Unpack the archive
tar -zxvf cudnn-9.0-linux-x64-v7.1.tgz
# Move the unpacked contents to your CUDA directory
sudo cp -P cuda/lib64/libcudnn* /usr/local/cuda-9.0/lib64/
sudo cp cuda/include/cudnn.h /usr/local/cuda-9.0/include/
# Give read access to all users
sudo chmod a+r /usr/local/cuda-9.0/include/cudnn.h /usr/local/cuda/lib64/libcudnn*

Step 4: Install libcupti

This one is easy.

sudo apt-get install libcupti-dev

Do the CUDA post-install actions

So Tensorflow can find your CUDA installation and use it properly, you need to add these lines to the end of you ~/.bashrc or ~/.zshrc.

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

Restart your terminal before proceeding to the next step or do source ~/.bashrc

Install Tensorflow GPU

Finally, to install tensorflow-gpu run

pip install --upgrade tensorflow-gpu

I recommend installing tensorflow in a virtualenv to prevent having to muck around with your system Python packages. The official Tensorflow install instructions give various options, so you can choose what works best for you. If you choose the virtualenv route, I highly recommend using virtualenvwrapper, which makes using virtualenv far easier.

You can now test everything worked by opening a new python interpreter with python and running the following commands:

from tensorflow.python.client import device_lib

device_lib.list_local_devices()

If everything worked fine, you’ll see your GPU listed as part of the output like so:

[name: "/device:CPU:0"
device_type: "CPU"
memory_limit: 268435456
locality {
}
incarnation: 182532294431716449, name: "/device:GPU:0"
device_type: "GPU"
memory_limit: 10498270823
locality {
bus_id: 1
links {
link {
device_id: 1
type: "StreamExecutor"
strength: 1
}
}
}
incarnation: 14673206105771676974
physical_device_desc: "device: 0, name: GeForce GTX 1080 Ti, pci bus id: 0000:01:00.0, compute capability: 6.1", name: "/device:GPU:1"
device_type: "GPU"
memory_limit: 10917150720
locality {
bus_id: 1
links {
link {
type: "StreamExecutor"
strength: 1
}
}
}
incarnation: 16384320033882398672
physical_device_desc: "device: 1, name: GeForce GTX 1080 Ti, pci bus id: 0000:02:00.0, compute capability: 6.1"]

Install PyTorch

You can also install pytorch & torchvision by running,

conda install pytorch torchvision cuda91 -c pytorch

Optionally, if you are using pip

pip3 install http://download.pytorch.org/whl/cu91/torch-0.4.0-cp36-cp36m-linux_x86_64.whl 
pip3 install torchvision

You can now test pytorch by opening a new python interpreter with python and running the following commands:

import torch
torch.cuda.is_available()
# True import torch
torch.cuda.get_device_name(0)
# 'GeForce GTX 1080 Ti'import torch
torch.cuda.device_count()
# 2

Cool. Enjoy

--

--