Building opencv 4.5.5 on Jetson AGX Xavier

Hi, I’m trying to build OpenCV 4.5.5 on my Jetson AGX Xavier.
But get error.
There is the script that should install the OpenCV:


#!/bin/bash
 
version="4.5.5"
folder="workspace"
 
echo "** Remove other OpenCV first"
sudo sudo apt-get purge *libopencv*
 
 
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 libdc1394-22-dev
sudo apt-get install -y libv4l-dev v4l-utils qv4l2 v4l2ucp
sudo apt-get install -y curl
 
 
echo "** Download opencv-"${version}
mkdir $folder
cd ${folder}
curl -L https://github.com/opencv/opencv/archive/${version}.zip -o opencv-${version}.zip
curl -L https://github.com/opencv/opencv_contrib/archive/${version}.zip -o opencv_contrib-${version}.zip
unzip opencv-${version}.zip
unzip opencv_contrib-${version}.zip
cd opencv-${version}/
 
 
echo "** Building..."
mkdir release
cd release/
cmake -D WITH_CUDA=ON \
      -D WITH_CUDNN=ON \
      -D CUDA_ARCH_BIN="5.3,6.2,7.2" \
      -D CUDA_ARCH_PTX="" \
      -D OPENCV_GENERATE_PKGCONFIG=ON \
      -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-${version}/modules \
      -D WITH_GSTREAMER=ON \
      -D WITH_LIBV4L=ON \
      -D BUILD_opencv_python2=ON \
      -D BUILD_opencv_python3=ON \
      -D BUILD_TESTS=OFF \
      -D BUILD_PERF_TESTS=OFF \
      -D BUILD_EXAMPLES=OFF \
      -D CMAKE_BUILD_TYPE=RELEASE \
      -D PYTHON3_PACKAGES_PATH=/usr/lib/python3/dist-packages \
      -D CMAKE_INSTALL_PREFIX=/usr/local ..
make -j$(nproc)
sudo make install
 
 
echo "** Install opencv-"${version}" successfully"
echo "** Bye :)"

And there is the error:

[ 89%] Building CXX object modules/sfm/src/libmv/libmv/multiview/CMakeFiles/multiview.dir/euclidean_resection.cc.o
c++: internal compiler error: Segmentation fault (program cc1plus)
Please submit a full bug report,
with preprocessed source if appropriate.
See <file:///usr/share/doc/gcc-7/README.Bugs> for instructions.
modules/sfm/src/libmv/libmv/multiview/CMakeFiles/multiview.dir/build.make:86: recipe for target 'modules/sfm/src/libmv/libmv/multiview/CMakeFiles/multiview.dir/euclidean_resection.cc.o' failed
make[2]: *** [modules/sfm/src/libmv/libmv/multiview/CMakeFiles/multiview.dir/euclidean_resection.cc.o] Error 4
CMakeFiles/Makefile2:6502: recipe for target 'modules/sfm/src/libmv/libmv/multiview/CMakeFiles/multiview.dir/all' failed
make[1]: *** [modules/sfm/src/libmv/libmv/multiview/CMakeFiles/multiview.dir/all] Error 2
Makefile:162: recipe for target 'all' failed
make: *** [all] Error 2

I enlarged the swap memory of jetson:
sudo apt-get install dphys-swapfile
and enlarged CONF_MAXSWAP to 8192 in /sbin/dphys-swapfile and /etc/dphys-swapfile

I can not figure what is going wrong. Any suggestions?

I think there is something wrong : two sudo
sudo sudo apt-get purge libopencv

When I did your codes, I get success message (Jetson Xavier AGX)