Install OpenCV 3 on macOS with Homebrew (the easy way)


Over the past few weeks I have demonstrated how to compile OpenCV 3 on macOS with Python (2.7, 3.5) bindings from source.

Compiling OpenCV via source gives you complete and total control over which modules you want to build, how they are built, and where they are installed.

All this control can come at a price though.

The downside is that determining the correct CMake paths to your Python interpreter, libraries, and include directories can be non-trivial, especially for users who are new to OpenCV/Unix systems.

That begs the question…

“Is there an easier way to install OpenCV on macOS? A way that avoids the complicated CMake configuration?”

It turns out, there is — just use Homebrew, what many consider to be “the missing package manager for Mac”.

So, is it really that easy? Just can a few simple keystrokes and commands can be used to avoid the hassle and install OpenCV 3 without the headaches?

Well, there’s a little more to it than that…but the process is greatly simplified. You lose a bit of control (as compared to compiling from source), but what you gain is an easier to follow path to installing OpenCV on your Mac system.

To discover the easy way to install OpenCV 3 on macOS via Homebrew, just keep reading.

Install OpenCV 3 on macOS with Homebrew (the easy way)

The remainder of this blog post demonstrates how to install OpenCV 3 with both Python 2.7 and Python 3 bindings on macOS via Homebrew. The benefit of using Homebrew is that it greatly simplifies the install process (although it can pose problems of its own if you aren’t careful) to only a few set of commands that need to be run.

If you prefer to compile OpenCV from source with Python bindings on macOS, please refer to these tutorials:

Step #1: Install XCode

Before we can install OpenCV 3 on macOS via Homebrew, we first need to install Xcode, a set of software development tools for the Mac Operating System.

Download Xcode

The easiest method to download and install Xcode is to use the included App Store application on your macOS system. Simply open up App Store, search for “Xcode” in the search bar, and then click the “Get” button:

Figure 1: Downloading and installing Xcode on macOS.

Figure 1: Downloading and installing Xcode on macOS.

Depending on your internet connection and system speed, the download and install process can take anywhere from 30 to 60 minutes. I would suggest installing Xocde in the background while you are getting some other work done or going for a nice long walk.

Accept the Apple developer license

I’m assuming that you’re working with a fresh install of macOS and Xcode. If so, you’ll need to accept the developer license before continuing. Personally, I think this is easier to do via the terminal. Just open up a terminal and execute the following command:

Scroll to the bottom of the license and accept it.

If you have already installed Xcode and previously accepted the Apple developer license, you can skip this step.

Install the Apple Command Line Tools

Now that Xcode is installed and we have accepted the Apple developer license, we can install the Apple Command Line Tools. These tools include packages such as make, GCC, clang, etc. This is a required step, so make you install the Apple Command line tools via:

When executing the above command you’ll see a confirmation window pop up asking you to approve the install:

Figure 2: Installing Apple Command Line Tools on macOS.

Figure 2: Installing Apple Command Line Tools on macOS.

Simply click the “Install” button to continue. The actual install process of Apple Command Line Tools should take less than a 5 minutes.

Step #2: Install Homebrew

We are now ready to install Homebrew, a package manager for macOS. You can think of Homebrew as the macOS equivalent of the Ubuntu/Debian-based apt-get.

Installing Homebrew is dead simple — simply copy and paste the command below the “Install Homebrew” section of the Homebrew website (make sure you copy and paste the entire command into your terminal). I have included the command below as reference:

After Homebrew installs you should make sure the package definitions (i.e., the instructions used to install a given library/package) are up to date by executing the following command:

Now that Homebrew is successfully installed and updated, we need to update our ~/.bash_profile  file so that it searches the Homebrew path for packages/libraries before it searches the system path. Failure to complete this step can lead to confusing errors, import problems, and segfaults when trying to utilize Python and OpenCV, so make sure you update your ~/.bash_profile  file correctly!

The ~/.bash_profile  file may or may not already exist on your system. In either case, open it with your favorite text editor (I’ll be using nano  in this example):

And then insert the following lines at the bottom of the file (if ~/.bash_profile  does not exist the file will be empty — this is okay, just add the following lines to the file):

All this snippet is doing is updating your PATH  variable to look for libraries/binaries along the Homebrew path before it searches the system path.

After updating the ~/.bash_profile  file, save and exit your text editor.

To make sure you are on the right path, I have included a screenshot of my ~/.bash_profile  below so you can compare it to yours:

