Skip to content

Building on macOS

  1. Install Xcode from the Mac App Store (required for the compiler toolchain).

  2. Install Xcode Command Line Tools:

    Terminal window
    xcode-select --install
  3. Install Homebrew (if not already installed):

    Terminal window
    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
  4. Install CMake and dependencies via Homebrew:

    Terminal window
    brew install cmake pkg-config
    brew install jpeg libpng libtiff openexr
    brew install eigen tbb
  5. Install Python and NumPy (for Python bindings):

    Terminal window
    brew install python3
    pip3 install numpy
Terminal window
git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git
git clone https://github.com/opencv/opencv_extra.git
mkdir build_opencv && cd build_opencv
cmake \
-DCMAKE_BUILD_TYPE=Debug \
-DBUILD_TESTS=ON \
-DBUILD_PERF_TESTS=ON \
-DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules \
-DOPENCV_TEST_DATA_PATH=../opencv_extra/testdata \
../opencv
make -j$(sysctl -n hw.ncpu)
Terminal window
# Generate Xcode project files
cmake -G Xcode \
-DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules \
../opencv
# Open in Xcode
open OpenCV.xcodeproj

In Xcode, Debug/Release is selected from the scheme toolbar. Set the build scheme to ALL_BUILD to compile everything, or select individual test targets.

VS Code with the CMake Tools extension works well. After building, set OpenCV_DIR in your project’s CMakeLists.txt to the build directory:

set(OpenCV_DIR "/path/to/build_opencv")
find_package(OpenCV REQUIRED)
Terminal window
# After build, set PYTHONPATH to use cv2 from build dir
export PYTHONPATH=/path/to/build_opencv/python_loader:/path/to/build_opencv/lib/python3/
export DYLD_LIBRARY_PATH=/path/to/build_opencv/lib:$DYLD_LIBRARY_PATH
python3 -c "import cv2; print(cv2.__version__)"