[PYTHON] Aim for easy installation of OpenCv and Cuda using JetPack (NVIDIA SDK Manager) from Nvidia

Aim for easy installation of OpenCv and Cuda using JetPack (NVIDIA SDK Manager) from Nvidia

スクリーンショット 2020-06-19 21.43.27.png

Prepare a machine with Nvidia GPU

Nvidia GPU needs to have a PC with it

$ apt-cache search "^nvidia-[0-9]{3}$"

Find out which Nvidia driver can be installed on that PC with apt-cache. 20191106123629.png 20191106125506.png

Cuda 10.0 is installed for NVIDIA SDK Manager. More than 410 NVIDIA drivers will be installed. Use a Windows machine that supports Cuda 10.0 or use Jatson. If not supported, give up * NVIDIA SDK Manager *, lower the version of Cuda and install manually

https://docs.nvidia.com/cuda/cuda-toolkit-release-notes/index.html Cuda 10.0 does not work because the Gpu driver on older PCs is 410 or less

~~ Use virtual environment software ~~

~~ Parallels Desktop, VMWare, etc. Here we use parallels-desktop ~~ Postscript: Geforce driver does not recognize in virtual environment For Mac, whether to boot Linux with dial boot or use Windows Whether to transfer to an external Jetson Nano and install (#1)

install ubuntu

Japanese version Ubuntu Distribution site スクリーンショット 2020-06-19 19.53.39.png

Release the cursor with Ctrl + alt ~~ The Parallels Tool CD will be mounted, so you should install it together. ~~

sudo apt-get update
sudo apt-get upgrade
Relationship between NVIDIA graphics driver and Cuda version

The Cuda version of the NVIDIA SDK Manager is 10.0. It doesn't work with old PC Gpu and old Nivida drivers. Either give up the automatic installation of the NVIDIA SDK Manager and switch to a manual installation by lowering the version of Cuda, or renewing a new PC

Check if GPU is Nvidia
sudo lshw -C display 

In a virtual environment, it will be called Virtual Video Adapter
Since the Nivide driver cannot be installed in the virtual environment, the host PC(Linux or Windows)Need to install the Nivide driver on

Find out if Ubuntu recognizes the GPU

$ sudo ubuntu-drivers devices

Nvidia driver installation method 1


Find out if Ubuntu recognizes the GPU

$ sudo ubuntu-drivers devices

When the GPU model number is displayed correctly, execute the following command to install the driver.

$ sudo ubuntu-drivers autoinstall
#List NVIDIA drivers installed on your machine
$ dpkg -l | grep nvidia

# apt-List of NVIDIA drivers that can be installed with get
$ apt-cache search "^nvidia-[0-9]{3}$"

Check if the driver version and GPU details are displayed with the nvidia-smi command

$ nvidia-smi

If it is displayed correctly, proceed to install Cuda.

I got an error when running nvidia-smi
$ nvidia-smi 
NVIDIA-SMI has failed because it couldn't communicate with the NVIDIA driver. Make sure that the latest NVIDIA driver is installed and running.
nvidia-smi could not confirm the driver of nvidia. Make sure you have the latest drivers.

In this case, disabling Nouveau will solve the problem. First, create /etc/modprobe.d/blacklist-nouveau.conf and edit it as follows.

$ sudo vim /etc/modprobe.d/blacklist-nouveau.conf

Fill in the following contents

blacklist nouveau
options nouveau modeset=0

Next, execute the following command to reload the kernel module.

$ sudo update-initramfs -u

$ sudo reboot

$ nvidia-smi


Restart your PC and if you can run nvidia-smi, proceed to install CUDA

Nvidia driver installation method 2

Download Nvidia Driver スクリーンショット 2020-06-30 14.18.07.png

Select the driver of the installed GPU, download the Run format file and install it

$ chmod +x GPU driver name.run
$sudo sh GPU driver name.run

Check if the driver version and GPU details are displayed with the nvidia-smi command

$ nvidia-smi
Precautions when re-installing

Installing the NVIDIA SDK Manager first would install the latest NVIDIA graphics driver, and then when I tried to install the old graphics driver that was on my machine, I sometimes got an error and couldn't install it. I tried to reinstall using the dependency resolution command aptitude, but it got stuck on the desktop screen.

Note that the machine hung sudo aptitude install libnvidia-encode-418 Reference

It may be better to uninstall and reinstall obediently

sudo apt-get --purge remove nvidia-*
sudo apt-get --purge remove cuda-*

Install Cuda and OpenCv

Install NVIDIA SDK Manager You can not install unless you are connected to the net スクリーンショット 2020-06-19 19.51.55.png

Download, unzip and install the JetPack archive. ** You need to create an Nvidia account ** スクリーンショット 2020-06-19 20.01.18.png

Select HostMachien. Select JetPack 4.3.

Reference article スクリーンショット 2020-06-19 20.05.24.png

** Be sure to check Install after download at the bottom ** ** Be sure to check it because an error may occur if you install it during download ** You need about 30G capacity

Installation complete

スクリーンショット 2020-06-20 19.09.20.png

I was able to install OpenCv and Cuda automatically.

JetPack 4.3 in jetson components:

L4T R32.3.1 (K4.9)
Ubuntu 18.04 LTS aarch64
CUDA 10.0
cuDNN 7.6.3
TensorRT 6.0.1
VisionWorks 1.6
OpenCV 4.1 (4.1.1)
Nsight Systems 2019.6
Nsight Graphics 2019.5
Nsight Compute 2019.3
SDK Manager 1.0.0

cuda location


How to check opencv

$ sudo apt-get install python3-pip

$ sudo pip3 install numpy
$ python3
>>> import cv2
>>> print(cv2)
>>> cv2.__version__

How to install official cuda

Explanation of Official cuda

Add path settings

$ sudo apt install vim
#update vim
sudo apt install vim

#Put the version number in x and y
$ sudo vim ~/.bashrc

## CUDA and cuDNN paths
export PATH=/usr/local/cuda-x.y/bin:${PATH}
export LD_LIBRARY_PATH=/usr/local/cuda-x.y/lib64:${LD_LIBRARY_PATH}
#Environment variable$LD_LIBRARY_Add NVIDIA driver path to PATH
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib/nvidia-number

$ source ~/.bashrc

$ nvcc -V

#Check the location of the CUDA Toolkit
$ which nvcc


Reference Reference

Installation and operation check of RunTime of cuDNN

cudnn-archive スクリーンショット 2020-06-30 13.27.00.png

cuDNN Runtime Library for Ubuntuh   cuDNN Developer Library for Ubuntuh cuDNN Code Samples and User Guide for Ubuntu Download and double click to install


Operation check of cuDNN

Check if the path is in cuda-10.0 / bin If it passes, you can run cuda-install-samples-10.0.sh Run it in your home directory. Enter the folder name in the argument. Running cuda-install-samples-10.0.sh will create a sample folder in your home directory. I have a Make file, so I will make it A sample file will be generated in the bin folder, so try running it. Execute deviceQuery to check if cuda device is valid

$ cd /usr/local/cuda-10.0/bin

# /usr/local/cuda-10.0/bin/cuda-install-samples-10.0.sh

#Copy the directory with the sample code to your home directory
$ cuda-install-samples-10.0.sh folder name

$cd folder name/NVIDIA_CUDA-10.0_Samples
$ make
$ cd NVIDIA_CUDA-10.0_Samples/bin/x86_64/linux/release

$ ./deviceQuery
$ ./bandwidthTest

$ ./volumeRender 

[Reference](https://medium.com/@vitali.usau/install-cuda-10-0-cudnn-7-3-and-build-tensorflow-gpu-from-source-on-ubuntu-18-04 -3daf720b83fe)

Confirmation of cuDNN installation

List the installed packages with the dpkg -l command.

#List cuDNN packages installed on your machine
$ dpkg -l | grep cudnn

cuDNN storage location

#Check the directory where the deb package is stored(-L option)
$ dpkg -L libcudnn7

Install Yolo

$ sudo apt install git
$ git clone https://github.com/AlexeyAB/darknet.git
$ cd darknet
darknet$ vim Makefile

darknet$ make

-lcuda not found error appears

Find out where libcuda.so is

# libcuda.Find out where so is
$ locate libcuda.so

# libcuda.so place hits

Once you find the libcuda.so file, create it in the x86_64-linux-gnu folder. Find good articles by creating a symbolic link for libcuda.so.1 in the x86_64-linux folder

sudo ln -s /usr/local/cuda-10.0/targets/x86_64-linux/lib/stubs/libcuda.so /usr/lib/x86_64-linux-gnu/libcuda.so.1

# libcuda.so.1 and libcuda.so Both symbolic links are required
sudo ln -s /usr/local/cuda-10.0/targets/x86_64-linux/lib/stubs/libcuda.so /usr/lib/x86_64-linux-gnu/libcuda.so


#Make again
$ make clean
$ make

It says that the cudnn.h file does not exist. Copy the cudnn.h file

[cudnn installation official](https://translate.google.co.jp/translate?hl=ja&sl=auto&tl=ja&u=https%3A%2F%2Fdocs.nvidia.com%2Fdeeplearning%2Fsdk%2Fcudnn-install%2Findex. html% 23installlinux-tar)

cudnn-archive スクリーンショット 2020-06-26 17.41.06.png

Download cuDNN Library for Linux

#Unzip the cuDNN package. Can be decompressed even from Finder

$ tar -xzvf cudnn-x.x-linux-x64-v8.x.x.x.tgz

$ sudo cp cuda/include/cudnn*.h /usr/local/cuda/include
$ sudo cp cuda/lib/libcudnn* /usr/local/cuda/lib64
$ sudo chmod a+r /usr/local/cuda/include/cudnn*.h /usr/local/cuda/lib64/libcudnn*

cudnn.h not found when making no error

cudnn related

libcudnn.so.7.0.Libcudnn other than 1.so delete the file
/usr/local/cuda/lib64 $ sudo rm -rf libcudnn.so libcudnn.so.7
sudo ln -s libcudnn.so.7.0.1 libcudnn.so.7
sudo ln -s libcudnn.so.7 libcudnn.so
make clean 

I can't write darknet / obj

change darknet / obj permissions

$sudo chmod 777 home path/darknet/obj home path/darknet/backup

Make again

$ make clean
$ make

Download weights

Not included in Alexey AB version, so download from pjreddie

$ wget https://pjreddie.com/media/files/yolov3.weights

Try running Yolo

./darknet detect cfg/yolov3.cfg yolov3.weights data/dog.jpg

Yolo runtime error Not compiled with OpenCV, saving to predictions.png instead

DarkNet Makefile openCv is not 1

$ vim Makefile

CUDA driver version is insufficient for CUDA runtime version CUDA driver version is insufficient for CUDA runtime version : No Such File or Directory

on hold [Reference](https://support.dl.sony.com/faq-ja/faq%EF%BC%9A%E3%82%BB%E3%83%83%E3%83%88%E3%82% A2% E3% 83% 83% E3% 83% 97 /) Does it come out if the NVIDIA graphics driver is not installed? investigating

Also update cuda
sudo apt-get install cuda

No CUDA-capable device is detected

CUDA compatible device not detected Because it does not recognize Nvidia's GPU in the virtual environment. Stop the virtual environment and re-install the OS on the host PC

PATH check for CUDA and NVIDIA drivers (/ usr / lib / nvidia-***)

Does the output contain "/ usr / local / cuda-10.0 / lib64" and "/ usr / lib / nvidia-number"?


PATH check for CUDA and nvcc commands

Is the output "/ usr / local / cuda-10.0 / bin / nvcc"?

$ which nvcc             

PATH check for nvidia-smi command

Is the output "/ usr / bin / nvidia-smi"?

 $ which nvidia-smi

How to uninstall

#Put the version in x
sudo apt remove cuda-x-y
sudo apt autoremove
sudo apt remove libcudnnx libcudnnx-dev libcudnnx-doc
rm -rfv ~/NVIDIA_CUDA-x.y_Samples/ #Erase the sample code.
# ~/.Delete the PATH setting added to bashrc with a text editor.
#Log out to reflect changes in PATH settings


Prepare a machine with Nvidia GPU Nvidia graphics driver cannot be installed in virtual environment The Nividia Sdk Maneger Cuda is 10.0, so it won't work with older Nividia drivers. Use Nvidia Driver that can run Cuda 10.0 Unzip the cuDNN package and copy the specified file (cudnn * .h) etc. to the specified location Find the location of libcuda.so with locate libcuda.so. Put the symbolic link in / usr / lib / x86_64-linux-gnu After editing .bashrc with vim, it will not be applied unless you save it with source ~ / .bashrc


When using Jetson nano, it is better to use Nividia Sdk Maneger. Compatible with cuda10 and Nividia drivers When installing on a PC, check the GPU, and if the Nvidia Driver supports 410 or higher, try using the Nividia SDK. If it does not support less than 410, give up the SDK and do it manually.

Reference site

** Inspiration-blog ** It's written in great detail

Coda linux official installation information

Nvidia SDK Manager on Docker for a comfortable Jetson life

JetPack setup procedure for Jetson TX2


(#1) CUDA on WSL2 If you use WSL2, it seems that you can build Linux on Windows and use the GPU driver of Windows.

Recommended Posts

Aim for easy installation of OpenCv and Cuda using JetPack (NVIDIA SDK Manager) from Nvidia
Easy installation of OpenCV on RaspberryPi 3+
Installation and easy usage of pytest
Easy introduction of python3 series and OpenCV3
Easy installation and launch of Jupyter Notebook using Docker (also supports nbextensions and Scala)