Installing OpenCV

  1. Log in to the server as the root user.
  2. In any directory (for example, /home) on the server, run the following command to install OpenCV:
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    # Download and unpack sources
    wget -O opencv.zip https://github.com/opencv/opencv/archive/4.x.zip --no-check-certificate
    unzip opencv.zip
    # Create build directory and switch into it
    mkdir -p build && cd build
    
    # Configure
    cmake -DCMAKE_BUILD_TYPE=Release \
    	  -DCMAKE_INSTALL_PREFIX=/usr/local \
              -DPYTHON_INCLUDE_DIR=$(python3 -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())")  \
              -DPYTHON_LIBRARY=$(python3 -c "import distutils.sysconfig as sysconfig; print(sysconfig.get_config_var('LIBDIR'))")  \
    	  ../opencv-4.x/
    
    make -j8 && make install
    
  3. After the installation, the paths of the header files and library files are as follows:
    • Header file path: /usr/local/include/opencv4/
    • Library file path: /usr/local/lib

      The path shown in the following figure is only an example. The onsite system environment may be /usr/local/lib64. Use the actual installation path.

      You can run the following command to go to the /usr/local/lib directory (or /usr/local/lib64 directory, which is subject to the actual installation path) to check whether the library file exists:

      1
      2
      cd /usr/local/lib
      ls
      

      The library files are as follows:

  4. Configure environment variables. (The library filepath /usr/local/lib is used as an example. Replace it with the actual installation path.)
    1
    export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH