• Home
  • >
  • Deep Learning
  • >
  • Installing Deep Learning Frameworks on Ubuntu with CUDA support

Installing Deep Learning Frameworks on Ubuntu with CUDA support

In this article, we will learn how to install Deep Learning Frameworks like TensorFlow and PyTorch on a machine having a NVIDIA graphics card. If you have a brand new computer with a graphics card and you don’t know what libraries to install to start your deep learning journey, this

Image with logos
Image with logos

In this article, we will learn how to install Deep Learning Frameworks like TensorFlow and PyTorch on a machine having a NVIDIA graphics card.

If you have a brand new computer with a graphics card and you don’t know what libraries to install to start your deep learning journey, this article will help you.

We will install CUDA, cuDNN, Python 3, TensorFlow, Pytorch, OpenCV, Dlib along with other Python Machine Learning libraries step-by-step. Note, that if you would like to use TensorFlow with Keras support, there is no need to install Keras package separately, since from TensorFlow2.0 Keras comes as tensorflow.keras submodule.

We have tested the instructions on a system with the following configuration:

Processor : Intel core i7 6850K with 6 cores and 40 PCIe lines
Motherboard : Gigabyte X99P – SLI
RAM : 32 GB
Graphics Card : Zotac GeForce GTX 1080 Ti with 11 GB RAM

We will be assuming Ubuntu 16.04 installation. i.e nothing has been installed on the system earlier.

Step 1 : Install Prerequisites

Before installing anything, let us first update the information about the packages stored on the computer and upgrade the already installed packages to their latest versions.

sudo apt-get update
sudo apt-get upgrade

Next, we will install some basic packages which we might need during the installation process as well in future. Also, remove the packages which are not needed.

sudo apt-get install -y build-essential cmake gfortran git pkg-config
sudo apt-get install -y python-dev software-properties-common wget vim
sudo apt-get autoremove

Step 2 : Install CUDA

CUDA ( Compute Unified Device Architecture ) is a parallel computing platform and API developed by NVIDIA which utilizes the parallel computing capabilities of the GPUs. In order to use the graphics card, we need to have CUDA drivers installed on our system.

If you do not have a NVIDIA CUDA supported Graphics Card, then you can skip this step. and go to Step 4.

Download the CUDA driver from the official nvidia website. We recommend you download the deb ( local ) version from Installer type as shown in the screenshot below.

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

After downloading the file, go to the folder where you have downloaded the file and run the following commands from the terminal to install the CUDA drivers.

Please make sure that the filename used in the command below is the same as the downloaded file.

sudo dpkg -i cuda-repo-ubuntu1604-8-0-local-ga2_8.0.61-1_amd64.deb
sudo apt-get update
sudo apt-get install -y cuda-8.0

Run the following command to check whether the driver has installed successfully by running NVIDIA’s System Management Interface (nvidia-smi). It is a tool used for monitoring the state of the GPU.

nvidia-smi

You should get an output as shown below.

output of nvidia-smi command

As a side note, I found that apart from getting better resolution options for display, installing the CUDA driver lowers the power consumption of the graphics card from 71W to 16W for a NVIDIA GTX 1080 Ti GPU attached via PCIe x16.

Step 3 : Install cuDNN

CUDA Deep Neural Network (cuDNN) is a library used for further optimizing neural network computations. It is written using the CUDA API.

Go to official cudnn website and fill out the form for downloading the cuDNN library. After you get to the download link ( sample shown below ), you should download the “cuDNN v6.0 Library for Linux” from the options.

https://developer.nvidia.com/rdp/cudnn-download

Now, go to the folder where you have downloaded the “.tgz” file and from the command line execute the following.