Figure 3: Updating my .bash_profile file to include Homebrew.

Figure 3: Updating my .bash_profile file to include Homebrew.

Remember, your ~/.bash_profile  may look very different than mine — that’s okay! Just make sure you have included the above Homebrew snippet in your file, followed by successfully saving and editing the editor.

Finally, we need to manually source  the ~/.bash_profile  file to ensure the changes have been reloaded:

The above command only needs to be executed once. Whenever you open up a new terminal, login, etc., your .bash_profile  file will be automatically loaded and sourced for you.

Step #3: Install Python 2.7 and Python 3 using Homebrew

The next step is to install the Homebrew versions of Python 2.7 and Python 3. It is considered bad form to develop against the system Python as your main interpreter. The system version of Python should serve exactly that — system routines.

Instead, you should install your own version of Python that is independent from the system install. Using Homebrew, we can install both Python 2.7 and Python 3 using the following command:

At the time of this writing the current Python versions installed by Homebrew are Python 2.7.12 and Python 3.5.2.

After the Python 2.7 and Python 3 install completes, we need to create some symbolic links:

As a sanity check, let’s confirm that you are using the Homebrew version of Python rather than the system version of Python. You can accomplish this via the which  command:

Inspect the output of which  closely. If you are see /usr/local/bin/python  and /usr/local/bin/python3  for each of the paths then you are correctly using the Homebrew versions of Python. However, if the output is instead /usr/bin/python  and /usr/bin/python3  then you are incorrectly using the system version of Python.

If you find yourself in this situation you should:

  1. Go back to Step #2 and ensure Homebrew installed without error.
  2. Check that brew install python python3  finished successfully.
  3. You have correctly updated your ~/.bash_profile  file and reloaded the changes via source . Your ~/.bash_profile  should look similar to mine in Figure 3 above.

Step #4: Install OpenCV 3 with Python bindings on macOS using Homebrew

Now that we have installed the Homebrew versions of Python 2.7 and Python 3 we are now ready to install OpenCV 3.

Tap the “homebrew/science” repo

The first step is to add the homebrew/science  repository to the set of packages we are tracking. This allows us to access the formulae to install OpenCV. To accomplish this, just use the following command:

Understanding the “brew install” command

To install OpenCV on our macOS system via Homebrew we are going to use the brew install  command. This command accepts the name of a package to install (like Debian/Ubuntu’s apt-get), followed by set of optional arguments.

The base of our command is: brew install opencv3 ; however, we need to add some additional parameters.

The most important set of parameters are listed below:

  • --with-contrib : This ensures that the opencv_contrib repository is installed, giving us access to additional, critical OpenCV features such as SIFT, SURF, etc.
  • --with-python3 : OpenCV 3 + Python 2.7 bindings will be automatically compiled; however, to compile OpenCV 3 + Python 3 bindings we need to explicitly supply the --with-python3  switch.
  • --HEAD : Rather than compiling a tagged OpenCV release (i.e., v3.0, v3.1, etc.) the --HEAD  switch instead clones down the bleeding-edge version of OpenCV from GitHub. Why would we bother doing this? Simple. We need to avoid the QTKit error that plagues macOS Sierra systems with the current tagged OpenCV 3 releases (please see the “Avoiding the QTKit/QTKit.h file not found error” section of this blog post for more information)

You can see the full listing of options/switches by running brew info opencv3 , the output of which I’ve included below:

For those who are curious, the Homebrew formulae (i.e., the actual commands used to install OpenCV 3) can be found here. Use the parameters above and the install script as a reference if you want to add any additional OpenCV 3 features.

We are now ready to install OpenCV 3 with Python bindings on your macOS system via Homebrew. Depending on the dependencies you do or do not already have installed, along with the speed of your system, this compilation could easily take a couple of hours, so you might want to go for a walk once you kick-off the install process.

Installing OpenCV 3 with Python 3 bindings via Homebrew

To start the OpenCV 3 install process, just execute the following command:

This command will install OpenCV 3 on your macOS system with both Python 2.7 and Python 3 bindings via Homebew. We’ll also be compiling the latest, bleeding edge version of OpenCV 3 (to avoid any QTKit errors) along with opencv_contrib  support enabled.

As I mentioned, this install process can take some time so consider going for a long walk while OpenCV installs. However, make sure your computer doesn’t go to sleep/shut down while you are gone! If it does, the install process will break and you’ll have to restart it.

