The Ultimate Ubuntu Deep Learning Installation Guide (CUDA, Tensorflow, Keras, Opencv, PyTorch)

Source: Deep Learning on Medium

The Ultimate Ubuntu 18.04 GPU Deep Learning Installation Guide (CUDA, cuDNN, Tensorflow, Keras, Opencv, PyTorch)

This tutorial is tested on multiple 18.04.2 and 18.04.3 PCs with RTX2080ti. All the commands in this tutorial will be done inside the “terminal”.

Purpose: A robust and all-in-one deep-learning Ubuntu setup guide with Python3.6 (beginner friendly).

GPU Requirement: Nvidia cards (G8-series onward).

Recommended Hardware: RAM (DDR3/4, 16GB+), CPU (2.9Ghz+ @ turbo), GPU style (open-air for 1*GPU; blower for 2+GPUs), PSU (750w for 1*GPU; 1000w+ for 2*GPU).

ML Package Focus: CUDA10, cuDNN7.5, Tensorflow-gpu1.14, Keras, Opencv3.4.6 (4.0.1), PyTorch1.2.

Side Note: Anaconda is not recommended, Python3.6 (or older) is preferred.

Installation Content

  1. Ubuntu Setup
  2. Nvidia (Driver, CUDA, cuDNN)
  3. Virtual Environment
  4. Tensorflow-gpu + Keras (GPU accelerated)
  5. Opencv (ditto)
  6. Pytorch (ditto)

1. Ubuntu Setup

This is step gets your system ready for the following installations. Just copy & paste the codes, it’s a lot easier. “sudo” gives you command the admin right, doing things without restrictions.

Note: At this point, it’s possible that your screen resolution is low and the system seems lagging. This is normal, just make your browser window smaller before we install the Nvidia driver.

sudo apt-get update
sudo apt-get upgrade

sudo apt-get install libjpeg-dev libpng-dev libtiff-dev
sudo apt-get install build-essential cmake unzip pkg-config
sudo apt-get install libxmu-dev libxi-dev libglu1-mesa libglu1-mesa-dev
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
sudo apt-get install libgtk-3-dev
sudo apt-get install libhdf5-serial-dev
sudo apt-get install libxvidcore-dev libx264-dev
sudo apt-get install libopenblas-dev libatlas-base-dev liblapack-dev gfortran
sudo apt-get install python3-dev python3-tk python-imaging-tk

2. Nvidia (Driver, CUDA, cuDnn)

It’s better to install CUDA before the display driver; otherwise, CUDA installation error might occur. We’ll not be using CUDA 10.1 because of its incompatibility with some deep learning packages.

sudo add-apt-repository ppa:graphics-drivers/ppa
sudo apt-get update

Install CUDA Toolkit

CUDA 10.0: https://developer.nvidia.com/cuda-10.0-download-archive (download)

Go to the folder where your file is downloaded, right click in the empty space, and choose “open in terminal”.

Filename: cuda_10.0.130_410.48_linux.run

chmod +x cuda_10.0.130_410.48_linux.run
sudo ./cuda_10.0.130_410.48_linux.run — override

Answering the installer: It might alert you pre-installation/GCC version/32bit-installation errors. These aren’t serious alerts, so just answer to ignore them and proceed.

  • Press [space-bar] to scroll down to the bottom and accept Terms.
  • Press [n] for “Install NVIDIA Accelerated Graphics Driver” (we’ll install manually later) & “X config” (last question).
  • Press [y] for everything else.

Add CUDA to Environment Path

“nano” is a text-editor within the “terminal”. “~/.bashrc” is the path and filename.

nano ~/.bashrc

Scroll to the bottom and add:

# NVIDIA CUDA Toolkit
export PATH=/usr/local/cuda-10.0/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda-10.0/lib64

Save and exit the ~/.bashrc file

crtl + o
[enter]
crtl + x

Update the system path file

source ~/.bashrc

Check that CUDA is properly installed

nvcc -V

How to know if installation failed: output “No command ‘nvcc’ found”

Install Nvidia Driver

Before finishing this step, your computer display should not be able to have HD resolution. If you do, that’s because your Ubuntu automatically installed the Nouveu driver for you (Not a good thing)

The fix: https://linuxconfig.org/how-to-disable-nouveau-nvidia-driver-on-ubuntu-18-04-bionic-beaver-linux

Nvidia driver: https://www.nvidia.com/Download/Find.aspx (download)

I recommend version 430.4 because I haven’t ran into any compatibility issues with it so far.

Filename: cuda_10.0.130_410.48_linux.run

chmod +x NVIDIA-Linux-x86_64–430.40.run
sudo ./ NVIDIA-Linux-x86_64–430.40.run

Reboot

sudo reboot

Now your screen resolution should be in HD

Install cuDNN

This is the NVIDIA CUDA® Deep Neural Network library. It will be used for installing the gpu-accelerated libraries (eg. opencv)

Signup & download cuDNN: https://developer.nvidia.com/cudnn

After the download, rename (if needed) the file extension to “.tgz”

tar -zxf
cd cuda
sudo cp -P lib64/* /usr/local/cuda/lib64/
sudo cp -P include/* /usr/local/cuda/include/
cd ~

3. Virtual Environment (VE)

Virtual environment is useful when one of your AIs requires opencv4.0.1, while another requires opencv3.4.6. In this case, if you created two python virtual environments, namely cv3 and cv4, then you wouldn’t have to reinstall opencv versions everytime; Instead, you could just type:

workon cv3

or

workon cv4

Install Virtual Environment (VE)

wget https://bootstrap.pypa.io/get-pip.py
sudo python3 get-pip.py
wget https://bootstrap.pypa.io/get-pip.py
sudo python3 get-pip.py
sudo pip install virtualenv virtualenvwrapper
sudo rm -rf ~/get-pip.py ~/.cache/pip

Add VE to System Environment Path (like CUDA)

nano ~/.bashrc

Scroll to the bottom and add:

# virtualenv and virtualenvwrapper
export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
source /usr/local/bin/virtualenvwrapper.sh

Save and exit the ~/.bashrc file

crtl + o
[enter]
crtl + x

Update the system file

source ~/.bashrc

Create VE

Let’s create a virtual environment called cv4 that uses python3

mkvirtualenv cv4 -p python3

DONE! Just write “workon cv4” whenever you want to activate this environment.

4. Tensorflow-gpu + Keras (GPU accelerated)

Go to your newly created environment. Only pip install everything after the VE is activated, using the following command:

workon cv4

Pip Install Python libraries

Note: we’ll be using tensorflow1.14.0

pip install numpy
pip install pandas scipy matplotlib pillow
pip install scikit-learn scikit-image
pip install tensorflow-gpu==1.14.0
pip install keras
pip install imutils h5py requests progressbar2

Feel free to pip install your other usual python packages, EXCEPT for opencv and pytorch. We will be “compiling” them instead of “pip-install”-ing later.

How to know if installation failed:

In your virtual environment, open python and write

python
import tensorflow as tf
tf.test.is_gpu_available()
tf.test.is_built_with_cuda()

It should print your GPU details and a true/false for CUDA capabilities, respectively.

Opencv (w/ GPU)

This is the most time-consuming part of the guide.

Will be updated soon.

Pytorch (w/ GPU)

Will be updated soon.