I found the tensorflow documentation rather lacking for installation instructions, especially in regards to getting GPU support. I’m going to write down my notes from wrangling with the installation here for future reference and hopefully this helps someone else too.
This will invariably go out-of-date at some point, so be mindful of the publish date of this post. Make sure to cross-reference other documentation that has more up-to-date information.
These instructions are very specific to my environment, so this is what I am assuming:
uname -a
uname -m
. (should say x86_64
)lspci |
grep -i nvidia
uname -r
gcc --version
update-manager
).
(I have version 375.66 installed)sudo apt-get install linux-headers-$(uname -r)
to install them
if you don’t have them installed already.python3
installed.
python3
by running sudo apt-get install python3
. This
will install Python 3.5.NVIDIA has a big scary documentation page on this, but I will summarize the only the parts you need to know here.
Go to the CUDA Toolkit Download page. Click Linux > x86_64 > Ubuntu > 16.04 > deb (network).
Click download and then follow the instructions, copied here:
sudo dpkg -i cuda-repo-ubuntu1604_8.0.61-1_amd64.deb
sudo apt-get update
sudo apt-get install cuda
This will install CUDA 8.0. It installed it to the directory
/usr/local/cuda-8.0/
on my machine.
There are some post-install actions we must follow:
~/.bashrc
gedit ~/.bashrc
, nano ~/.bashrc
, vim
~/.bashrc
, whatever.# CUDA 8.0 (nvidia) paths
export CUDA_HOME=/usr/local/cuda-8.0
export PATH=/usr/local/cuda-8.0/bin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda-8.0/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
source ~/.bashrc
.cuda-install-samples-8.0.sh
~/
.
deviceQuery
binary which can be ran to test if
everything is working properly.nvcc -V
outputs something.
cd ~/NVIDIA_CUDA-8.0_Samples
, cross your fingers, and run make
/usr/bin/ld:
cannot find -lnvcuvid
I think that doesn’t really matter because the
binary files were still output.~/NVIDIA_CUDA-8.0_Samples/bin/x86_64/linux/release/deviceQuery
to see if you get any output. Hopefully you will see your GPU listed.This AskUbuntu answer has good instructions. Here are the instructions specific to this set-up:
cd ~/Downloads
tar -xvf cudnn-8.0-linux-x64-v5.1.tgz
cd cuda
sudo cp -P include/* /usr/local/cuda-8.0/include/
sudo cp -P lib64/* /usr/local/cuda-8.0/lib64/
sudo chmod a+r /usr/local/cuda-8.0/lib64/libcudnn*
This one is simple. Just run:
sudo apt-get install libcupti-dev
I recommend using virtualenvwrapper to create the tensorflow virtualenv, but the TensorFlow docs still have instructions to create the virtualenv manually.
~/.bashrc
.mkvirtualenv --python=python3 tensorflow
If you just run pip install tensorflow
you will not get GPU support. To
install the correct version you will have to install from a particular
url. Here is the
install command you will have to run to install TensorFlow 1.2 for Python 3.5
with GPU support:
pip install https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-1.2.0-cp35-cp35m-linux_x86_64.whl
If you need a different version of TensorFlow, you can edit the version number
in the URL. Same with the Python version (change cp35
to cp36
to install for
Python 3.6 instead, for example).
Save this script from the TensorFlow
tutorials
to a file called test_gpu.py
:
# Creates a graph.
with tf.device('/cpu:0'):
a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')
c = tf.matmul(a, b)
# Creates a session with log_device_placement set to True.
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
# Runs the op.
print(sess.run(c))
And then run it:
python test_gpu.py
You should see your GPU card listed under “Device mapping:” and that each task
in the compute graph is assigned to gpu:0
.
If you see “Device mapping: no known devices” then something went wrong and TensorFlow cannot access your GPU.