Assuming OpenCV 3 installed without a problem, your terminal output should look similar to mine below:

Figure 5: Compiling and installing OpenCV 3 with Python bindings on macOS with Homebrew.

Figure 4: Compiling and installing OpenCV 3 with Python bindings on macOS with Homebrew.

However, we’re not quite done yet.

You’ll notice a little note at the bottom of the install output:

This means that our Python 2.7 + OpenCV 3 bindings are now installed in /usr/local/opt/opencv3/lib/python2.7/site-packages , which is Homebrew path to the OpenCV compile. We can verify this via the ls  command:

However, we need to get these bindings into /usr/local/lib/python2.7/site-packages/ , which is the site-packages  directory for Python 2.7. We can do this by executing the following command:

The above command creates a .pth  file which tells Homebrew’s Python 2.7 install to look for additional packages in /usr/local/opt/opencv3/lib/python2.7/site-packages — in essence, the .pth  file can be considered a “glorified sym-link”.

At this point you now have OpenCV 3 + Python 2.7 bindings installed!

However, we’re not quite done yet…there is still a few extra steps we need to take for Python 3.

Handling the Python 3 issue

Remember the --with-python3  option we supplied to brew install opencv3 ?

Well, this option did work (although it might not seem like it) — we do have Python 3 + OpenCV 3 bindings installed on our system.

Note: A big thank you to Brandon Hurr for pointing this out. For a long time I thought the --with-python3  switch simply wasn’t working.

However, there’s a bit of a problem. If you check the contents of /usr/local/opt/opencv3/lib/python3.5/site-packages/  you’ll see that our  file has a funny name:

I have no idea why the Python 3 + OpenCV 3 bindings are not named  as they should be, but the same is true across operating systems. You’ll see this same issue on macOS, Ubuntu, and Raspbian.

Luckily, the fix is easy — all you need to do is rename  to :

From there, we can create another .pth  file, this time for the Python 3 + OpenCV 3 install:

At this point you now have both Python 2.7 + OpenCV 3 and Python 3 + OpenCV 3 installed on your macOS system via Homebrew.

Verifying that OpenCV 3 has been installed

Here are the commands I use to validate that OpenCV 3 with Python 2.7 bindings are working on my system:

The screenshot below shows how to import the OpenCV 3 bindings into a Python 3 shell as well:

Figure 6: Confirming that OpenCV 3 with Python 3 bindings have been successfully installed on my macOS system via Homebrew.

Figure 5: Confirming that OpenCV 3 with Python 3 bindings have been successfully installed on my macOS system via Homebrew.

Congratulations, you have installed OpenCV 3 with Python bindings on your macOS system via Homebrew!

But if you’re a longtime reader reader of this blog, you know that I use Python virtual environments extensively — and you should too.

Step #5: Setup your Python virtual environment (optional)

You’ll notice that unlike many of my previous OpenCV 3 install tutorials, Homebrew does not make use of Python virtual environments, a best practice when doing Python development.

While Steps #5-#7 are optional, I highly recommend that you do them to ensure your system is configured in the same way as my previous tutorials. You’ll see many tutorials on the PyImageSearch blog leverage Python virtual environments. While they are indeed optional, you’ll find that in the long run they make your life easier.

Installing virtualenv and virtualenvwrapper

The virtualenv and virtualenvwrapper packages allow us to create separate, independent Python virtual environments for each project we are working on. I’ve mentioned Python virtual environments many times before on this blog so I won’t rehash what’s already been said. Instead, if you are unfamiliar with Python virtual environments, how they work, and why we use them, please refer to the first half of this blog post. I also recommend this excellent tutorial on the blog that takes a more in-depth dive into Python virtual environments.

To install both virtualenv  and virtualenvwrapper , just use pip :

After both packages have successfully installed, you’ll need to update your ~/.bash_profile  file again:

Append the following lines to the file:

After updating, your ~/.bash_profile  should look similar to mine below:

Figure 7: Update your .bash_profile file to include virtualenv/virtualenvwrapper.

Figure 6: Update your .bash_profile file to include virtualenv/virtualenvwrapper.

Once you have confirmed that your ~/.bash_profile  has been created, you need to refresh your shell by using the source  command:

This command only needs to be executed once. Assuming that your ~/.bash_profile  has been updated correctly, it will automatically be loaded and source ‘d each time you open a new shell, login, etc.

Create your Python virtual environment

