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:
- Open Applications > Utilities > Terminal…
- Install Homebrew by typing the following into Terminal
1ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install) - Run the command brew doctor to check for any errors
- 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:
1brew tap homebrew/science - Then you can run the usual Homebrew command to install OpenCV:
1brew install opencv
If this fails, it may be that you have ffmpeg installed – if so, try this instead:
1brew install opencv --env=std - This may take several minutes, so be patient!
- 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
- Navigate up to your home folder and open the .bash_profile file – if it doesn’t exist, create it yourself
- Add the line suggested by Homebrew, probably something like:
1export PYTHONPATH=/usr/local/lib/python2.7/site-packages:$PYTHONPATH - Save the file and close it – you may also have to exit Terminal and open it again
- Test by opening Python:
1python
Then try to import OpenCV:
1import cv - 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.
Thank you!!!!!! It worked perfectly! :)
Have you tried this on Mavericks? I can’t seem to get it to work without build errors.
Haven’t had time to do the install yet – at which step is it breaking?
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).
Hmm, I’ll have to upgrade and see, but my guess is that the Homebrew version of OpenCV hasn’t yet been updated for Mavericks:
http://stackoverflow.com/questions/19537206/opencv-for-python-on-mavericks
For what it’s worth, this issue seems to be resolved – I was able to install OpenCV on Mavericks without any issues.
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
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!
Worked like a charm! Thanks a ton, I was trying since quite a while.
Dude, this works for me, thanks…
Update in the first line
ruby -e “$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)”
should be changed to
ruby -e “$(curl -fsSL https://raw.github.com/mxcl/homebrew/go/install)”
Thanks, looks like the command has been updated.
If you are having problems on
brew tap homebrew/science
then runbrew update
and thenbrew tap --repair
. Then you canbrew install opencv
.How is this different than http://tilomitra.com/opencv-on-mac-osx/?
From a quick look, it appears that tutorial uses MacPorts and doesn’t show how to update the
PYTHONPATH
.My hero! I was trying to get openCV to work FOREVEr and this finally fixed it.
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.
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 :)
Worked perfectly – many thanks for the tutorial!!!
I recently bought a macbook and I wanted to set up OpenCV on it. This guide helped me a lot. Thank a lot for this guide.
Thanks so much! Been struggling to get this working but finally there!!
Thanks!!! It works!!!
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.
Don’t know – this just installs the usual distro of OpenCV, so if that didn’t work for you, then this likely won’t either.
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
>>>
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!
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.
So perfect that I want more.
Can you offer similar advice for visualpython?
Hey! I’m getting this error-
curl: (22) The requested URL returned error: 404
Error: Download failed: http://downloads.sf.net/project/libpng/libpng15/1.5.17/libpng-1.5.17.tar.bz2
Any idea of why it could be?
oh, it was an xcode issue. nevermind!
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.
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 tousr/local/bin
.Maybe be sure you properly edited your
.bash_profile
file?my .bash_profile has this code:
export PATH=/usr/local/bin:$PATH
export PYTHONPATH=/usr/local/lib/python2.7/site-packages:$PYTHONPATH
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?
In place of
formula_name
, writeopencv
.Thank you so much, works perfect
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.
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 !
*Le site interprète les crochets comme une balise html et l’élimine. Je voulais écrire :
#include < opencv2/highgui/highgui.hpp &rt
Sorry forgot to post in English …
There is a problem I havn’t managed to solve : when typing
#include < 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 !
On my machine, Homebrew installs actual files stored in the “cellar”
usr/local/Cellar
, with links inusr/local/bin
, which is probably where you want to link to.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.
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
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!
Worked like a charm, thanks!
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! :-)
Everything works except the final step: import cv.
Apparently “import: command not found”.
Help me pleeeeease someone!
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!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! :)
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?