Recently bought a new computer, encountered some strange problems and stuck for a long time. Just document the steps for setting up the environment here.
GPU driver
Use the following commands to install the GPU drive.
sudo add-apt-repository ppa:graphics-drivers/ppa
sudo apt-get update
sudo apt-get install nvidia-driver-390
Install version 390 here, the version should align with CUDA. For the version alignment, please refer to the official website.
Reboot the machine after installation, and run nvidia-smi
, should able to see the status of the GPU.
It is said that downloading the driver from the official website will have problems in ubuntu, but I did not test it.
CUDA and cuDNN
Then, install CUDA 9.0 (official website). No 18.04 here, just choose 17.04. Remember that the version of CUDA and cuDNN must align with TensorFlow. The version alignment can be found in the TensorFlow offical website. Non latest CUDA can be found here.
I installed patch 1 and patch 2 by dpkg
:
sudo dpkg -i cuda-repo-ubuntu1704-9-0-local_9.0.176-1_amd64.deb
sudo dpkg -i cuda-repo-ubuntu1704-9-0-local-cublas-performance-update_1.0-1_amd64.deb
sudo dpkg -i cuda-repo-ubuntu1704-9-0-local-cublas-performance-update-2_1.0-1_amd64.deb
sudo apt-key add /var/cuda-repo-9-0-local/7fa2af80.pub
sudo apt-get update
sudo apt-get install cuda-9-0
Then, install cuDNN 7.0.5, also download from the official website. Choose Library for Linux
:
The downloaded file is a tgz file: cudnn-9.0-linux-x64-v7.tgz
. After decompression, you will get:
ls cuda/*
cuda/NVIDIA_SLA_cuDNN_Support.txt
cuda/include:
cudnn.h
cuda/lib64:
libcudnn.so libcudnn.so.7 libcudnn.so.7.0.5 libcudnn_static.a
Copy files in cuda
to /usr/local/cuda-9-0
, use cp -P
to keep the symbolic links while copying. And make libcudnn readable for all process:
sudo cp -P cuda/lib64/libcudnn* /usr/local/cuda-9.0/lib64/
sudo cp cuda/include/cudnn.h /usr/local/cuda-9.0/include/
sudo chmod a+r /usr/local/cuda-9.0/include/cudnn.h /usr/local/cuda/lib64/libcudnn*
libcupti
Install libcupti: sudo apt-get install libcupti-dev
. And add cuda path to the environment variable by adding the following lines to ~/.bashrc
.
export PATH="/usr/local/cuda-9.0/bin:$PATH"
export LD_LIBRARY_PATH="/usr/local/cuda-9.0/lib64:$LD_LIBRARY_PATH"
TensorFlow
Last step, install TensorFlow by pip, the newest version is 1.8.0:
pip install tensorflow-gpu
2020 update: you can install both CPU and GPU version of the latest Tensorflow 2.x by the following command:
pip install tensorflow
Test
from tensorflow.python.client import device_lib
device_lib.list_local_devices()
Prints:
Run device_lib.list_local_devices()
, you can see both cpu and gpu.
References
https://medium.com/@taylordenouden/installing-tensorflow-gpu-on-ubuntu-18-04-89a142325138
https://askubuntu.com/questions/1028830/install-cuda-on-ubuntu-18-04
https://github.com/tensorflow/tensorflow/issues/15817