We are now ready to use the mkvirtualenv  command to create a Python virtual environment named cv  (for “computer vision”).

For Python 2.7 use the following command:

For Python 3 use this command:

The -p  switch controls which Python version is used to create your virtual environment. Please note that each virtual environment needs to be uniquely named so if you want to create two separate virtual environments, one for Python 2.7 and another for Python 3, you’ll want to make sure that each environment has a separate name — both cannot be named “cv”.

The mkvirtualenv  command only needs to be executed once. To access the cv  Python virtual environment after you have already created it, just use the workon  command:

To visually validate you are in the cv  virtual environment, just examine your command line. If you see the text (cv)  preceding the prompt, then you are in the cv  virtual environment:

Figure 8: Make sure you see the "(cv)" text on your prompt, indicating that you are in the cv virtual environment.

Figure 7: Make sure you see the “(cv)” text on your prompt, indicating that you are in the cv virtual environment.

Otherwise, if you do not  see the cv  text, then you are not in the cv  virtual environment:

Figure 9: If you do not see the “(cv)” text on your prompt, then you are not in the cv virtual environment and you need to run the "workon" command to resolve this issue before continuing.

Figure 8: If you do not see the “(cv)” text on your prompt, then you are not in the cv virtual environment and you need to run the “workon” command to resolve this issue before continuing.

Install NumPy

The only Python prerequisite for OpenCV is NumPy, a scientific computing package.

To install NumPy, first make sure you are in the cv  virtual environment and then let pip  handle the actual installation:

Step #6: Sym-link the OpenCV 3 bindings (optional)

We are now ready to sym-link in the  bindings into our cv  virtual environment. I have included the commands for both Python 2.7 and Python 3, although the process is very similar.

For Python 2.7

To sym-link the  bindings into your Python 2.7 virtual environment named cv , use these commands:

For Python 3:

To sym-link the  bindings installed via Homebrew to your Python 3 virtual environment (named cv ), execute these commands:

Repeat as necessary

If you would like to have OpenCV 3 bindings installed for both Python 2.7 and Python 3, then you’ll want to repeat Step #5 and Step #6 for both Python versions. This includes creating a uniquely named Python virtual environment, installing NumPy, and sym-linking in the  bindings.

Step #7: Test your OpenCV 3 install (optional)

To verify that your OpenCV 3 + Python + virtual environment install on macOS is working properly, you should:

  1. Open up a new terminal window.
  2. Execute the workon  command to access the cv  Python virtual environment.
  3. Attempt to import your Python + OpenCV 3 bindings on macOS.

Here are the exact commands I used to validate that my Python virtual environment + OpenCV install are working correctly:

Note that the above output demonstrates how to use OpenCV 3 + Python 2.7 with virtual environments.

I also created an OpenCV 3 + Python 3 virtual environment as well (named py3cv3 ), installed NumPy, and sym-linked the OpenCV 3 bindings. The output of me accessing the py3cv3  virtual environment and importing OpenCV can be seen below:

Figure 10: Utilizing virtual environments with Python 3 + OpenCV 3 on macOS.

Figure 10: Utilizing virtual environments with Python 3 + OpenCV 3 on macOS.

So, what’s next?

Congrats! You now have a brand new, fresh install of OpenCV on your macOS system — and I’m sure you’re just itching to leverage your install to build some awesome computer vision apps…

…but I’m also willing to bet that you’re just getting started learning computer vision and OpenCV, and probably feeling a bit confused and overwhelmed on exactly where to start.

Personally, I’m a big fan of learning by example, so a good first step would be to have some fun and read this blog post on detecting cats in images/videos. This tutorial is meant to be very hands-on and demonstrate how you can (quickly) build a Python + OpenCV application to detect the presence of cats in images.

And if you’re really interested in leveling-up your computer vision skills, you should definitely check out my book, Practical Python and OpenCV + Case Studies. My book not only covers the basics of computer vision and image processing, but also teaches you how to solve real-world computer vision problems including face detection in images and video streamsobject tracking in video, and handwriting recognition.


So, let’s put that fresh install of OpenCV 3 on your macOS system to good use — just click here to learn more about the real-world projects you can solve using Practical Python and OpenCV.


In today’s blog post I demonstrated how to install OpenCV 3 with Python 2.7 and Python 3 bindings on your macOS system via Homebrew.

As you can see, utilizing Homebrew is a great method to avoid the tedious process of manually configuring your CMake command to compile OpenCV via source (my full list of OpenCV install tutorials can be found on this page).

