Step by step installing OpenCV in Linux.

Installing OpenCV with conda or pip installs successfully. But importing the modules and using some methods run a lot of errors. Also tried installing on Linux Fedora like this. But it wouldn’t find the module when importing it in Python. Also couldn’t find the installation directory to add it to the path.

$ sudo dnf install python-opencv --setopt=install_weak_deps=False

Install in Linux Fedora from source as shown in the official OpenCV docs here.

$ sudo dnf install cmake
$ sudo dnf install python-devel numpy
$ sudo dnf install gcc gcc-c++
$ sudo dnf install gtk2-devel
$ sudo dnf install libdc1394-devel
$ sudo dnf install ffmpeg-devel

Note that the official docs has this:

$ yum install gstreamer-plugins-base-devel

But this shows an error on Fedora 34:

No match for argument: gstreamer-plugins-base-devel

The Fedora docs here show that the package is called gstreamer1

$ sudo dnf install gstreamer1-plugins-base-devel

Install optional dependencies

$ sudo dnf install libpng-devel
$ sudo dnf install libjpeg-turbo-devel
$ sudo dnf install jasper-devel
$ sudo dnf install openexr-devel
$ sudo dnf install libtiff-devel
$ sudo dnf install libwebp-devel

Install TBB (Threading Building Blocks) for parallelization, Eigen for math operations, and Doxygen for documentation

$ sudo dnf install tbb-devel
$ sudo dnf install eigen3-devel
$ sudo dnf install doxygen

Download OpenCV, configure and install

$ git clone
$ cd opencv
$ mkdir build
$ cd build
$ cmake -D WITH_OPENCL=OFF -D BUILD_opencv_gpu=OFF -D BUILD_opencv_gpuarithm=OFF -D BUILD_opencv_gpubgsegm=OFF -D BUILD_opencv_gpucodec=OFF -D BUILD_opencv_gpufeatures2d=OFF -D BUILD_opencv_gpufilters=OFF -D BUILD_opencv_gpuimgproc=OFF -D BUILD_opencv_gpulegacy=OFF -D BUILD_opencv_gpuoptflow=OFF -D BUILD_opencv_gpustereo=OFF -D BUILD_opencv_gpuwarping=OFF ..

Check the resulting configuration. Then install

$ make
$ su
$ make install

Check if the files are installed in /usr/local/. In my case my directory is the following:


In ~/.bashrc add this directory to the Python path

export PYTHONPATH=$PYTHONPATH:/usr/local/lib/python3.9/site-packages

Exit the terminal and open again.

Build the documentation with make doxygen.

$ cd opencv/build/
$ make doxygen

Open the documentation in the browser.

$ xdg-open doc/doxygen/html/index.html

Go to the directory where you want to start the project.

$ cd
$ cd some_directory

Create a conda environment:

$ conda update conda
$ conda create --name cv
$ conda activate cv

Test the install. Open Python. Check the numpy version:

>>> import numpy
>>> numpy.__version__

Check that OpenCV can be imported and version:

>>> import cv2 as cv
>>> cv.__version__

Test these methods to see if OpenCV is working correctly.

Copy an image to your current directory. Let’s say the image is awesome_image.png

(cv)$ python

Import libraries:

>>> import numpy as np
>>> import cv2 as cv

Read an image:

>>> img = cv.imread("awesome_image.png")
>>> cv.namedWindow("Image", cv.WINDOW_NORMAL)
>>> cv.imshow("Image", img)
>>> cv.waitKey(0)

Then the image will load. Press ESC to get back control of the Terminal.

>>> cv.imwrite("output.jpg", img)

It creates a copy of the file saved as output.jpg. The conversion changes the sizes.

$ ls -lh
25K awesome_image.png
65K output.jpg