Taha Bagatur

Home › Lab

A dated build log

PyTorch, TorchVision and OpenCV on a Jetson Orin (JetPack 6.1)

Read this first. I wrote these steps in March 2025 for a Jetson Orin running JetPack 6.1 (Ubuntu 22.04, Python 3.10, CUDA 12.6). I published them as working at the time. I have not re-run them for this page, and version numbers move fast. Treat every pinned version below as an example, and check the NVIDIA compatibility table first.

Order of work

  1. Install the NVIDIA PyTorch wheel that matches your JetPack.
  2. If no wheel fits, build PyTorch from source.
  3. Build TorchVision to match that PyTorch version.
  4. Build OpenCV with CUDA (the preinstalled one has no GPU support).

1. Prepare the system

Free space of around 5 GB is the minimum for the wheel route; a source build needs much more, plus patience.

sudo apt-get update && sudo apt-get upgrade -y
sudo apt-get install -y python3-pip libopenblas-dev libopenmpi-dev \
  libjpeg-dev zlib1g-dev libpng-dev python3-setuptools

Package names change between Ubuntu releases. If apt cannot find one, search with apt-cache search instead of forcing it.

2. PyTorch from NVIDIA's wheel

NVIDIA publishes PyTorch wheels built for Jetson. Pick the one for your JetPack and CUDA version from NVIDIA's download index (the install guide explains the naming), download it, then install it:

sudo -H pip3 install --upgrade pip
sudo -H pip3 install ./torch-2.5.0a0+872d972e41.nv24.08.17622132-cp310-cp310-linux_aarch64.whl   # example file name; use yours
python3 -c "import torch; print(torch.__version__, torch.cuda.is_available())"

You want to see a version and True. If the version is wrong for your JetPack, or CUDA reports False, go to the next section.

3. If no wheel fits: build PyTorch from source

This is the fallback and it is slow, often several hours. Set the board to a high-performance mode first so the build does not stall on clocks:

sudo nvpmodel -q                # show the current mode
sudo nvpmodel -m 0             # on many Orin images 0 is MAXN; check /etc/nvpmodel.conf for your model
sudo jetson_clocks             # pin clocks high while building

In my original draft the comments on -m 0 and -m 2 were swapped. Mode numbers differ between Orin models, so read /etc/nvpmodel.conf rather than trusting a number from a blog post, including this one.

Set the build options, then clone, check out the tag and build. TORCH_CUDA_ARCH_LIST must match your GPU's compute capability (8.7 for Orin).

export USE_NCCL=1
export USE_DISTRIBUTED=1
export USE_QNNPACK=0
export USE_PYTORCH_QNNPACK=0
export TORCH_CUDA_ARCH_LIST="8.7"   # Orin; see the compute capability table for other boards
export PYTORCH_BUILD_VERSION=2.5.0
export PYTORCH_BUILD_NUMBER=1
cd ~
git clone --recursive https://github.com/pytorch/pytorch.git
cd pytorch
git checkout v2.5.0
sudo apt-get install -y cmake ninja-build python3-dev libopenblas-dev libopenmpi-dev \
  libomp-dev libjpeg-dev libpng-dev libtiff-dev libpython3-dev \
  libavcodec-dev libavformat-dev libswscale-dev ccache
pip3 install --upgrade pip
pip3 install -r requirements.txt
pip3 install scikit-build ninja
python3 setup.py bdist_wheel      # can take several hours
pip3 install --no-cache-dir dist/torch-*.whl
python3 -c "import torch; print(torch.__version__, torch.cuda.is_available())"

4. TorchVision

When I wrote this there was no prebuilt TorchVision wheel for JetPack 6.1, so I built it from source. The version must match the PyTorch you installed (compatibility table); PyTorch 2.5.0 pairs with TorchVision 0.20.0.

sudo apt-get install -y cmake ninja-build gcc g++
cd ~
git clone https://github.com/pytorch/vision.git
cd vision
git checkout tags/v0.20.0
sudo -H python3 setup.py install
python3 -c "import torch, torchvision; print(torch.__version__, torch.cuda.is_available(), torchvision.__version__)"