The downside is that you lose much of the control that CMake affords you.

Furthermore, while the Homebrew method certainly requires executing less commands and avoids potentially frustrating configurations, it’s still worth mentioning that you still need to do a bit of work yourself, especially when it comes to the Python 3 bindings.

These steps also compound if you decide to use virtual environments, a best practice when doing Python development.

When it comes to installing OpenCV 3 on your own macOS system I would suggest you:

  1. First try to install OpenCV 3 via source. If you run into considerable trouble and struggle to get OpenCV 3 to compile, use this as an opportunity to teach yourself more about Unix environments. More times than not, OpenCV 3 failing to compile is due to an incorrect CMake parameter that can be correctly determined with a little more knowledge over Unix systems, paths, and libraries.
  2. Use Homebrew as a fallback. I would recommend using the Homebrew method to install OpenCV 3 as your fallback option. You lose a bit of control when installing OpenCV 3 via Homebrew, and worse, if any sym-links break during a major operating system upgrade you’ll struggle to resolve them. Don’t get me wrong: I love Homebrew and think it’s a great tool — but make sure you use it wisely.

Anyway, I hope you enjoyed this blog post! And I hope it helps you get OpenCV 3 installed on their macOS systems.

If you’re interested in learning more about OpenCV, computer vision, and image processing, be sure to enter your email address in the form below to be notified when new blog posts + tutorials are published!

, , , , , ,

