Installing OpenCV on Mac

UPDATE: Mavericks users were likely frustrated for the last few weeks trying to install OpenCV. It appears the problem has been fixed, and these instructions should work without issue.

My previous post on installing OpenCV for Mac users is one of the most popular on this site (which is simultaneously surprising and fantastic). However, I recently switched from using MacPorts to Homebrew – users that need to migrate existing installs can check out this guide.

Still complicated but thoroughly do-able, here is how to install OpenCV on a Mac running Mountain Lion:

  1. Open Applications > Utilities > Terminal…
  2. Install Homebrew by typing the following into Terminal
  3. Run the command brew doctor to check for any errors
  4. While normal installations using Homebrew are very straightforward, for some reason OpenCV requires an extra step – you need to install an add-on called science:
  5. Then you can run the usual Homebrew command to install OpenCV:

    If this fails, it may be that you have ffmpeg installed – if so, try this instead:
  6. This may take several minutes, so be patient!
  7. Homebrew may tell you that you have to update  PYTHONPATH  – if so, open your favorite text editor, click File > Open… and hit command+shift+. to see hidden files
  8. Navigate up to your home folder and open the  .bash_profile  file – if it doesn’t exist, create it yourself
  9. Add the line suggested by Homebrew, probably something like:
  10. Save the file and close it – you may also have to exit Terminal and open it again
  11. Test by opening Python:

    Then try to import OpenCV:
  12. If you don’t get any errors, you’re all set! Now go enjoy a real beer!

Hope this helps! Problems, suggestions – leave them in the comments.

Did you find this post helpful or interesting? A small donation goes a long way towards helping produce this content. If you can't, please share what you've done with others!
Donate via Flattr

