Installing OpenCV 3 With Python On Mac OS X

1 mainOpenCV is the world’s most popular computer vision library and it’s used extensively by researchers and developers around the world. OpenCV has been around for a while now and they add something new and interesting with every new release. One of the main additions of OpenCV 3 is “opencv_contrib” which contains a lot of cutting edge algorithms for feature descriptors, text detection, object tracking, shape matching, and so on. They have greatly improved Python support in this release as well. Since OpenCV is available on almost all the popular platforms, this version looks very promising. Let’s see how to install OpenCV 3 with Python support on Mac OS X.  


CMake: Make sure you have cmake. If you don’t, you can download it from here. It’s a dmg file, so you can just download it and run the installer.

Install Python using Homebrew: This is an important step! Homebrew is a package manager for OS X that makes our lives easier in many different ways. Instead of using system Python, we need to use brewed Python (this is basically Python installed using Homebrew). If you don’t have Homebrew, you can install it using the following command:

$ ruby -e "$(curl -fsSL

Now that Homebrew is installed, let’s update it and install Python:

$ brew update
$ brew install python

Open up your ~/.profile file and add the following line:

export PATH=/usr/local/bin:$PATH

We need to reload the file to update the environment variables. Run the following command to do it:

$ source ~/.profile

Let’s confirm that you are using brewed Python. Run the following command from your terminal:

$ which python

If you see “/usr/local/bin/python” printed on your terminal, you can proceed.

Download OpenCV 3.0.0: You can download it from here.

Download “opencv_contrib”: As discussed earlier, we can use the latest computer vision algorithms from “opencv_contrib”. It is basically a repository that contains state of the art algorithms. Bear in mind that some of them are not free for commercial use, but it is great tool to learn new algorithms. Download opencv_contrib from here.


We are now ready to build. Run the following commands from you terminal:

$ cd /path/to/opencv-3.0.0/
$ mkdir build
$ cd build
$ cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/path/to/opencv-3.0.0/build -D PYTHON2_LIBRARY=/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/bin -D PYTHON2_INCLUDE_DIR=/usr/local/Frameworks/Python.framework/Headers -D PYTHON2_PACKAGES_PATH=/usr/local/lib/python2.7/site-packages -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D OPENCV_EXTRA_MODULES_PATH=/path/to/opencv_contrib-3.0.0/modules ../

Let’s take a moment to understand what these flags mean exactly:

  • CMAKE_BUILD_TYPE=RELEASE : We are telling cmake that we are building a “release” version of OpenCV.
  • CMAKE_INSTALL_PREFIX : This is the directory where OpenCV 3.0.0 will be installed
  • PYTHON2_LIBRARY : This is the path to your brewed Python (Hombrew installation of Python)
  • PYTHON2_INCLUDE_DIR : This is the path to Python header files for compilation.
  • INSTALL_C_EXAMPLES : This flag indicates that the C/C++ examples need to be installed after compilation.
  • INSTALL_PYTHON_EXAMPLES : This flag indicates that the Python examples need to be installed after compilation.
  • BUILD_EXAMPLES=ON : This flag indicates that we want to compile the included OpenCV examples.
  • OPENCV_EXTRA_MODULES_PATH : This flag indicates that OpenCV should compile the extra modules (opencv_contrib) that we downloaded earlier.

Let’s go ahead and install OpenCV 3.0.0. Make sure you are inside the directory “/path/to/opencv-3.0.0/build” and run the following commands:

$ make -j4
$ make install

The “-j4” flag indicates that it should use 4 cores. We are not done yet! Let’s set the library path:

$ export DYLD_LIBRARY_PATH=/path/to/opencv-3.0.0/build/lib:$DYLD_LIBRARY_PATH

If you want to make it permanent, just add the following line in your “~/.profile” file:

export DYLD_LIBRARY_PATH=/path/to/opencv-3.0.0/build/lib:$DYLD_LIBRARY_PATH

We need to copy the pkg-config file “opencv.pc” to “/usr/local/lib/pkgconfig” and name it “opencv3.pc” so that it doesn’t conflict with our existing OpenCV 2.4.x config file:

$ cp /path/to/opencv-3.0.0/build/lib/pkgconfig/opencv.pc /usr/local/lib/pkgconfig/opencv3.pc

We also need to update our PKG_CONFIG_PATH environment variable to make sure it knows where opencv3.pc is located. Open up your “~/.profile” file and add the following line:

export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig/:$PKG_CONFIG_PATH