tar xvf cudnn-8.0-linux-x64-v6.0.tgz
sudo cp -P cuda/lib64/* /usr/local/cuda/lib64/
sudo cp cuda/include/* /usr/local/cuda/include/

Next, update the paths for CUDA library and executables:

echo 'export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/local/cuda/lib64:/usr/local/cuda/extras/CUPTI/lib64"' >> ~/.bashrc
echo 'export CUDA_HOME=/usr/local/cuda' >> ~/.bashrc 
echo 'export PATH="/usr/local/cuda/bin:$PATH"' >> ~/.bashrc 
source ~/.bashrc

This should get everything sorted out with respect to CUDA and cuDNN

Step 4 : Install requirements for DL Frameworks

Install dependencies of Deep Learning Frameworks:

sudo apt-get update
sudo apt-get install -y libprotobuf-dev libleveldb-dev libsnappy-dev libhdf5-serial-dev protobuf-compiler libopencv-dev

NOTE : If you get a warning saying the following:

/usr/lib/nvidia-375/libEGL.so.1 not a symbolic link

Then execute the below commands:

sudo mv /usr/lib/nvidia-375/libEGL.so.1 /usr/lib/nvidia-375/libEGL.so.1.org
sudo mv /usr/lib32/nvidia-375/libEGL.so.1 /usr/lib32/nvidia-375/libEGL.so.1.org
sudo ln -s /usr/lib/nvidia-375/libEGL.so.375.82 /usr/lib/nvidia-375/libEGL.so.1
sudo ln -s /usr/lib32/nvidia-375/libEGL.so.375.82 /usr/lib32/nvidia-375/libEGL.so.1

Next, we install python 3 along with other important packages like boost, lmdb, glog, blas etc.

sudo apt-get install -y --no-install-recommends libboost-all-dev doxygen
sudo apt-get install -y libgflags-dev libgoogle-glog-dev liblmdb-dev libblas-dev
sudo apt-get install -y libatlas-base-dev libopenblas-dev libgphoto2-dev libeigen3-dev libhdf5-dev 

sudo apt-get install -y python3-dev python3-pip python3-nose python3-numpy python3-scipy

Step 5 : Enable Virtual Environments

Most of us work on different projects and like to keep the settings for these projects separate too. This can be done using virtual environments in Python. In a virtual environment, you can install any python library without affecting the global installation or other virtual environments. This way, even if you damage the libraries in one virtual environment, your rest of the projects remain safe. It is highly recommended to use virtual environments.

Install the virtual environment wrapper which enables us to create and work on virtual environments in python.

sudo pip3 install virtualenv virtualenvwrapper
echo "# Virtual Environment Wrapper" >> ~/.bashrc
echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bashrc
source ~/.bashrc

Step 6 : Install Deep Learning frameworks

Now, we install Tensorflow, PyTorch, dlib along with other standard Python ML libraries like numpy, scipy, sklearn etc.
We will create virtual environments and install all the deep learning frameworks inside them. We create a separate environment for Python 3:

# create a virtual environment for python 3
mkvirtualenv virtual-py3 -p python3
# Activate the virtual environment
workon virtual-py3

pip install numpy scipy matplotlib scikit-image scikit-learn ipython protobuf jupyter

# If you do not have CUDA installed
pip install tensorflow
# If you have CUDA installed
pip install tensorflow-gpu 

pip install torch
pip install dlib

deactivate

Check Installation of Frameworks

workon virtual-py3
python
import numpy
numpy.__version__
import tensorflow
tensorflow.__version__
import torch
torch.__version__
import cv2
cv2.__version__

If you want to install OpenCV 3.3, follow along

Step 7 : Install OpenCV 3.3

First we will install the dependencies:

sudo apt-get remove x264 libx264-dev
sudo apt-get install -y checkinstall yasm
sudo apt-get install -y libjpeg8-dev libjasper-dev libpng12-dev

# If you are using Ubuntu 16.04
sudo apt-get install -y libtiff5-dev
sudo apt-get install -y libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev

sudo apt-get install -y libxine2-dev libv4l-dev
sudo apt-get install -y libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev
sudo apt-get install -y libqt4-dev libgtk2.0-dev libtbb-dev
sudo apt-get install -y libfaac-dev libmp3lame-dev libtheora-dev
sudo apt-get install -y libvorbis-dev libxvidcore-dev
sudo apt-get install -y libopencore-amrnb-dev libopencore-amrwb-dev
sudo apt-get install -y x264 v4l-utils

Download OpenCV and OpenCV-contrib

git clone https://github.com/opencv/opencv.git
cd opencv
git checkout 3.3.0
cd ..

git clone https://github.com/opencv/opencv_contrib.git
cd opencv_contrib
git checkout 3.3.0
cd ..

Configure and generate the MakeFile

cd opencv
mkdir build
cd build
#Remove the line WITH_CUDA=ON if you dont have CUDA in your system

cmake -D CMAKE_BUILD_TYPE=RELEASE \
      -D CMAKE_INSTALL_PREFIX=/usr/local \
      -D INSTALL_C_EXAMPLES=ON \
      -D INSTALL_PYTHON_EXAMPLES=ON \
      -D WITH_TBB=ON \
      -D WITH_V4L=ON \
      -D WITH_QT=ON \
      -D WITH_OPENGL=ON \
      -D WITH_CUDA=ON \
      -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \
      -D BUILD_EXAMPLES=ON ..

Compile and Install

NOTE : The make operation takes quite a long time, almost an hour using 12 cores on an i7 processor. Also, it might get stuck for long at some places, but don’t worry unless it is stuck for more than an hour.

make -j4
sudo make install
sudo sh -c 'echo "/usr/local/lib" >> /etc/ld.so.conf.d/opencv.conf'
sudo ldconfig

Link OpenCV to your virtual environments

# Check the opencv .so was created
find /usr/local/lib/ -type f -name "cv2*.so"


It should give an output similar to the one shown below
/usr/local/lib/python3.5/dist-packages/cv2.cpython-35m-x86_64-linux-gnu.so

These are the locations where OpenCV’s Python runtime library file ( cv2.so ) is located. We need to create symlinks to these files from our virtual environment in order to use OpenCV inside them without reinstalling OpenCV.

Note the exact path of the cv2.so file. In my system, it is located in dist-packages. But in most systems, it is located in site-packages directory.

The creation of symlinks is done as follows:

cd ~/.virtualenvs/virtual-py3/lib/python3.5/site-packages
ln -s /usr/local/lib/python3.5/dist-packages/cv2.cpython-35m-x86_64-linux-gnu.so cv2.so

Check OpenCV Installation

workon virtual-py3
python
import cv2
cv2.__version__

As an output you should get a corresponding OpenCV version.

What next?

Check out our next posts on Keras Basics and Feedforward Neural Networks Basics. More posts on Deep Learning to follow. Stay Tuned!

References

Here is a list of other resources you may find useful:



Read Next

VideoRAG: Redefining Long-Context Video Comprehension

VideoRAG: Redefining Long-Context Video Comprehension

Discover VideoRAG, a framework that fuses graph-based reasoning and multi-modal retrieval to enhance LLMs' ability to understand multi-hour videos efficiently.

AI Agent in Action: Automating Desktop Tasks with VLMs

AI Agent in Action: Automating Desktop Tasks with VLMs

Learn how to build AI agent from scratch using Moondream3 and Gemini. It is a generic task based agent free from…

The Ultimate Guide To VLM Evaluation Metrics, Datasets, And Benchmarks

The Ultimate Guide To VLM Evaluation Metrics, Datasets, And Benchmarks

Get a comprehensive overview of VLM Evaluation Metrics, Benchmarks and various datasets for tasks like VQA, OCR and Image Captioning.

Subscribe to our Newsletter

Subscribe to our email newsletter to get the latest posts delivered right to your email.

Subscribe to receive the download link, receive updates, and be notified of bug fixes

Which email should I send you the download link?

🎃 Halloween Sale: Exclusive Offer – 30% Off on All Courses.
D
H
M
S
Expired
 

Get Started with OpenCV

Subscribe To Receive

We hate SPAM and promise to keep your email address safe.​