81 Replies to “Installing OpenCV on Mac”

  1. On Mac Mountain Lion, I had to do two things.

    Tuomas Rasila says correctly to:
    brew install eigen

    This eliminated the error:
    make[2]: *** [modules/contrib/CMakeFiles/opencv_contrib.dir/src/rgbdodometry.cpp.o] Error 1
    make[2]: *** Waiting for unfinished jobs….
    make[1]: *** [modules/contrib/CMakeFiles/opencv_contrib.dir/all] Error 2
    make: *** [all] Error 2

    Then Nis Sarup says:
    $ brew unlink opencv
    $ brew uninstall opencv
    $ brew install opencv

    This eliminated the error:
    Segmentation fault: 11

    when running the program with an ‘import cv2’

  2. When I try to import cv it gives “Segmentation fault: 11” error. I couldn’t find any solution for that :/

  3. I tried all of them however i couldn’t solve it. after recovery from time machine I followed same steps and it worked. I still don’t know the problem :/

  4. thanks for ur post, it works on me.

    one more question if u dont mind

    i would like to modify the opence_traincascade source n rebuild it

    how to do so ? any instructions on how to rebuild the opencv with my modify source ?

    thanks

  5. I don’t really work in C/C++, so I don’t know. I’m sure there’s tutorials on building OpenCV from source, but I’m guessing that build might conflict with the one you just installed.

  6. I am embarassed at asking this trivial question.
    How do I fix this problem?

    $ brew install opencv
    ==> Installing dependencies for opencv: cmake, eigen, jpeg, jasper, libpng, libtiff, ilmbase, openexr, pkg-config
    ==> Installing opencv dependency: cmake
    ==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/cmake-3.0.1.mavericks.bottle.tar.gz
    ######################################################################## 100.0%
    ==> Pouring cmake-3.0.1.mavericks.bottle.tar.gz
    Error: An unexpected error occurred during the brew link step
    The formula built, but is not symlinked into /usr/local
    No such file or directory – /usr/local/Cellar/cmake/2.8.12.2/share/cmake
    Error: No such file or directory – /usr/local/Cellar/cmake/2.8.12.2/share/cmake

  7. When I ran brew install opencv or brew install opencv –env=std, this was the output:
    ==> Installing opencv from homebrew/homebrew-science
    ==> Downloading https://github.com/Itseez/opencv/archive/2.4.9.tar.gz
    ######################################################################## 100.0%
    curl: (33) HTTP server doesn’t seem to support byte ranges. Cannot resume.
    ==> Trying a full download

    What is the curl error and how do I fix it?

    Thank you!

  8. Hi, I am following your steps to install OpenCV and it appears that the homebrew installer has moved. They supply the correct command when installing but thought you may want to change it on the post. This is command that I homebrew asked me to run instead: ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

  9. Thanks a lot Jeffrey. After about 4 hours of web browsing, unsuccessful installs, I tried your solution… and it works on Mac OS maverick (don’t want of Yosemite…) with Pycharm. Just remember to force your scripts to run with Python 2.7, if I am not wrong.
    Thanks again.
    Eric

  10. I run into following error, while doing import cv

    Traceback (most recent call last):
    File “”, line 1, in
    File “cv.py”, line 1, in
    from cv2.cv import *
    ImportError: dlopen(./cv2.so, 2): Library not loaded: /usr/local/lib/libpng16.16.dylib
    Referenced from: /usr/local/Cellar/opencv/2.4.10.1/lib/libopencv_highgui.2.4.dylib
    Reason: Incompatible library version: libopencv_highgui.2.4.dylib requires version 33.0.0 or later, but libpng16.16.dylib provides version 30.0.0

  11. Hmm, not sure. It looks like the issue is with libpng which might have been installed by Homebrew when installing OpenCV. Try running this command in the Terminal: brew update.

  12. Jeff – Tried running brew doctor, still running into the same issue. I’ll search some more. Error is clear though not sure how to fix it.

  13. Hi! I have a macbook. I’m trying to install opencv and use Netbeans IDE, but i can’t, it’s say, for example: “”main.cpp:23:10: fatal error: ‘highgui.h’ file not found””
    but I added the files. Hope you can help me, waiting for your answer, thanks!
    Macarena

  14. I am not being able to install any ideas?

    $ brew install opencv
    ==> Installing opencv from homebrew/homebrew-science
    ==> Installing dependencies for homebrew/science/opencv: gcc, eigen, jpeg
    ==> Installing homebrew/science/opencv dependency: gcc
    ==> Downloading http://ftpmirror.gnu.org/gcc/gcc-5.1.0/gcc-5.1.0.tar.bz2
    Already downloaded: /Library/Caches/Homebrew/gcc-5.1.0.tar.bz2
    ==> Patching
    patching file gcc/jit/Make-lang.in
    ==> ../configure –build=x86_64-apple-darwin14.3.0 –prefix=/usr/local/Cellar/gc
    ==> make bootstrap
    configure: error: cannot compute suffix of object files: cannot compile
    See config.log' for more details.
    make[2]: *** [configure-stage1-target-libgcc] Error 1
    make[1]: *** [stage1-bubble] Error 2
    make: *** [bootstrap] Error 2
    /usr/local/Library/Homebrew/build.rb:181:in
    dump’: can’t dump anonymous class #::Gcc (TypeError)
    from /usr/local/Library/Homebrew/build.rb:181:in rescue in '
    from /usr/local/Library/Homebrew/build.rb:167:in

    Error: Suspicious failure
    $

  15. Hmm, I really don’t know! Looks like an install/Homebrew/GCC error, not OpenCV. Did you try this version of the command instead: brew install opencv --env=std?

  16. I have somehow installed two different versions of python on my Mac (OsX Yosemite) i.e. 3.4.2 and 2.7.9. From command line my python 2.7.9 is configured but from IDLE it is configured to 3.4.2. I need to re-configure IDLE to 2.7.9. How do I do it?

  17. Thanks for this tutorial.
    Can I code in C++ using openCV after I install openCV this way? I have the impression that brew install the Python OpenCV?

  18. I don’t know for sure (I don’t do much C++) but you’re right, this is targeted at using Python with OpenCV. This post suggests that the base OpenCV gets installed regardless, it then is bound to Python, so I think it might work. Please let me know if it does!

  19. Hi,

    I followed you steps above and cv seems to be installed – if I now try ‘brew install cv’ I get ‘homebrew/science/opencv-2.4.13.2 already installed’.

    However when I run python or python3 and try ‘import cv’ I get the following error:

    Jonass-MacBook-Pro:~ Jonas$ python3
    Error in sitecustomize; set PYTHONVERBOSE for traceback:
    KeyError: ‘PYTHONPATH’
    Python 3.5.2 |Anaconda 4.2.0 (x86_64)| (default, Jul 2 2016, 17:52:12)
    [GCC 4.2.1 Compatible Apple LLVM 4.2 (clang-425.0.28)] on darwin
    Type “help”, “copyright”, “credits” or “license” for more information.
    >>> import cv
    Traceback (most recent call last):
    File “”, line 1, in
    File “/usr/local/lib/python2.7/site-packages/cv.py”, line 1, in
    from cv2.cv import *
    ImportError: dlopen(/usr/local/lib/python2.7/site-packages/cv2.so, 2): Symbol not found: _PyCObject_Type
    Referenced from: /usr/local/lib/python2.7/site-packages/cv2.so
    Expected in: flat namespace
    in /usr/local/lib/python2.7/site-packages/cv2.so

    Any idea how to solve this?

    Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *