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 2.4.6.1, at opencv_videostab – but actually every version fails somewhere. Using brew + Xcode CLT 5.0.1.

    I can’t seem to build it from source either (i.e. non-brew).

  2. I was able to install open cv 2.4.6.1 but when i open python and try to import cv i get an error that says:

    File “”, line 1, in
    ImportError: No module named cv

  3. Never mind I figured it out. I missed the update python path message after i installed it. i followed your instructions to update that and it works now. Thanks!

  4. I had to add quote marks surrounding the path to get it running:

    export PYTHONPATH=”/usr/local/lib/python2.7/site-packages:$PYTHONPATH”

    thanks for the tutorial.

  5. I tried installing OpenCV a while ago. Something went wrong and I ended up with a segmentation fault 11 when importing opencv.
    I did the following:

    $ brew unlink opencv
    $ brew uninstall opencv
    $ brew install opencv

    Basically a reinstall. Everything works now :)

  6. Will opencv installed this way be able to load videos?

    My main issue with my current opencv installation is that it is not able to work with videos.

  7. Not having much success with installing SimpleCV on iMac and Mavericks
    Have followed above and others. I think I am close. Very grateful for any suggestions. Big kiss for the correct one. Thank you.

    My .bash_profile reads
    # Set architecture flags
    export ARCHFLAGS=”-arch x86_64″
    # Ensure user-installed binaries take precedence
    export PATH=/usr/local/bin:$PATH
    export PYTHONPATH=/usr/local/lib/python2.7/site-packages:$PYTHONPATH

    Terminal screen
    server:PythonF xx$ brew install opencv
    Warning: opencv-2.4.8.2 already installed
    server:PythonF xx$ which python
    /usr/local/bin/python
    server:PythonF xx$ python
    Python 2.7.6 (v2.7.6:3a1db0d2747e, Nov 10 2013, 00:42:54)
    [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
    Type “help”, “copyright”, “credits” or “license” for more information.
    >>> import cv
    Segmentation fault: 11 (plus a problem report for python)

    server:PythonF xx$ python
    Python 2.7.6 (v2.7.6:3a1db0d2747e, Nov 10 2013, 00:42:54)
    [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
    Type “help”, “copyright”, “credits” or “license” for more information.
    >>> import SimpleCV
    Traceback (most recent call last):
    File “”, line 1, in
    ImportError: No module named SimpleCV
    >>>

  8. Jon, I’m really not sure, but my guess is you installed OpenCV previously, maybe using Mac Ports? If so, Homebrew won’t be able to install it and you’ll get the error you see above.

    If this is the case, try this guide. Otherwise, I’m sorry, I have no idea!

  9. Thank you for your reply. I have installed OpenCV numerous times I fear but have not been near MacPorts. I have brew uninstalled all that I could and reinstalled but I have stalled. Time to move on.

  10. It gives me this error when i type 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): Library not loaded: lib/libopencv_core.2.4.dylib
    Referenced from: /usr/local/lib/python2.7/site-packages/cv2.so
    Reason: image not found

    Anyone can help me? I reinstall brew and opencv too much times and i don’t know what i have to do.

  11. Hmm. My only thought is the path your computer is looking for OpenCV in seems odd.

    For my computer the actual files stored in usr/local/Cellar, but links to usr/local/bin.

    Maybe be sure you properly edited your .bash_profile file?

  12. my .bash_profile has this code:

    export PATH=/usr/local/bin:$PATH
    export PYTHONPATH=/usr/local/lib/python2.7/site-packages:$PYTHONPATH

  13. I type brew doctor and the result is this:

    goncalos-mbp:~ goncalo$ brew doctor
    Warning: You have unlinked kegs in your Cellar
    Leaving kegs unlinked can lead to build-trouble and cause brews that depend on
    those kegs to fail to run properly once built. Run brew link on these:

    opencv
    goncalos-mbp:~ goncalo$ brew link opencv
    Linking /usr/local/Cellar/opencv/2.4.8.2…
    Warning: Could not link opencv. Unlinking…
    Error: Could not symlink file: /usr/local/Cellar/opencv/2.4.8.2/bin/opencv_performance
    Target /usr/local/bin/opencv_performance already exists. You may need to delete it.
    To force the link and overwrite all other conflicting files, do:
    brew link –overwrite formula_name

    To list all files that would be deleted:
    brew link –overwrite –dry-run formula_name

    I do brew link –overwrite formula name and the terminal give me this:

    goncalos-mbp:~ goncalo$ brew link –overwrite formula_name
    Error: No such keg: /usr/local/Cellar/formula_name

    Anyone help me?

  14. Thanks a lot ! Installation went well.
    After installing opencv, homebrew gave me those two commands to execute :
    mkdir -p ~/Library/Python/2.7/lib/python/site-packages
    echo ‘/usr/local/lib/python2.7/site-packages’ > ~/Library/Python/2.7/lib/python/site-packages/homebrew.pth
    and after that, the instruction
    import cv
    went without error.

  15. Il y a cependant un problème au moment de faire
    #include
    sous XCode, qui indique
    ‘opencv2/highgui/highgui.hpp’ file not found

    Je suppose qu’il faut modifier le Library Search Path, mais lequel faut-il indiquer ? (autrement dit, où ont été installés les fichiers ?)
    Merci !

  16. *Le site interprète les crochets comme une balise html et l’élimine. Je voulais écrire :
    #include &lt opencv2/highgui/highgui.hpp &rt

  17. Sorry forgot to post in English …

    There is a problem I havn’t managed to solve : when typing
    #include &lt opencv2/highgui/highgui.hpp &rt
    in XCode, it indicates
    ‘opencv2/highgui/highgui.hpp’ file not found

    I guess I have to modify the Library Search Path, but in what way should I fill it ? (said differently, where are now the files that homebrew installed ?)
    Thanks a lot !

  18. On my machine, Homebrew installs actual files stored in the “cellar” usr/local/Cellar, with links in usr/local/bin, which is probably where you want to link to.

  19. Thank you, the files where indeed in this folder.
    Xcode was still not happy though, I had to put the absolute path everywhere to indicate him where were the files to include … I’m sure there’s a better way, but at least it works for me.

  20. On my case with with OS X 10.9.2 what I needed to do to fix this work was “brew install eigen”.

    Before that it kept on saying:
    ==> make
    1 error generated.
    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

  21. I’m having two problems:

    When I run brew doctor, I get this:

    Warning: Python is installed at /Library/Frameworks/Python.framework

    I decided to go ahead, and when I ran the brew install opencv command, I got this error:

    opencv: Unsatisfied dependency: numpy
    Homebrew does not provide Python dependencies; install with:
    pip install numpy
    Error: An unsatisfied requirement failed this build.

    What should I do?

    Thanks!

  22. Worked for me too! I’m using Mac OS X 10.9.3. The only hiccup was installing XCode. I didn’t realize I needed that. I was prompted to install the command line XCode tool during the install, but I stopped and did the full install of XCode from the App Store and started over. Setting up the path at the end required some different steps, but I just followed the instructions supplied by the installer:

    If you need Python to find the installed site-packages:
    mkdir -p ~/Library/Python/2.7/lib/python/site-packages
    echo ‘/usr/local/lib/python2.7/site-packages’ > ~/Library/Python/2.7/lib/python/site-packages/homebrew.pth

    Thanks! :-)

  23. Everything works except the final step: import cv.
    Apparently “import: command not found”.

    Help me pleeeeease someone!

  24. Are you running Python in the Terminal, or via a script? If it’s a script saved to file, you might need to add #!/usr/bin/python to the first line. Otherwise, I’d post to Stack Overflow – sounds like a problem with your Python installation!

  25. It was in the terminal but a few hours down the line, I’ve solved the issue. I had to change some permissions and literally keep trying, I didn’t have to add that to the first line. Thanks, your post really helped! :)

  26. I was able to install open cv 2.4.6.1 but when i open python and try to import cv i get an error that says:

    File “”, line 1, in
    ImportError: No module named cv
    how do i update my python path?

Leave a Reply

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