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

DeepLCH
9 min readNov 20, 2019

--

Last update: 1st March 2023

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 -y
sudo apt-get install -y build-essential cmake unzip pkg-config
sudo apt-get install -y libxmu-dev libxi-dev libglu1-mesa libglu1-mesa-dev
sudo apt-get install -y libjpeg-dev libpng-dev libtiff-dev
sudo apt-get install -y libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
sudo apt-get install -y libxvidcore-dev libx264-dev
sudo apt-get install -y libgtk-3-dev
sudo apt-get install -y libopenblas-dev libatlas-base-dev liblapack-dev gfortran
sudo apt-get install -y libhdf5-serial-dev graphviz
sudo apt-get install -y python3-dev python3-tk python-imaging-tk
sudo apt-get install -y linux-image-generic linux-image-extra-virtual
sudo apt-get install -y linux-source linux-headers-generic

A nice hack: copy&paste the code above and save as a xxxxx.sh file, then run them all in one go.

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:

  • 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).
  • Press [y] for everything else.

Add CUDA to Environment Path

“nano” is a text-editor within the “terminal”. “~/.bashrc” is the path and filename. The file is a shell script for Bash (or the Linux Shell) to run.

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 by typing

crtl + o
[enter]
crtl + x

Update the system path file

source ~/.bashrc

Check that CUDA is properly installed

nvcc -V

How to know if the installation worked:

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

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 [n] for “X config” (last question).
  • Press [y] for everything else.

Reboot

sudo reboot

Now your screen resolution should be in HD. Check this in the terminal

nvidia-smi

Note: It does say “CUDA Version: 10.1”, we actually have 10.0. We know this from the previous section.

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 cudnn-10.0-linux-x64-v7.6.1.34.tgz
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)

sudo apt install python3-testresourceswget 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 the installation worked:

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.

Keras’ Saving Error: It’s possible that when you save your Keras model weight an issue would occur. Here’s the fix, go to “site-packages/keras/engine/saving.py”. Find the lines with “raise TypeError(‘Not JSON Serializable:’, obj)”, around line 120.

Add this import to the file

from tensorflow.python.framework.tensor_shape import Dimension

Modify the codes to (with [tab] before “return”):

# if obj is a python ‘type’
if type(obj) == Dimension:
return int(obj.value or 0)

5. Opencv (w/ GPU)

This is the most time-consuming part of the guide. Instead of using “pip”, we will be compiling the python library from it’s sources code. So that we can add the opencv “contrib” capabilities to it.

Why not “pip install”: You don’t get the CUDA capabilities. And compiling allows you to accommodate different opencv (and contrib) versions’s built-in functions.

If you have already “pip install”-ed, run this before proceeding

pip uninstall opencv-python

We will be using the opencv4.0.1 (if you needed other versions, just change 4.0.1 => [your version #])

Let install some more dependencies

sudo add-apt-repository “deb http://security.ubuntu.com ubuntu xenial-security main”
sudo apt update
sudo apt install libjasper1 libjasper-dev
sudo apt-get update && sudo apt-get upgrade — fix-missing — fix-broken
sudo apt-get install libgtk2.0-dev
sudo apt-get install qtcreator
sudo apt-get install qtdeclarative5-dev

Use commands to download 2 files from the official opencv github

wget -O opencv.zip https://github.com/opencv/opencv/archive/4.0.1.zipwget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.0.1.zipunzip opencv.zip
unzip opencv_contrib.zip
mv opencv-4.0.1 opencv
mv opencv_contrib-4.0.1 opencv_contrib
cd ~/opencv
mkdir build
cd build

Prepare to compile

Note: If your VE name is not “cv4”, please change the last line

cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
-D WITH_CUDA=ON \
-D BUILD_NEW_PYTHON_SUPPORT=ON \
-D BUILD_PYTHON_SUPPORT=ON \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D INSTALL_C_EXAMPLES=ON \
-D BUILD_EXAMPLES=ON \
-D OPENCV_ENABLE_NONFREE=ON \
-D BUILD_opencv_cudacodec=OFF \
-D ENABLE_FAST_MATH=1 \
-D CUDA_FAST_MATH=1 \
-D WITH_OPENGL=ON \
-D WITH_TBB=ON \
-D WITH_V4L=ON \
-D WITH_QT=ON \
-D WITH_GTK=ON \
-D BUILD_opencv_python3=ON \
-D PYTHON3_PACKAGES_PATH=/home/dev/.virtualenvs/cv4/lib/python3.6/site-packages \
-D PYTHON3_NUMPY_INCLUDE_DIRS=/home/dev/.local/lib/python3.6/site-packages/numpy/core/include \
-D PYTHON_EXECUTABLE=~/.virtualenvs/cv4/bin/python3.6 ..

Potential Error1: If it says it cannot locate the python3 directory, please remove the “ ..” at the end and retype it yourself.

Potential Error2: If the numpy version (my version is 1.17.4 ,see the second image below) is blank, that means your compiler cannot find the numpy directory. Either find it yourself somewhere inside “…/dist-packages/…” or try

sudo pip uninstall numpy

It will show similar output as below, then you will see that the numpy folder might be in “~/.local”. Edit your “-D PYTHON3_NUMPY_INCLUDE_DIRS=” accordingly and cmake again

The image below is what a successful cmake should look like.

Important: DO NOT PROCEED if you can’t see a reasonable path for “numpy” & “install path”. Otherwise, it won’t work with python.

Compilation

Note: -j16” means your cpu has 16 threads (change this accordingly). The “make-j16” could take up to an hour to finish.

make -j16

Important: Last chance to check, take note of the last line of output, it will tell you part of the path to the final .so file (cv2.cpython-36m-x86_64-linux-gnu.so).

sudo make install
sudo ldconfig

Go to the folder in the terminal, and rename the file

cd /usr/local/lib/python3.6/site-packages/cv2/python-3.6
sudo mv cv2.cpython-36m-x86_64-linux-gnu.so cv2.so

In case of file not found: Check the partial path you read from the “make -j16” output, and manually find the file from guesstimation. Here are some possible locations:

/usr/local/python/cv2/python-3.6
~/opencv/build/lib/python3

If you still can’t find the file try:

sudo find / -name "cv2.cpython-36m-x86_64-linux-gnu.so"

Go to your virtual environment python site-packages folder, and sym-link the compiled cv2.so file

Note: if your VE name is not “cv4, modify the first line below, where …/cv4/…

cd ~/.virtualenvs/cv4/lib/python3.6/site-packages/
ln -s /usr/local/python/cv2/python-3.6/cv2.so cv2.so

How to know if the installation worked:

workon cv4
python
import cv2
cv2.__version__

The final command should output ‘4.0.1’ (your compiled version)

6. Pytorch (w/ GPU)

Finally, I recommend pytorch1.2.0 (known as “torch” in “pip install”) with torchvision0.4.0.

pip install torch==1.2.0 
pip install torchvision==0.4.0

Updated 2023: As this article assumes you are using the RTX 20## series GPU and Tensorflow GPU v1.14. Therefore your CUDA version should be v10.1

Please run the installation code below for compatibility (if you do not want a separate virtual environment):

# To be compatible with TF1.14 w/GPU
pip install torch==1.8.1+cu101 torchvision==0.9.1+cu101 -f https://download.pytorch.org/whl/torch_stable.html

— END —

--

--

DeepLCH

Deep learning Scientist, specialized in computer vision and data analytics.