47 Responses to Install OpenCV 3 on macOS with Homebrew (the easy way)

  1. Javier de la Rosa December 19, 2016 at 11:25 am #

    Thanks! This is super useful. FWIW, I’ve also been using the opencv-python [1] package. If the architectures, Python versions, and OS versions are supported, it’s a super easy alternative.


    • Adrian Rosebrock December 21, 2016 at 10:34 am #

      Thanks for sharing Javier. I personally have not used the opencv-python package. I’ll look into this more.

      My biggest worry is that the opencv_contrib package isn’t installed by default. I can’t seem to find any mention of this in their GitHub repo?

      • Javier de la Rosa January 2, 2017 at 10:10 pm #

        Yeah, it seems like unfortunately opencv_contrib is not there yet.

        • Adrian Rosebrock January 4, 2017 at 10:49 am #

          That’s good to know — and also sounds like a great pull request for the project 🙂

      • Quang February 15, 2017 at 11:34 pm #

        I tried “opencv-python” before finding out this blog. I still recommend newcomers to install opencv from source, or through homebrew. opencv-python works by providing a precompile and the Python binding. It is fast and extremely convenient, but at the cost of the limitation of that It will not have UI support, since it is not compiled with GTK on *your* computer.

        That may work for some people, when they know exactly what they need, and can work around the limitation. But for newcomers, it is better to grab everything, and save yourself a headache of finding out which is missing.

        • Adrian Rosebrock February 16, 2017 at 9:48 am #

          Thank you for sharing Quang, I didn’t realize that “opencv-python” didn’t have UI support out of the box. I’ll also worth nothing that the “opencv_contrib” module is not included either.

  2. Scott Sideleau December 19, 2016 at 11:33 am #

    I think that it’s easier to install/manage Python3 bindings via ‘pip’, the Python package manager. In this case, the Homebrew dependencies shrink and the additional flags are not required, as specifying the pip3 installer handles the configuration:

    $ brew install python3 opencv3
    $ pip3 install scipy scikit-image matplotlib opencv-python

    This works to get the compare-two-images example from 2014/09/15 running correctly on MacOS 10.11+, for example.

    • Adrian Rosebrock December 21, 2016 at 10:35 am #

      Thanks for sharing Scott. I’m not familiar with the opencv-python package, but I’ll check it out.

      As I mentioned to “Javier” above, I’m concerned with opencv_contrib isn’t installed by default, but I’ll look into this.

  3. Stewart F December 19, 2016 at 1:47 pm #

    n Step 7, I execute python and cv2.__version__ and the version returns 2.4.12. How do I update OpenCV to 3.1.0-dev?

    • Adrian Rosebrock December 21, 2016 at 10:31 am #

      It sounds like you already have OpenCV 2.4.12 installed on your system. How did you previously install OpenCV 2.4? Via Homebrew? Or from source?

      • Visaal A. January 5, 2017 at 6:30 pm #

        I have the same problem, it says the version I installed is 2.4.12. The only thing I did differently was not include the –with-python3 when installing opencv3, but I don’t think that would cause this weird error. I did previously try to install opencv from the source on your other guide but ran into the QTkit error.

        • Adrian Rosebrock January 7, 2017 at 9:34 am #

          Did you have OpenCV installed on your system prior to running the steps in this tutorial?

  4. Steve December 19, 2016 at 8:07 pm #

    Thanks, this was very helpful!

    FWIW: I didn’t have to rename the python 3.5 opencv shared object file in order to successfully import cv2 in python3.5 and I didn’t need to create the .pth file either. Leaving the versioned file name alone the import still succeeded. Unless I need a more involved test?

    • Adrian Rosebrock December 21, 2016 at 10:29 am #

      Thanks for sharing your experience Steve, congrats on getting OpenCV installed!

  5. Guilherme December 23, 2016 at 10:01 am #

    Hi Adrian,

    Have you tried conda? conda is a cross-platform package manager (OSX, Windows, Linux).
    It provides a precompiled opencv package. I never tried this package specifically, but I highly recommend conda when working with compiled libraries.

    After installing conda, it should be just a matter of running:

    conda create -n cvenv opencv
    source activate cvenv

    • Adrian Rosebrock December 23, 2016 at 10:46 am #

      I’ve used conda in the past and found it reasonable. I might still use it today, but the parent company (Continuum) responsible for conda had a very large falling out over a business deal when I was creating the PyImageSearch Gurus course. It’s a great tool, but I’d caution you regarding their OpenCV support — they have removed it in the past and have only recently added it back in over the past couple of years.

      • Guilherme December 26, 2016 at 5:21 pm #

        Ok, I see, but I think it is getting more stable now and they are getting more open. And meanwhile conda-forge was created:
        It is a community-driven initiative to provide conda packages and it has almost all packages provided by Continuum, so it is probably less risky now.

  6. Guilherme December 23, 2016 at 10:09 am #

    Missing reference:
    miniconda is a minimal conda installer

  7. JBeale January 1, 2017 at 11:08 am #

    Anyone try to download recently? Just timing out for me (Jan.1 2017) despite saying all systems running.

    • Adrian Rosebrock January 4, 2017 at 11:07 am #

      The link seems to be working with me.

  8. Tori January 5, 2017 at 2:27 pm #

    This was a breeze! I’ve spent *days upon days* trying to get OpenCV installed before finding this tutorial. Installing this way took me about an hour. Amazing.

    Note that the latest version of Python is now 3.6 so if you’re copy-pasting Adrian’s code, you might need to replace any instances of 3.5 with 3.6.

    • Adrian Rosebrock January 7, 2017 at 9:36 am #

      Congrats on getting OpenCV installed Tori, I know how much of a struggle it was based on our email chats! I hope you can now continue with your research 🙂

  9. Yasu January 18, 2017 at 9:35 am #

    Thank you so much for this tutorial!! Still don’t get why installing OpenCV is so hard compared to other modules… :p
    Articles like this really helps us noobs actually get into the environment.

    I think there were some updates to the installation of OpenCV, and though it may just be an issue with me, I’ll note the changes I had to make, in case it may help someone…

    As Tori has already said, I had to change all the 3.5 for 3.6 (and for “Handling the Python 3 issue” in step 4, became, to accommodate Python 3.6.

    Also, “the little note” that came when I installed OpenCV via Homebrew, told me to run
    $ echo /usr/local/opt/opencv3/lib/python2.7/site-packages >> /usr/local/lib/python2.7/site-packages/opencv3.pth
    $ mkdir -p /Users/yasunori/Library/Python/2.7/lib/python/site-packages
    $ echo ‘import site; site.addsitedir(“/usr/local/lib/python2.7/site-packages”)’ >> /Users/yasunori/Library/Python/2.7/lib/python/site-packages/homebrew.pth

    whereas in this tutorial, only the first line was mentioned.
    This fixes finding the binding for Python 2.7, and for 3.6, I ran the same 3 lines except with 3.6 in the place of 2.7 .

    • Adrian Rosebrock January 18, 2017 at 12:19 pm #

      Thanks for sharing Yasu!

  10. David Skidmore January 21, 2017 at 12:35 pm #

    I could not get the Homebrew version of Python to work. Then I found out about brew doctor.

    • T January 22, 2017 at 10:11 pm #

      Can you explain how you got yours to work? I’m struggling here, installing on a MacBook Pro on OSierra. I keep getting an ImportError. What did you do with brew doctor?

  11. hua January 22, 2017 at 11:00 am #

    Thank you so much for this tutorial also. And a small reminder to the people in China, want to install successfully, need to cross GFW…

  12. Andy February 1, 2017 at 9:03 am #

    Hey Adrian,
    Wonderful installation walkthrough!
    The only problem is that I seem to be getting an error when I’m “solving the python3 issue”
    In my terminal I write: “ls -l /usr/local/opt/opencv3/lib/python3.5/site-packages/” like you said, but my output is saying “no such file or directory”.
    Did I do something wrong? I’m pretty sure that I followed your instructions to the letter and when I did “ls” for python (not python3) it worked…


    • Adrian Rosebrock February 1, 2017 at 12:45 pm #

      Hey Andy — are you sure you’re using Python 3.5? You might be using Python 3.6. Another suggestion is to check whether the file is in the site-packages or the dist-packages.

      • SoPhi February 7, 2017 at 2:09 pm #

        Hey Adrian,
        Thanks for the tutorial! It worked perfectly for Python2.7 and also for Python 3.5.
        But I encountered the same problem as Andy with Python3.6, no *.so file in site-packages directory. It seems coming from 3.6 since with 3.5 it worked. Any idea where it may come from?

        • Adrian Rosebrock February 10, 2017 at 2:16 pm #

          I haven’t tried with Python 3.6, so I’m not sure what the exact problem is. I would check the CMake logs created by Homebrew and then see if the path to the file is listed there.

        • Quang February 15, 2017 at 11:47 pm #

          If you coming from 3.5, you may need to reinstall opencv3 with homebrew, to get it work with python 3.6. I don’t have a concrete proof about how opencv3 is brewed, but it seems to me that it looks for the current version of python and creates the “site-package” for that.

          So, when you upgrade your python from 3.5 to 3.6, I don’t think you can expect to have “/usr/local/opt/opencv3/lib/python3.6” created for you. But you can just link the *.so file in “/usr/local/opt/opent3/lib/python3.5/site-package”.

          For what it worth, I can confirm that brew install opencv3 works for my python 3.6.

    • Sundar February 19, 2017 at 2:02 am #

      I had the same issue “No such file or directory” while performing the “Handling the python3 issue” steps. Like Adrian pointed out below, I saw that I was using python 3.6, so I changed all the paths to python3.6 instead of python3.5:
      $ cd /usr/local/opt/opencv3/lib/python3.6/site-packages/
      $ mv
      $ cd ~

      echo /usr/local/opt/opencv3/lib/python3.6/site-packages >> /usr/local/lib/python3.6/site-packages/opencv3.pth

      I was able to complete the install without any errors.

      • Adrian Rosebrock February 20, 2017 at 7:48 am #

        Thanks for sharing Sunday!

  13. Andres Alberto Ramirez Duque February 23, 2017 at 12:29 pm #

    I have the same issue “No such file or directory” “/usr/local/opt/opencv3/lib/python3.6”, any solution?

    MacOS Sierra Python3.6 Opencv3.2.0

    • Guillermo Duenas Arana February 28, 2017 at 6:42 pm #

      Same here, I installed it –with-python3, but the only directory in “/usr/local/opt/opencv3/lib/” is python2.7.

      • Adrian Rosebrock March 2, 2017 at 6:56 am #

        Just to clarify, did you use Homebrew to install Python 3? And if so, is the python3 command pointing to your Homebrew installation?

        • Claudio Fritsche March 13, 2017 at 6:12 pm #

          I’ve got the same problem, in /usr/local/opt/opencv3/lib there is only python2.7, but no python3.6.
          While following the tutorial I did type “which python3” which gave me “/usr/local/bin/python3” so it seems to point to the Homebrew installation.

          Do you have any idea how to tackle this problem?

          • Zeyu Zhang March 17, 2017 at 12:56 am #

            Same problem happened to me. It bothered my nearly two days, and finally I figured out what is going wrong here, but I cannot solve it.

            Enter following command in your terminal:

            brew gist-logs opencv3

            then you will get I link to github, and find out the whole installation log. In the log, you will find out following lines (may not exactly the same, but similar):

            — Found PythonInterp: /usr/local/bin/python3.4 (found suitable version “3.4.4”, minimum required is “3.4”)
            — Could NOT find PythonLibs: Found unsuitable version “3.6.0”, but required is exact version “3.4.4” (found /usr/local/opt/python3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/config-3.6m-darwin/libpython3.6.dylib)

            Here is the problem, since opencv3 cannot find a suitable PythonLibs, therefore it does not install the opencv3 for python3 (e.g. the python3.6 folder in /usr/local/opt/opencv3/lib).

            How to solve this problem? Here is my “solution”,
            remove python3 and start using python2 :(.

  14. Emmanuel Ovalle February 25, 2017 at 3:39 pm #

    Thanks for the tutorial Adrian. !!

    I followed the steps, and i can run opencv in python, but i cannot run an opencv example with c++ and qt-creator. Any solutions ?


  15. Sam H March 5, 2017 at 6:50 am #

    Without doubt the easiest to follow tutorial i’ve found thus far – thank you (as I mentioned on Twitter)

    However, I still encounter a couple of errors as I go through – including the 3.5/3.6 ones mentioned above.

    So, I went back to the beginning and noticed that $ which python gives me: /usr/bin/python

    But $ which python3 gives me: /usr/local/bin/python3

    I’m fairly sure this is probably the root of the rest of the errors, but I’ve not idea why it would be like this?

    • Adrian Rosebrock March 6, 2017 at 3:44 pm #

      Hey Sam — it’s great to hear that you’re enjoying the tutorial.

      Regarding your error, it sounds like your Python 3 path is updated, but not Python 2.7. Have you updated your .bash_profile file? And have you installed both Python 3 and Python 2.7 via Homebrew?

  16. Claudio Fritsche March 13, 2017 at 6:14 pm #

    I followed the whole tutorial, everything worked out, the only problem I’ve got was the binding to Python 3.6.

    If I open the python shell with IDLE, everything works fine. But if I open Xcode and try to import the cv2 (for c++) it doesn’t work at all, do you have any idea how to make it work there as well?

    Thank you very much for the tutorial! It was very helpful

    • Adrian Rosebrock March 15, 2017 at 9:04 am #

      I’m not sure what you are trying to accomplish here. You’re trying to import Python OpenCV bindings into a C++ program? Or you’re trying to include the OpenCV header files in a C++ program?

    • Zeyu Zhang March 17, 2017 at 12:44 am #

      When you tried to compile your C++ source files, you have to tell the compiler where is the Opencv header file (.h or .hpp), so that the compiler can find the header file.

      You can add follow command when you compile your c++-opencv source file:

      -I/usr/local/Cellar/opencv3/3.2.0/include/opencv -I/usr/local/Cellar/opencv3/3.2.0/include

      Note: “/usr/local/Cellar/opencv3/3.2.0/include/opencv” and “/usr/local/Cellar/opencv3/3.2.0/include” is the path where my opencv header files stored, you may have different paths.

      Then during, the linking process, you need to tell the compiler where is the .lib file, so that them could be linked to your final executable file.

      Add following command to achieve this goal:

      -L/usr/local/Cellar/opencv3/3.2.0/lib -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_cvv -lopencv_dpm -lopencv_fuzzy -lopencv_hdf -lopencv_line_descriptor -lopencv_optflow -lopencv_reg -lopencv_saliency -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_rgbd -lopencv_surface_matching -lopencv_tracking -lopencv_datasets -lopencv_text -lopencv_face -lopencv_plot -lopencv_dnn -lopencv_xfeatures2d -lopencv_shape -lopencv_video -lopencv_ximgproc -lopencv_calib3d -lopencv_features2d -lopencv_flann -lopencv_xobjdetect -lopencv_objdetect -lopencv_ml -lopencv_xphoto -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_photo -lopencv_imgproc -lopencv_core

      Note: “/usr/local/Cellar/opencv3/3.2.0/lib” is the path where my opencv lib files stored, you may have a different path, just change it to yours.

  17. Arthur March 24, 2017 at 6:23 pm #

    Hi Adrian

    this instruction is awsome! I installed opencv3 successfully on my mac sierra system. However now I may want to add ffmpeg surpport to my opencv3.
    I noticed that actually there is a –with–ffmpeg option for brew install. However I followed exactly your commands and did not add this option at that time.
    Now how can I add ffmpeg support without uninstalling and installing opencv3 again ?

    • Adrian Rosebrock March 25, 2017 at 9:17 am #

      If you want to add FFMPEG support you will have to uninstall OpenCV and install it with the FFMPEG flag.

Leave a Reply