Reload your “~/.profile” file.

$ source ~/.profile

Let’s see if OpenCV with C++ is working:

$ cd /path/to/opencv-3.0.0/samples/cpp
$ g++ -ggdb `pkg-config --cflags --libs opencv3` opencv_version.cpp -o /tmp/opencv_version && /tmp/opencv_version

If you see “Welcome to OpenCV 3.0.0” printed on the terminal, you are good! Let’s check the OpenCV-Python version:

$ python -c "import cv2; print cv2.__version__"

You should see “3.0.0” printed on the terminal. If you see that, then you are done! You have successfully installed OpenCV 3 with Python support on Mac OS X. Let’s check if it’s working by using something that exists in OpenCV 3.0.0 but not in OpenCV 2.4.9. Go into Python shell by typing “python” in your terminal and run the following commands:

>>> import cv2
>>> detector = cv2.AKAZE_create()

If the above line doesn’t throw an error, then you are all set! You have now successfully verified your OpenCV 3 installation with Python support.


16 thoughts on “Installing OpenCV 3 With Python On Mac OS X

  1. Hello,

    thanks for your post.

    I am at tis step :
    $ make -j4

    And an error is generated :

    [ 74%] Linking CXX executable ../../bin/opencv_test_calib3d
    [ 74%] Built target opencv_test_calib3d
    make: *** [all] Error 2

    When I look before, i’ve got this error :

    [ 72%] Building CXX object modules/python2/CMakeFiles/opencv_python2.dir/__/src2/cv2.cpp.o
    /Applications/opencv-3.0.0/modules/python/src2/cv2.cpp:6:10: fatal error:
    ‘Python.h’ file not found

    Could you help me ?

    D. Massenet

  2. Hello,

    I’ve improved but at the last step :
    $ python -c “import cv2; print cv2.__version__”
    I obtain :
    ImportError : No module named cv2

  3. Updated PYTHON2_LIBRARY and PYTHON2_INCLUDE_DIR values to reflect my installation (e.g., /usr/local/Cellar/python/2.7.10_2/… blah). Build then succeeds, as does C++ test for version.

    Python binding test fails.

    ImportError: No module named cv2.

    BTW, you can apparently leave off both PYTHON2_LIBRARY and PYTHON2_INCLUDE_DIR and it builds fine (still does not work, however).

      1. Hi Prateek,
        Thanks for this tutorial!

        I have the same experience as Rick. I have tried so many times to install this python library, with always the same result… Since I did not have a ~/.profile file, I created one, with no result. I have also put the “export DYLD…” line in the ~/.bash_profile in case, still not recognized in python…

        Any ideas you might have would be greatly appreciated!

  4. Hi Prateek,
    I moved from using Macports to homebrew. Macports used to store everything in /opt and homebrew keeps it in /usr/local. I need to uninstall my 2.4.9, but when run make uninstall, it cant find cmake and looks for cmake in /opt whereas the new cmake installation is in /usr/local:

    make: /opt/local/bin/cmake: No such file or directory
    make: *** [cmake_check_build_system] Error 1

    Any ideas how to point cmake to its right directory?

    1. You don’t have to uninstall OpenCV 2.4.9 to make 3.0.0 work. But if you really want to get rid of it, you can uninstall cmake using brew. This way, you can get your cmake to point to /opt/local/bin and get through the uninstallation process.

  5. Hi Prateek,
    I did everything as instructed, but at the end it doesn’t work with C++ or Python. When I try

    $ cd /path/to/opencv-3.0.0/samples/cpp
    $ g++ -ggdb `pkg-config –cflags –libs opencv3` opencv_version.cpp -o /tmp/opencv_version && /tmp/opencv_version

    I get this:

    -bash: pkg-config: command not found
    opencv_version.cpp:1:10: fatal error: ‘opencv2/core/utility.hpp’ file not found
    1 error generated.

    And when I try:

    python -c “import cv2; print cv2.__version__”

    I get:

    Traceback (most recent call last):
    File “”, line 1, in
    ImportError: No module named cv2

    I have set DYLD_LIBRARY_PATH to “/path/to/opencv-3.0.0/build/lib” in my “~/.profile” file and when I type “which python” I get /usr/local/bin/python as I should.

    If you could help me, I’d greatly appreciate it!

  6. Hi Prateek,
    I believe what is missing in the cmake command because of which python packages aren’t being built and we get no module named cv2 is the python packages path specification:


Leave a Reply to Prateek Joshi Cancel reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s