Skip to content

Instantly share code, notes, and snippets.

@ruslangrimov
Last active September 2, 2021 12:22
Show Gist options
  • Save ruslangrimov/6e57ac24943cceb9ebac5d50a2025d17 to your computer and use it in GitHub Desktop.
Save ruslangrimov/6e57ac24943cceb9ebac5d50a2025d17 to your computer and use it in GitHub Desktop.
Install OpenCV on Ubuntu
# Prepare
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install build-essential cmake unzip pkg-config
sudo apt-get install libjpeg-dev libpng-dev libtiff-dev
sudo apt-get install libjasper-dev
# If previous line doesn't work
sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main"
sudo apt update
sudo apt install libjasper1 libjasper-dev
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
sudo apt-get install libxvidcore-dev libx264-dev
sudo apt-get install libgtk-3-dev
sudo apt-get install libatlas-base-dev gfortran
sudo apt-get install python3.6-dev
# Download
mkdir ~/tmp && cd ~/tmp
wget -O opencv.zip https://github.com/opencv/opencv/archive/4.5.2.zip
wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.5.2.zip
# Unpack
unzip opencv.zip
unzip opencv_contrib.zip
mv opencv-4.5.2 opencv
mv opencv_contrib-4.5.2 opencv_contrib
cd opencv
mkdir build && cd build
ls $HOME/.local
# Clear PATH from anaconda as it has its own libtiff
conda deactivate
python --version
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
echo $PATH
# Configure
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=$HOME/.local -D WITH_CUDA=OFF -D OPENCV_EXTRA_MODULES_PATH=~/tmp/opencv_contrib/modules -D OPENCV_ENABLE_NONFREE=ON -D BUILD_EXAMPLES=ON ..
# Build
make -j12
# Install to $HOME/.local
sudo make install
sudo ldconfig
# Check folders
ls $HOME/.local/
ls $HOME/.local/include/
ls $HOME/.local/bin
# Go to our project
cd project_folder/
cd build
cmake .. -DCMAKE_PREFIX_PATH=$HOME/.local
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment