Thursday, February 1, 2018

Installing Tensorflow 1.5 Xubuntu with NVIDIA GPU

Tensorflow 1.5 prerequisites on Xubuntu 17.10


This is a small guide to install Tensorflow 1.5 and Keras 2.1.3 on Xubuntu 17.10. If you are unsure what Linux distribution you have open a terminal and type the following command

lsb_release -a

It should output something like

Distributor ID: Ubuntu
Description: Ubuntu 17.10
Release: 17.10
Codename: artful

Install NVIDIA drivers


The easiest way to install NVIDIA graphics driver on Xubuntu is by using the Software Update panel. Start the Software Updater and press the button "Settings...".

Select the "Additional Drivers" tab and once a list of driver is displayed select and install the latest driver, something like this

"NVIDIA binary driver - version 384.90 from nvidia-384 (propietary,tested)"

Once the driver has been installed reboot. You can check that the driver has been properly installed by typing the following command after rebooting.

nvidia-smi


Remove old NVIDIA CUDA


Should you have a previously installed NVIDIA CUDA Toolkit you can remove it using the following commands. By default the Toolkit is installed in /usr/local/cuda/

sudo apt remove cuda
sudo apt purge cuda
sudo apt autoremove

Delete the remaining dirs in /usr/local/cuda

Install NVIDIA CUDA Toolkit 9.0


As described in the website

"The NVIDIA® CUDA® Toolkit provides a development environment for creating high performance GPU-accelerated applications. With the CUDA Toolkit, you can develop, optimize and deploy your applications on GPU-accelerated embedded systems, desktop workstations, enterprise data centers, cloud-based platforms and HPC supercomputers. The toolkit includes GPU-accelerated libraries, debugging and optimization tools, a C/C++ compiler and a runtime library to deploy your application."

Tensorflow 1.5 requires CUDA 9.0. Do not install CUDA 9.1 or later if you plan to use Tensorflow 1.5. Download the file

cuda-repo-ubuntu1704-9-0-local_9.0.176-1_amd64.deb

using the following URL

https://developer.nvidia.com/cuda-downloads

and run

sudo dpkg -i cuda-repo-ubuntu1704-9-0-local_9.0.176-1_amd64.deb 
sudo apt update
sudo apt install cuda

The toolkit will be installed together with a symlink into the following locations

/usr/local/cuda/
/usr/local/cuda-9.0/

Install LibCUDNN 7 for NVIDIA CUDA Toolkit 9.0


As described in the website

The NVIDIA CUDA® Deep Neural Network library (cuDNN) is a GPU-accelerated library of primitives for deep neural networks. cuDNN provides highly tuned implementations for standard routines such as forward and backward convolution, pooling, normalization, and activation layers. cuDNN is part of the NVIDIA Deep Learning SDK.

Download the file

cudnn-9.0-linux-x64-v7.tgz

using the following URL

https://developer.nvidia.com/cudnn

Then run the following commands to copy the required files into the same location where the Toolkit has been installed.

tar -zxvf cudnn-9.0-linux-x64-v7.tgz
sudo mv cuda/include/* /usr/local/cuda/include/
sudo mv cuda/lib64/* /usr/local/cuda/lib64/


Update .bashrc file


It is important to update you local .bashrc file to let the system know about the location of the CUDA libraries. Add the following line at the end

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

Install Anaconda 3


Anaconda is a virtual sandbox that allows you to install different developing environments with different version of Python, Tensorflow with CPU support, Tensorflow with GPU, ecc. 
It is easy to switch between developing environments and it is highly recommended.

Download the file

Anaconda3-5.0.1-Linux-x86_64.sh

from

https://www.anaconda.com/download/

Execute the following command with a normal user

chmod a+rwx Anaconda3-5.0.1-Linux-x86_64.sh
./Anaconda3-5.0.1-Linux-x86_64.sh

The script automatically installs Anaconda in ~/anaconda3. Let it modify the .bashrc by adding the following line, then restart all terminals.

export PATH=/home/giovanni/anaconda3/bin:$PATH

Create a conda virtual environment with the command

conda create --name tensorflow

and activate it with

source activate tensorflow


Install latest Keras and Tensorflow


After the conda command "source activate tensorflow", you can install Tensorflow with GPU support and Keras.

pip install --upgrade tensorflow-gpu==1.5.0
pip install --upgrade keras==2.1.3

To check that they are correctly installed and working type the following commands.

python -c 'import keras; print(keras.__version__)'
python -c 'import tensorflow as tf; print(tf.__version__)'

No comments:

Post a Comment