Set up deep learning system for Ubuntu with Nvidia GPU

To prepare deep learning system in Ubuntu with Nvidia GPU, we need to install the following prerequisite.

Then, we can set up deep learning environments:

Install Prerequisite

Install Nvidia driver

To download GPU driver, we can go to this link, choose our GPU/operation, and click serach. Then we can download the correspond driver. For a linux with GTX Titan X, the newest driver name should be NVIDIA-Linux-x86_64-384.90.run as date of 2017-10-09. Move to the folder that the driver downloaded and run the following script to install it.

chmod +x NVIDIA-Linux-x86_64-384.90.run
# We need root to install the driver ("sudo" can give us root right, need to type password).
sudo ./NVIDIA-Linux-x86_64-384.90.run

Install Cuda toolkit

Cuda 9.0 is recently released, unfortunately however, most dl platform currently only support cuda 8.0. Therefore, we need to download the archived release from here. On this website, we can download the installer after picking up the operation system (usually choose x86_64 as Architecture). The “deb(local)” isntaller is recommanded. To install it, simply type the following script:

#  file name might vary depends on version of operation system and cuda
sudo dpkg -i cuda-repo-ubuntu1604-8-0-local-ga2_8.0.61-1_amd64.deb
sudo apt-key add /var/cuda-repo-8.0.61-1/7fa2af80.pub
sudo apt-get update
sudo apt-get install cuda

More details about installing cuda is avaliable here

Install cuDNN

After downloaded Nvidia driver and Cuda toolkit, we can install cnDNN. Newest version of cuDNN can be downloaded from here, people need to register before downloading them. We can download the cuDNN v7.0 Library for CUDA 8.0 for Linux, this link will give you file named “cudnn-8.0-linux-x64-v7.tgz”. Then we can use the following scripts to install cuDNN.

tar -xzvf cudnn-9.0-linux-x64-v7.tgz
sudo cp cuda/include/cudnn.h /usr/local/cuda/include
sudo cp cuda/lib64/libcudnn* /usr/local/cuda/lib64
sudo chmod a+r /usr/local/cuda/include/cudnn.h /usr/local/cuda/lib64/libcudnn*

More details about installing cuDNN is avaliable here.

Set up deep learning environment

Usually, to avoid any package conflictions between different platforms or users, we expect to establish independent environment for different users, platforms, or projects. Conda is a package, dependency, and environment management platform that can easily achieve this goal. We also can use it to easily manage and use different version of softwares.

Install Conda

To install conda, we have two options: anaconda and miniconda. Compare to miniconda, anaconda has more preinstalled packages. Here, take miniconda as an example, we can run the following command to install miniconda.

wget https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh
bash Miniconda3-latest-Linux-x86_64.sh

This will install conda for the current user.

Create a conda environment

Then, we can create a new conda environment for tensorflow. As an example, we create a environment named “tf“(any other name is OK) using the following script.

conda create -name tf python=3.6

After creating a env, you can enter this env by:

source activate tf

After entering the env, you can install packages (take installing numpy as an example) for this env by:

conda install numpy

Install through conda is usually better than from pip because of less conflict and more comprehensize dependencies.

Install deep learning platforms

You might install any of the following platforms you like. I recommend you to create an independent conda env for each platform (except Keras).

Tensorflow

After creating a env named tf in above section, we can enter that environment by the following command:

source activate tf

And install the newest GPU version of tensorlfow by:

pip install --ignore-installed --upgradehttps://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-1.3.0-cp27-none-linux_x86_64.whl

Installing tensorflow without conda can be tricky, more details can be found here

pytorch

Pytorch is very easy to install on Linux. After entering a conda env, simply type the following command:

conda install pytorch torchvision cuda80 -c soumith

Keras

Before installing Keras, you need to have at least one of tensorflow or CNTK installed as backend (Theano is not recommended anymore) in your conda env. At the same conda env, you can use the following command to install Keras.

pip install keras

If this doesn’t work (for any reason), try the alternative command:

conda install -c conda-forge keras

After installing keras, we need to pick a backend. Keras use tensorflow by default. We can change it by edit the “~/.keras” file.

CNTK

CNTK, thought not very popular, is a great option for Keras backend because it runs much faster than current version of tensorflow. It can be downloaded and installed by the following commands (after entering a conda env).

sudo apt-get install openmpi-bin
pip install https://cntk.ai/PythonWheel/GPU-1bit-SGD/cntk-2.2-cp36-cp36m-linux_x86_64.whl

Install frequently used packages

opencv

For python 3.6, opencv cannot be directly installed by using

conda install opencv

Instead, we need to the following script.

conda install -c menpo opencv3

However, this is still not guranteed to work, you might want to do some Google to search for new options.

Written on October 9, 2017