OpenCV 4 + CUDA on Jetson Nano

Building OpenCV 4 with CUDA support on the NVIDIA Jetson Nano Developer Kit can be a bit of a chore. Here’s some help. Looky here:


In an earlier article, 5 Things about OpenCV on Jetson, we discuss some of the reasons which you may want to build OpenCV from source. The reasons include:

  • You need a specific version of OpenCV (the default on the Jetson is 3.3.1)
  • You would like to use OpenCV version 4
  • You would like to have CUDA acceleration
  • You would like to have a different configuration than the default version, such as support for Qt, OpenGL, and so on.

Note: While we mention why you may want to switch to CUDA enabled algorithms, reader Patrick pointed out that a real world example of when you want CUDA acceleration is when using the OpenCV DNN module. To do so, you may need to set the CMake flag OPENCV_DNN_CUDA to YES.

In the video, we use:

  • Jetson Nano
  • A Samsung T5 USB drive
  • A RPi V2 camera
  • A 5V, 4A power supply

You can find these items on the JetsonHacks Amazon store. Buying through the link does not cost you anything additional, and sends a small commission to the channel. Thank you for your support!


There are already some good resources for building OpenCV on the Jetson. First, there is the “official” NVIDIA script in this Github repository: As of this writing, the OpenCV script is in the scripts directory, the name of the script is

NVIDIA updates this script frequently to support newer versions of OpenCV and L4T. A good choice if you want something to work and do not want to spend the time specifying your configuration. In the video, we go over the script.

Michael de Gans is a frequent Jetson forum contributor. He knows what’s up! Michael has a Github repository with the name nano_build_opencv which contains a script to build OpenCV. The script has much more structure than the NVIDIA one, you will benefit reading through/using it.

Sergio Canu’s excellent article on building OpenCV 4.1 discusses how to increase the size of the system swap memory when building OpenCV. Here’s a JetsonHacks article on how to set the swap memory size. This should cut your build time dramatically when building OpenCV. Setting the swap memory size to 8GB cut the time as shown in the video of 2.5 hours to 1.5 hours.

Another great resource is JK Jung’s blog. The blog covers many aspects of using Jetsons in machine learning, certainly one of the best Jetson resources out there. There are articles on different Machine Learning frameworks, how to speed up your projects with TensorRT, ML optimizing and more.

By going through JK Jung’s blog post, you will begin to understand some of the decisions that you may need to make for any given environment. OpenCV is a very rich environment. In computer speak that usually means that it is so flexible that it takes a lot of knowledge to configure it for best performance for any given application.

Which leads us to the gist of this post. The JetsonHacks take on build OpenCV. In the broad sense, this is version updates previous work we have done on the other members of Jetson family.

Building OpenCV 4

The version of OpenCV 4 that we are building has the following features:

  • CUDA support
  • Gstreamer support
  • Video for Linux support (V4L2)
  • Qt support
  • OpenCV version 4.1.1
  • Python 2 and Python 3 support
  • Build an OpenCV package with installer
  • Build for Jetson Nano

In the video, we are using a Jetson Nano running L4T 32.2.1/JetPack 4.2.2. The Nano is running with the rootfs on a USB drive. This speeds up the build time considerably. As in Sergio Canu’s article, you can increase the size of the swap file to reduce memory thrashing. Here’s a JetsonHacks article on how to set the swap memory size. This should cut your build time dramatically when building OpenCV. Setting the swap memory size to 8GB cut the time as shown in the video of 2.5 hours down to 1.5 hours.

Note: The swap file in the video is only 2GB, and results in a lot of memory thrashing when building the Python wrappers.

Let’s build this puppy

As we discuss in the video, you will want to set the CMAKE flags to reflect the OpenCV configuration you want to build. We have a quick walk through in the video, but it is certainly worth exploring on your own. To build from a Terminal:

$ git clone

$ cd buildOpenCV

$ ./ |& tee openCV_build.log

The last command will build OpenCV, and send the results of the build to the file openCV_build.log. This is useful when you want to review your build configuration, or help hunt down issues that may occur during the build.

The resulting Package of OpenCV will end up in the opencv directory: ~/opencv/build/_CPACK_Packages/Linux/STGZ with a name similar to OpenCV-4.1.1-<commit>, where commit is the name of the Github version.

We can run a couple of demos to make sure things are working. Please see the Examples folder and follow the README instructions (as shown in the video). The demos expect that you have a RPi V2 camera, you may have to change some code for a USB camera.

Note about pkg-config

