How to setup CUDA in Ubuntu for Darknet YOLO

Abhishek Chand
3 min readNov 3, 2020

--

This tutorial is tested on multiple 20.04.1 PCs with GTX 1080ti & GTX 1050ti. All the commands in this tutorial will be done inside the “terminal”.

Content:

  1. Ubuntu Setup
  2. CUDA Download and Setup
  3. Install cuDNN

1 — Ubuntu Setup

First we need to install some packages in our Ubuntu system. Run the following commands.

sudo apt-get update
sudo apt-get upgrade -y
sudo apt-get install -y libopencv-dev
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
sudo apt-get install -y libv4l-dev libxvidcore-dev libx264-dev
sudo apt-get install -y libgtk-3-dev libopenblas-dev
sudo apt-get install -y 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

You can also copy paste the above commands into a sh file like installLibs.sh Then just execute that file using

sudo sh installLibs.sh

2 — CUDA Download and Setup

First make sure you disable the nouveau driver if you have it installed.
To check if it is loaded run this command.

lsmod | grep nouveau

If there is no result, then you are good to go.
If you get a result, then you need to disable nouveau.

Disable nouveau

  1. Create a file at /etc/modprobe.d/blacklist-nouveau.conf using the nano command like this:
nano /etc/modprobe.d/blacklist-nouveau.conf

2. Add the following contents in the file:

blacklist nouveau options nouveau modeset=0

Save and exit the file by typing

crtl + o
[enter]
crtl + x

3. Regenerate the kernel initramfs:

sudo update-initramfs -u

Install CUDA Toolkit

Once this is done, you can download the CUDA Toolkit runfile from here
Assuming I want to download & install CUDA Toolkit 11.0 Update 1.
I will run the 2 commands mentioned in CUDA Toolkit website.

wget https://developer.download.nvidia.com/compute/cuda/11.0.3/local_installers/cuda_11.0.3_450.51.06_linux.run
sudo sh cuda_11.0.3_450.51.06_linux.run

Follow the installer instructions and install the CUDA Toolkit. (You can skip the NVIDIA Driver if you have it installed already)

Add CUDA to Environment Path

The file is a shell script for Bash (or the Linux Shell) to run.

nano ~/.bashrc

At the bottom of the file, add the following lines (make sure you change the CUDA version according to your installation):

# NVIDIA CUDA Toolkit
export PATH=/usr/local/cuda-11.0/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda-11.0/lib64
export CPATH=/usr/local/cuda-11.0/targets/x86_64-linux/include:$CPATH

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

3 — Install cuDNN

This is the NVIDIA CUDA Deep Neural Network library. It is used for installing the gpu-accelerated libraries. You will need a developer NVIDIA account to download cuDNN.

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

After downloading the appropriate cuDNN for your CUDA Toolkit version, open the terminal in which the downloaded file is located and run the following commands.

tar -zxf cudnn-11.0-linux-x64-v8.0.4.30.tgz
cd cuda
sudo cp -P lib64/* /usr/local/cuda/lib64/
sudo cp -P include/* /usr/local/cuda/include/
cd ~

And you are done. You have successfully installed the CUDA Toolkit & cuDNN library for it in your system.

--

--