Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
How to set up tensorflow with CUDA 8 cuDNN 5.1 in virtualenv with Python 3.5 on Ubuntu 16.04 http://ksopyla.com/2017/02/tensorflow-gpu-virtualenv-python3/
# This is shorthened version of blog post
# http://ksopyla.com/2017/02/tensorflow-gpu-virtualenv-python3/
# update packages
sudo apt-get update
sudo apt-get upgrade
#Add the ppa repo for NVIDIA graphics driver
sudo add-apt-repository ppa:graphics-drivers/ppa
sudo apt-get update
#Install the recommended driver (currently nvidia-378)
sudo ubuntu-drivers autoinstall
sudo reboot
#check if drivers were installed
nvidia-smi
#############################################
# Instal CUDA Toolkit 8.0 for x64 Ubuntu 16.04
wget -O cuda_8_linux.run https://developer.nvidia.com/compute/cuda/8.0/Prod2/local_installers/cuda_8.0.61_375.26_linux-run
sudo chmod +x cuda_8_linux.run
./cuda_8.0.61_375.26_linux.run
#Do you accept the previously read EULA?
#accept
#Install NVIDIA Accelerated Graphics Driver for Linux-x86_64 367.48?
#n (we installed drivers previously)
#Install the CUDA 8.0 Toolkit?
#y
#Enter Toolkit Location:
#/usr/local/cuda-8.0 (enter)
#Do you wish to run the installation with ‚sudo’?
#y
#Do you want to install a symbolic link at /usr/local/cuda?
#y
#Install the CUDA 8.0 Samples?
#y
#Enter CUDA Samples Location:
#enter
# Install cuDNN
# go to website and download cudnn-8 https://developer.nvidia.com/cudnn
tar -zxvf cudnn-8.0-linux-x64-v5.1.tgz
# copy libs to /usr/local/cuda folder
sudo cp -P cuda/include/cudnn.h /usr/local/cuda/include
sudo cp -P cuda/lib64/libcudnn* /usr/local/cuda/lib64
sudo chmod a+r /usr/local/cuda/include/cudnn.h /usr/local/cuda/lib64/libcudnn*
# isntall python 3 and virtual env
sudo apt install python3-pip
sudo apt install python3-venv
# create virtual environment for tensorflow
python3 -m venv tfenv
source tfenv/bin/activate
# Instal tensorflow package with gpu support
(tfenv)$ pip install tensorflow-gpu
#or CPU version
(tfenv)$ pip install tensorflow
# check installation, run simple python scipt from console
$ python
import tensorflow as tf
I tensorflow/stream_executor/dso_loader.cc:108] successfully opened CUDA library libcublas.so locally
I tensorflow/stream_executor/dso_loader.cc:108] successfully opened CUDA library libcudnn.so locally
I tensorflow/stream_executor/dso_loader.cc:108] successfully opened CUDA library libcufft.so locally
I tensorflow/stream_executor/dso_loader.cc:108] successfully opened CUDA library libcuda.so locally
I tensorflow/stream_executor/dso_loader.cc:108] successfully opened CUDA library libcurand.so locally
tf_session = tf.Session()
x = tf.constant(1)
y = tf.constant(1)
print(tf_session.run(x + y))

facundoq commented Aug 9, 2017

I think you should add that CUDA_HOME should be set and LD_LIBRARY_PATH modified for TF to find the libraries.

export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/local/cuda/lib64:/usr/local/cuda/extras/CUPTI/lib64"
export CUDA_HOME=/usr/local/cuda

You can add those lines to ~/.bashrc so that they are executed each time you open as shell.

You should run the following command before upgrading the driver automatically.
"sudo apt-get install ubuntu-drivers-common"

It is recommended to use cudnn version 6.0 to use the latest version of the tensorflow-gpu library.

There were some difficulties, but it was easy to install.
Thank you very much.

isalirezag commented Aug 28, 2017

I have a stupid question.
How to know what the link in wget -O cuda_8_linux.run https://developer.nvidia.com/compute/cuda/8.0/Prod2/local_installers/cuda_8.0.61_375.26_linux-run should be? where we get that from?
also any specific reason you didnot use sudo sh .... comand?

@isalirazeg:
0. Register a developer on Nvidia (its free of charge)

  1. Go to https://developer.nvidia.com/cuda-downloads
  2. Select your cuda version (normally about 1.4 - 1.9G)
  3. right-click to get the url link
  4. paste it to your terminal to download on your server

Is reboot necessary?

btw, the updated driver, didn't work for me ("modprobe: ERROR: could not insert 'nvidia_387_uvm': Unknown symbol in module, or unknown parameter (see dmesg)") , I had to yes | sudo apt-get remove nvidia-387

Up and running configuration for me:

Ubuntu 16.04.3 LTS
GeForce GTX 1080 Ti
TensorFlow: 1.3.0
Python: 3.5.2
nvidia driver: nvidia-387
CUDA: cuda_8.0.61_375.26 + cuda_8.0.61.2
cuDNN: cudnn-8.0-linux-x64-v6.0

Will I only be able to run CUDA & CuDNN in python3?. I am not able to see the above lines that in my machine. In fact after i import nothing happens. Any clue?
I tensorflow/stream_executor/dso_loader.cc:108] successfully opened CUDA library libcublas.so locally
I tensorflow/stream_executor/dso_loader.cc:108] successfully opened CUDA library libcudnn.so locally
I tensorflow/stream_executor/dso_loader.cc:108] successfully opened CUDA library libcufft.so locally
I tensorflow/stream_executor/dso_loader.cc:108] successfully opened CUDA library libcuda.so locally
I tensorflow/stream_executor/dso_loader.cc:108] successfully opened CUDA library libcurand.so locally

Aquakor commented Oct 22, 2017

@saikishor
Tensorflow supports python 2.7, 3.4, 3.5, 3.6. I don't know the answer for the second problem.

harpone commented Nov 8, 2017

it would be nice to add this wget method to get the cudnn file:
https://gist.github.com/mjdietzx/0ff77af5ae60622ce6ed8c4d9b419f45

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