Configuring a GPU powered machine with Ubuntu

Arun Rajan
5 min readNov 19, 2018

--

Prior moving into the configuration process, I will provide a brief introduction about my system at work. System specifications are as follows:

RAM — 16 GB
Processor — Intel® Core™ i5–7400 CPU @ 3.00GHz × 4
Graphics — GeForce GTX 1060 6GB/PCIe/SSE2
OS type — 64-bit
DISK space — 256 GB

My work at logicalsteps involves lot much of training and testing of various AI models, hence I need to put my GPU for maximum utilisation. The system configuration process involves the following step:

1. Ubuntu 16.04 installation
2. Install nvidia driver and verify
3. Install cuda and verify
4. Install cuDNN and verify
5. Install python and dependent libraries through anaconda platform
6. Verify GPU utilisation

1. Ubuntu 16.04 installation

Installing Ubuntu in the system requires a bootable pen drive for Ubuntu. Download the Ubuntu desktop image from this link. Once the download is complete, set up a bootable pen drive. In windows, bootable pen drive could be created using Rufus application. In Ubuntu, bootable pen drive could be created using startup disk creator.

Rufus/ Startup disk creator

Once the bootable pen drive is ready, reboot the system. Try holding F12 when your computer, first starts. With most machines this will allow you to select the USB device from a system-specific boot menu. Once the Ubuntu home page is displayed, it will prompt for installation options. Opt for Normal installation. Also enable both ‘Download updates’, and ‘Install third party software’. Select the installation type from the options provided and click install. For a fresh start opt for ‘Erase disk and install Ubuntu’. Provide details required when prompted. Once the installation is done, remove the USB stick and reboot.

2. Install Nvidia driver and verify

Prior installing Nvidia driver, one needs to create PPA repository to include specific Nvidia drivers for Ubuntu. Follow the commands in terminal to complete the installation of Nvidia drivers.

$ sudo add-apt-repository ppa:graphics-drivers/ppa
$ sudo apt update
$ sudo apt-get install build-essential
$ sudo apt-get install nvidia-driver-410
$ sudo apt-get install mesa-common-dev
$ sudo apt-get install freeglut3-dev

Prevent automatic updates that might break the drivers.Do this by removing the graphics-drivers PPA from your software sources

$ sudo add-apt-repository — r ppa:graphics-drivers/ppa

Reboot and verify nvidia installation using the following steps:

$ lsmod | grep nvidia

Should return something similar:

Make sure the Nouveau drivers are disabled. Enter the command as follows and nothing should be displayed.

$ lsmod | grep nouveau

Verify the driver installation using Nvidia-smi tool

$ watch -n 1 nvidia-smi

Above watch command refreshes every second. Nvidia-smi should display something similar:

3. Install Cuda and verify

Download Cuda (9.0 preferred) run file for the following target platform.

OS — Linux
Architecture — x86_64
Distribution — Ubuntu
Version — 16.04
Installer type — runfile(local)

Open terminal and navigate to the cuda run file download location and run

$ sudo sh cuda_9.0.176_384.81_linux.run

You will be asked a series of y/n questions. It is crucial that you do NOT install the Cuda driver when asked, otherwise it will overwrite the 410.48 driver which we installed first. So select (n)o when this question arises:

Install NVIDIA Accelerated Graphics Driver for Linux-x86_64 …?(y)es/(n)o/(q)uit: n

The remainder of the questions ask whether you want to install the CUDA 9.0 Toolkit (yes), the Toolkit Location (keep the default), a symbolic link (yes) and whether to install the Samples (yes — it helps for testing, put them in the default location asked). You will also see a warning similar to the following:

***WARNING: Incomplete installation! This installation did not install the CUDA Driver…

Do not be alarmed as that is simply the script telling us that no driver was installed by the script. We have already installed the necessary Nvidia driver above. Final steps involves editing ~/.bashrc using the command below to modify PATH and LD_LIBRARY_PATH

$ sudo gedit ~/.bashrc

Append the following two lines to the end of the file:

export PATH=/usr/local/cuda-9.0/bin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda-9.0/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}

Close the bashrc file and run the following command to load the modified environment variables.

$ source ~/.bashrc

For verifying Cuda installation, go to the Cuda samples location as selected while Cuda installation and follow:

$ cd NVIDIA_CUDA-9.0_Samples_Backup
$make
cd 1_Utilities/deviceQuery
$ ./deviceQuery

Cuda installation, if successful, the above query should display something similar as below:

4. Install cuDNN and verify

Download cuDNN run time library, developer library and code samples. Navigate to the download directory containing cuDNN Debian file. Install the run time library:

$ sudo dpkg -i libcudnn7_7.4.2.24–1+cuda9.0_amd64.deb

Install the developer library:

$ sudo dpkg -i libcudnn7-dev_7.4.2.24–1+cuda9.0_amd64.deb

Install the code samples and the cuDNN Library User Guide:

$ sudo dpkg -i libcudnn7-doc_7.4.2.24–1+cuda9.0_amd64.deb

Copy the code samples to home ($HOME) location.

$ cp -r /usr/src/cudnn_samples_v7/ $HOME
$ cd $HOME/cudnn_samples_v7/mnistCUDNN
$ make clean && make
$ ./mnistCUDNN

If cuDNN is properly installed and running on your Linux system, you will see a message similar to the following:

Test Passed!

Reboot the system to prepare it for anaconda installation.

5. Install python and dependent libraries through anaconda platform

Download and install Anaconda latest version for Linux from here.

Go to the downloaded directory in terminal and enter

$ sudo sh Anaconda3–5.3.0-Linux-x86_64.sh

Restart the terminal. For a better usage of python libraries, use virtual environments. I would be demonstrating based on such virtual environments. Choose a name for the virtual environment. Since I work in deep learning projects extensively, I chose to name the virtual environment — deeplearning.

$ conda create -name deeplearning
$ source activate deeplearning

Once the virtual environment prompt is active, install opencv, tensorflow, keras libraries.

(deeplearning)$ conda install -c conda-forge opencv
(deeplearning)$ conda install -c anaconda tensorflow-gpu
(deeplearning)$ conda install -c anaconda keras

When prompted for confirmation for installating dependencies, enter (Y)es.

6. Verify GPU utilisation

Open python from the virtual environment by entering the following:

(deeplearning)$ python

Enter the following commands into the python console:

from tensorflow.python.client import device_libdef get_available_gpus():
local_device_protos = device_lib.list_local_devices()
return [x.name for x in local_device_protos if x.device_type==‘GPU’]
listGPU = get_available_gpus()

It should list the GPU available. The result would like below:

Now your system is ready to develop some fine deep learning models. Enjoyyyyyy deep learning :)

--

--