By default, OpenCV does not build a .pc file for use by pkg-config. In order to build the file, the correct Cmake option is:


When installation is complete, you will need to relocate the .pc file (assuming the script default install path here) /usr/local/lib/pkgconfig/ and edit the file paths in the .pc file appropriately. Make sure you run:

$ sudo ldconfig -v

afterwards so that pkg-config knows where to look.


That’s a lot of reading for three simple lines of code! However, when undertaking a project like this it make sense to understand why you are doing it, what your options are, and what benefit you expect to receive after completion.


In the video:

  • Jetson Nano
  • Build OpenCV 4.1.1
  • L4T 32.2.1/JetPack 4.2.2


  1. Good evening, I was wondering if you had any thoughts on ways to reduce the 300-400ms latency that you mention in the video? On the Nvidia Jetson forums I’ve been told there is a 2-3 frame delay in the CSI pipeline but you are clearly seeing much greater.

    (Thank you for all your efforts to make Jetson Nano easier to use)

    • I haven’t really looked into it, you can probably get better advice in the forums. I would advise you to perform your own tests, there are lots of parameters to tweak that I may not have set for optimal performance.

  2. hi Jim, opencv compiled okay, but canny code didnt detect a camera? Did you use the csi cam or a usb cam? if you used the csi cam, is there a driver that needs to be installed or any setting changed?

  3. Anyone wanting to use DNNs (yolo etc.):
    CUDA_DNN requires latest master branch (script uses a different branch). Ran into a build fail a few hours in due to missing libwebp-dev module. Install the module before (or add to script):
    $ sudo apt-get install libwebp-dev

  4. Phil,
    sudo apt-get install libwebp-dev
    above cmd Is it mandatory to run before building the opencv?

    Is it ok if i run the “sudo apt-get install libwebp-dev” cmd after build?

    please advise

    I already built the opencv4.1.1, just now I see your comments.

  5. I have a question regarding opencv supporting CUDA. The latest Jetpack 4.3 (and also the image I have on my jetson nano) has OpenCV 4.1.1 already installed. I did the print cv2.getBuildInformation() that you indicated in your other post but this time it did not indicate that CUDA was not supported but it did not indicate that CUDA was supported either.

    So my question is, is CUDA supported by default in OpenCV 4.1.1 or should I build it from source anyway?

    • It depends on your needs. The default OpenCV 4.1.1 does not have CUDA support. However, make sure that the OpenCV algorithms that you are going to use have CUDA support before recompiling OpenCV with CUDA support. Thanks for reading!

  6. Thanks for the great tutorial! Can I use this same script to install opencv4.1.2 by only changing the OPENCV VERSION in the file?

    • mmm, eventhough cuda is activated, cv::gpu::getCudaEnabledDeviceCount() returns 0.
      and your python example fails because cv2.namedWindow(windowName, cv2.WINDOW_NORMAL) is not implemented

  7. mmm, eventhough cuda is activated, cv::gpu::getCudaEnabledDeviceCount() returns 0.
    and your python example fails because cv2.namedWindow(windowName, cv2.WINDOW_NORMAL) is not implemented

  8. Great write up. Thank you. Iā€™m trying to build 4.1.1 but get the following error
    fatal error: sys/videoio.h: No such file or directory
    Not sure what to do about it, see other have the same issue but I can not locate a definitive solution. Do you have an idea?

  9. 64G micro SD, 4G swap, 1 core, OpenCV 4.3.0.
    Just over 4 hours to build.
    Appears 4.3.0 does not support cuDNN.
    Also to build ‘gstreamer_veiw’, had to change
    –cfags opencv to –cflags opencv4
    and –libs opencv to –libs opencv4
    cv2 is now very cuda’y in Python šŸ™‚

  10. can anyone please explain the “Note about pkg-config” steps I am new to this field,.. installation is complete but I am getting a fatal error(no file named OpenCV is found).

  11. Thanks for this. It works great on my jetson Nano. I just received my Xavier NX kit, and the opencv 4.1 installed does not include the contrib libs. Would it work for me to use this same procedure on the Xavier? I assume i would first uninstall the included version.

  12. thanks for the advice,ill try it out. do you have any other advice for uninstalling the version i currently have under the jetpack preview, without causing any problems, both on the uninstall and also on its impact on the new install?

  13. Hi,
    I did the installation, and seemed to work. But when i test it, i can import cv2, but i cant use cv2.__version__ or any other attributes of cv2. Ant addvice/ tips

Leave a Reply

Your email address will not be published.