Expect True for CUDA and matching versions. If the build complains about image libraries, install libjpeg-dev, libpng-dev and libtiff-dev first.

5. OpenCV with CUDA

The OpenCV that ships on the image works but has no CUDA. To use the GPU you build it yourself.

sudo sh -c "echo '/usr/local/cuda/lib64' >> /etc/ld.so.conf.d/nvidia-tegra.conf"
sudo ldconfig
sudo apt-get install -y build-essential cmake git unzip pkg-config zlib1g-dev \
  libjpeg-dev libpng-dev libtiff-dev libglew-dev libavcodec-dev libavformat-dev \
  libswscale-dev libgtk-3-dev python3-dev python3-numpy python3-pip \
  libxvidcore-dev libx264-dev libtbb-dev v4l-utils libv4l-dev gstreamer1.0-tools \
  libgstreamer-plugins-base1.0-dev libgstreamer-plugins-good1.0-dev \
  libopenblas-dev liblapack-dev libeigen3-dev gfortran libhdf5-dev

My first list also had libfaac-dev, libdc1394-22-dev and several older GTK and codec packages. I trimmed the list for this page: some of those names are missing on Ubuntu 22.04 and are not needed for the CUDA modules. The shorter list is my edit and is untested, so add back whatever your build asks for.

cd ~
OPENCV_VERSION=4.10.0    # example tag; use one release for both repositories
git clone --depth=1 --branch "$OPENCV_VERSION" https://github.com/opencv/opencv.git
git clone --depth=1 --branch "$OPENCV_VERSION" https://github.com/opencv/opencv_contrib.git

The first version of this guide cloned the default branch. Pin both repositories to the same release tag so the main modules and opencv_contrib agree.

cd ~/opencv && mkdir -p build && cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE \
  -D CMAKE_INSTALL_PREFIX=/usr/local \
  -D OPENCV_EXTRA_MODULES_PATH="$HOME/opencv_contrib/modules" \
  -D WITH_CUDA=ON -D CUDA_ARCH_BIN=8.7 \
  -D WITH_CUDNN=ON -D WITH_CUBLAS=ON \
  -D ENABLE_FAST_MATH=ON -D CUDA_FAST_MATH=ON -D OPENCV_DNN_CUDA=ON \
  -D WITH_GSTREAMER=ON -D WITH_FFMPEG=ON -D WITH_V4L=ON -D WITH_LIBV4L=ON \
  -D WITH_TBB=ON -D WITH_OPENMP=ON -D WITH_EIGEN=ON \
  -D OPENCV_ENABLE_NONFREE=ON \
  -D BUILD_TESTS=OFF -D BUILD_EXAMPLES=OFF \
  -D INSTALL_C_EXAMPLES=OFF -D INSTALL_PYTHON_EXAMPLES=OFF \
  -D PYTHON3_PACKAGES_PATH=/usr/lib/python3/dist-packages \
  -D OPENCV_GENERATE_PKGCONFIG=ON ..
make -j"$(nproc)"
sudo make install
sudo ldconfig

CUDA_ARCH_BIN=8.7 is for Orin only. The original used -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules; the shell does not expand ~ after an equals sign, so use $HOME. On an 8 GB board a full -j$(nproc) build can run out of memory; lower the job count or add swap if it dies.

python3 -c "import cv2; print(cv2.getBuildInformation())" | grep -i cuda

You should see NVIDIA CUDA: YES and the CUDA version.

6. Monitor and clean up

jtop (from jetson-stats) shows CPU, GPU, memory and power live.

sudo -H pip3 install -U jetson-stats
sudo reboot
# after the reboot:
jtop
sudo apt-get autoremove -y && sudo apt-get clean
rm -rf ~/opencv ~/opencv_contrib    # frees roughly 275 MB; keep them if you plan to rebuild

What I would check before following this today

First published on Medium in March 2025 as three separate posts (PyTorch troubleshooting, PyTorch and TorchVision, OpenCV). Combined and corrected here; the numbering skip in the original TorchVision post is fixed. Written for JetPack 6.1, not re-tested for this page.

by Taha Bağatur → tahabagatur.com