Install OpenCV on Jetson Nano
Last updated: August 10, 2023
Why would you install OpenCV on the Jetson Nano, when your operating system has already a version pre-installed? The main reason is that the shipped version has no CUDA support. And after all, wasn't the CUDA accelerator the main reason you bought the Jetson Nano in the first place?
Regularly, we get the question if we have an SD image of a Jetson Nano with pre-installed OpenCV, TensorFlow and PyTorch.
We are happy to comply with this request. Please, find a complete Jetson Nano image on our GitHub page. Download the zip file from our Gdrive site, unzip and flash the image on a 32 GB SD-card, and enjoy!
On June 28, 2023, OpenCV released version 4.8.0. As usual, most of the changes and improvements can be found in the DNN module, apart from the bug fixes. One nice improvement is the TFLite models support, including int8 quantized models. More info can be found in the change log.
On December 17, 2022, OpenCV released version 4.7.0. As usual, most of the changes and improvements can be found in the DNN module, apart from the bug fixes. More info in the change log.
On June 7, 2022, OpenCV released version 4.6.0. As usual, most of the changes and improvements can be found in the DNN module, apart from the bug fixes. Also noteworthy is the support for FFmpeg 5.0. More info in the change log.
We have to live with the extra line -D PYTHON3_PACKAGES_PATH=/usr/lib/python3/dist-packages.
As of Python 3.12, the installation in the dist-packages folder is deprecated in favour of the site-package folder.
It may be advisable to install the lightdm desktop instead of the gdm3 when prompted during the initial installation, saving about 500 MByte of precious memory space. It can make all the difference, especially with memory-hungry deep learning models. Although some of you probably prefer the Ubuntu look and feel to the somewhat archaic lxdm experience.
Before installing OpenCV on your Jetson Nano, consider overclocking. When the CUDA accelerator is not used, which is in most daily applications, the Jetson Nano has a quad ARM Cortex-A57 core running at 1.4 GHz. Compared to the quad Cortex-A72 at 1.5 GHz of the Raspberry Pi 4, there isn't that great a difference. Especially with an overclocked RPi. In that case, it will beat the Jetson in most tasks. Except, of course, when the CUDA kicks in. On this page the overclocking of the Jetson Nano is covered.
Basically, there are three ways to install OpenCV on a machine. Let's take a closer look.
First, we have the Python package manager pip.
To install OpenCV, you give the command $ pip3 install opencv-python or $ pip3 install opencv-contrib-python.
The latter installs all the contributions, as well as the core opencv-python library. Do not use them! The distributions come without CUDA bindings, so you gain nothing. Even worse, the installation is likely to interfere with the already existing OpenCV. And what's more, it doesn't support C++ programming.
sudo apt-get install.
The second common method is $ sudo apt-get install libopencv-dev. Unfortunately, he returns with the comment that you already have the latest version (4.1.1 !). And still you are missing CUDA support. So there is no need to use it.
Compiling from source.
In fact, the only method to install the latest OpenCV on the Jetson Nano with CUDA and cuDNN support is by building it from the source.
Enlarge memory swap.
Building the complete OpenCV package requires more than 4 Gbytes of RAM and the 2 Gbytes of swap space delivered by zram, usually found on your Jetson Nano. Temporarily, we install dphys-swapfile to get the additional room from your SD card. After the compilation, we remove the mechanism, eliminating swapping to the SD card. Please follow the commands. Note also the installation of nano, a tiny text editor.
You need to increase the dphys swap beyond the regular 2048 limit. First, change the maximum boundary in /sbin/dphys-swapfile to 4096. Next, set the /etc/dphys-swapfile. The slideshow will guide you. Without enough swap memory, the compilation will crash around 100% without notification, especially when using all four cores simultaneously.
Obviously, there must be enough free space on the SD card. With 2 GBytes left, you never get the required 4 GBytes swap space.
We do not recommend increasing the zram swap limits. You can't just keep compressing system memory, hoping to get some extra space. There are limits. It is better that you temporarily use the SD memory. When OpenCV is installed, you can remove the dphys-swapfile again.
# a fresh start, so check for updates
$ sudo apt-get update
$ sudo apt-get upgrade
# install nano
$ sudo apt-get install nano
# install dphys-swapfile
$ sudo apt-get install dphys-swapfile
# enlarge the boundary (4.5.2 and higher)
$ sudo nano /sbin/dphys-swapfile
# give the required memory size
$ sudo nano /etc/dphys-swapfile
# reboot afterwards
$ sudo reboot
Please follow the steps in the side show below.
Installing OpenCV on your Jetson Nano is not that complicated. With its 68 command lines, it is more of an administrative task. That's why we created an installation script that executes all commands in this guide at once. Use it if you want, it shouldn't cause any problems. The whole installation will take two hours to complete. It starts with the installation of the dependencies and ends with the $sudo ldconfig.
If you want to beautify your OpenCV with the Qt5 GUI, follow the instructions on the GitHub page, or in the section below.
Not using the script? Here's the guide.
The OpenCV software uses other third-party software libraries. Install these first. Some come with the Jetson Tegra operating system, and others you may already have installed. Better to be safe than sorry, so here's the complete list. Only the latest packages are installed according to the procedure.
The installation will take up an additional 800 MByte on your disk if not already present.
# reveal the CUDA location
$ sudo sh -c "echo '/usr/local/cuda/lib64' >> /etc/ld.so.conf.d/nvidia-tegra.conf"
$ sudo ldconfig
# third-party libraries
$ sudo apt-get install build-essential cmake git unzip pkg-config zlib1g-dev
$ sudo apt-get install libjpeg-dev libjpeg8-dev libjpeg-turbo8-dev
$ sudo apt-get install libpng-dev libtiff-dev libglew-dev$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev $ sudo apt-get install libgtk2.0-dev libgtk-3-dev libcanberra-gtk*$ sudo apt-get install python-dev python-numpy python-pip$ sudo apt-get install python3-dev python3-numpy python3-pip$ sudo apt-get install libxvidcore-dev libx264-dev libgtk-3-dev$ sudo apt-get install libtbb2 libtbb-dev libdc1394-22-dev libxine2-dev$ sudo apt-get install gstreamer1.0-tools libgstreamer-plugins-base1.0-dev
$ sudo apt-get install libgstreamer-plugins-good1.0-dev
$ sudo apt-get install libv4l-dev v4l-utils v4l2ucp qv4l2
$ sudo apt-get install libtesseract-dev libxine2-dev libpostproc-dev$ sudo apt-get install libavresample-dev libvorbis-dev $ sudo apt-get install libfaac-dev libmp3lame-dev libtheora-dev $ sudo apt-get install libopencore-amrnb-dev libopencore-amrwb-dev$ sudo apt-get install libopenblas-dev libatlas-base-dev libblas-dev$ sudo apt-get install liblapack-dev liblapacke-dev libeigen3-dev gfortran$ sudo apt-get install libhdf5-dev libprotobuf-dev protobuf-compiler
$ sudo apt-get install libgoogle-glog-dev libgflags-dev
Qt is an open-source toolkit for the development of cross-platform graphical user interfaces. It also works on the Linux Tegra operating system. The software can be used to beautify OpenCV windows and other user interfaces, such as sliders and checkboxes. It is absolutely not mandatory for the working of OpenCV, only to embellish the look. Must be said, that using Qt5 will slow down your OpenCV by a few percent. If you go for the fastest possible apps, don't use it.
If you want the Qt5 support enabled in OpenCV, you have to download the library as shown in the command below. The next step is to set the -D WITH_QT flag during the build.
# only install if you want Qt5
# to beautify your OpenCV GUI
$ sudo apt-get install qt5-default
When all third-party software is installed, OpenCV itself can be downloaded. There are two packages needed; the basic version and the additional contributions. After downloading, you can unzip the files. Please be aware of line wrapping in the text boxes. The two commands are starting with wget and ending with zip.
Before we begin with the actual build of the library, there is one small step to go. You have to make a directory where all the build files can be located.
$ cd ~/opencv
$ mkdir build
$ cd build
Now it is time for an important step. Here you tell CMake what, where and how to make OpenCV on your Jetson Nano. There are many flags involved. The most you will recognize. You will probably notice the -D WITH_QT=OFF line. Here Qt5 support is disabled. Set -D WITH_QT=ON if you want to use the Qt5 software for the GUI. We save space by excluding any (Python) examples or tests.
There are only bare spaces before the -D flags, not tabs. By the way, the two last dots are no typos. It tells CMake where it can find its CMakeLists.txt (the large recipe file); one directory up.
Also, note the /usr directory where all header files and OpenCV libraries will reside. We have a slight preference for the /usr/local folder. For now, we'll keep the Tegra installation directory to let pre-existing software run smoothly with the reinstalled OpenCV. Keep it in mind when you port some of our Raspberry Pi software on GitHub to your Jetson Nano.