How to install OpenCV on Jetson Nano?

Image credit: OpenCV logo

Table of Contents

Generate swap file

To prevent errors due to insufficient memory during compilation, create a swap file firstly, and store it to somewhere you know. After OS restarting, the swap space will disappear, or you can manually delete this file after the completion of the following compilation and other work.

  $ fallocate -l 4G swapfile
  $ chmod 600 swapfile
  $ mkswap swapfile
  $ sudo swapon swapfile
  $ swapon -s

Generate installation script

The original link of the script to install the OpenCV 4.0.0 version

The above is the version used to install OpenCV 4.0.0, here we want to install the newer version 4.1.0, so change the version number part of the above code.

  # Copyright (c) 2018, NVIDIA CORPORATION.  All rights reserved.
  # NVIDIA Corporation and its licensors retain all intellectual property
  # and proprietary rights in and to this software, related documentation
  # and any modifications thereto.  Any use, reproduction, disclosure or
  # distribution of this software and related documentation without an express
  # license agreement from NVIDIA Corporation is strictly prohibited.

  if [ "$#" -ne 1 ]; then
      echo "Usage: $0 <Install Folder>"

  echo "** Install requirement"
  sudo apt-get update
  sudo apt-get install -y build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
  sudo apt-get install -y libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev
  sudo apt-get install -y python2.7-dev python3.6-dev python-dev python-numpy python3-numpy
  sudo apt-get install -y libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
  sudo apt-get install -y libv4l-dev v4l-utils qv4l2 v4l2ucp
  sudo apt-get install -y curl
  sudo apt-get update

  echo "** Download opencv-4.1.0"
  cd $folder
  curl -L -o
  curl -L -o
  cd opencv-4.1.0/

  echo "** Building..."
  mkdir release
  cd release/

  make -j3
  sudo make install
  sudo apt-get install -y python-opencv python3-opencv

  echo "** Install opencv-4.1.0 successfully"
  echo "** Bye :)"

Save the above script as a sh file, for example,

Run the script

  mkdir opencv
  sh opencv

The installation may take a lot of time, please be patient and wait for the installation to end. It is recommended to use tmux to hang in the background.

Test the installed OpenCV

Test the statement import cv2 through the interactive interface of python, it is good if no error is reported.

However, considering that users who use tools such as pyenv for python version management, if they switch to the python version installed under pyenv, the installed OpenCV may not be recognized. The solution is to create a soft link to the OpenCV library and link it to the python library directory under the management of pyenv.

For example, suppose the library directory of the python version in pyenv is,


And the installed OpenCV library path is,


Then execute the following statement to create a soft link,

  cd /home/{username}/.anyenv/envs/pyenv/versions/3.6.8/lib/python3.6/site-packages
  ln -s /usr/local/lib/python3.6/dist-packages/cv2/python-3.6/ ./

Besides, use the python version of pip in pyenv to install numpy through the pip install numpy statement, and go to the interactive interface to test import cv2. If no error is displayed, the installation is complete.

Edit this page

Xin Li
Xin Li

Research in areas such as machine learning, deep learning, and artificial intelligence applications, specialize in priority sampling and learning algorithm for non-uniform data and offset data.Learn more

comments powered by Disqus