Title: Install OpenCV 4 on macOS - PyImageSearch

URL Source: https://pyimagesearch.com/2018/08/17/install-opencv-4-on-macos/

Published Time: 2018-08-17T14:00:07+00:00

Markdown Content:
[![Image 1](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20506'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2018/08/install-opencv4-macos-header.jpg)

This tutorial provides step-by-step instructions to install OpenCV 4 (with Python bindings) on your macOS machine.

[OpenCV 4 was released](https://opencv.org/opencv-4-0-0.html) on November 20th, 2018.

I originally wrote this blog post when the alpha version was released, and it has now been _**updated on November 30th, 2018** to support the official release._

OpenCV 4 comes with new features, particularly in the DNN module for deep learning.

**To learn how to install OpenCV 4 on macOS, _just keep reading._**

![Image 2](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20128%20128'%3E%3C/svg%3E)

#### Looking for the source code to this post?

[Jump Right To The Downloads Section](#download-the-code)

In this blog post we’re going to install OpenCV 4 on macOS. OpenCV 4 is packed with new features, many of which are deep-learning focused.

_**Note:** If you landed on the wrong install tutorial (perhaps you want to install OpenCV on Ubuntu or your Raspberry Pi), then you should visit my [OpenCV installation guides](https://pyimagesearch.com/opencv-tutorials-resources-guides/) page. I post links to all my OpenCV install tutorials there._

First, we’ll install Xcode and set up Homebrew.

From there, we’ll establish Python virtual environments.

Then we’ll compile OpenCV 4 from source. Compiling from source allows us to have full control over the compile and build as well as to install the full OpenCV 4 build. I’ll cover alternative methods (pip and Homebrew) in future installation guides (neither of these methods can be covered until OpenCV 4 is officially released).

Finally, we’ll test out our OpenCV 4 install and get our feet wet with a real OpenCV project.

Let’s begin!

### Step #1: Install Xcode

First we need to install [Xcode](https://developer.apple.com/xcode/).

To install Xcode, fire up the **Apple App Store**, find the Xcode app, and install. You’ll need to wait patiently as this figure indicates:

[![Image 3](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20550%20253'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2018/07/install-opencv4-macos-xcode.jpg)

**Figure 1:** Xcode is a dependency for Homebrew and therefore OpenCV 4 on macOS. To install Xcode, launch the App Store, find Xcode, and run the installation.

After Xcode has installed we need to accept a license agreement. Launch a terminal and enter the following command:

$ sudo xcodebuild -license

To accept the license, simply scroll down and accept it.

Once you’ve accepted the license agreement, let’s install Apple Command Line Tools. **This is required****,** so that you’ll have `make` , `gcc` , `clang` , etc. You can install the tools via:

$ sudo xcode-select --install

![Image 4: Figure 2: Installing Apple Command Line Tools on macOS.](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20554%20440'%3E%3C/svg%3E)

**Figure 2:** Installing Apple Command Line Tools on macOS.

Click the _“Install”_ button and wait about 5 minutes for the installation to complete.

### Step #2: Install Homebrew

For this step we’re going to install the Mac community package manager, [Homebrew](https://brew.sh/).

Homebrew runs on Ruby which is a popular programming language. When you’re ready, copy the entire command below to install Homebrew:

$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Homebrew commands are shortened to `brew` .

Let’s update the Homebrew definitions:

$ brew update

And now let’s edit our Mac’s bash profile. This script is run each time you launch a terminal. For simplicity, I suggest the nano text editor. If you’re more comfortable with vim or emacs, then go for it.

Edit your bash profile with nano using the following command:

$ nano ~/.bash\_profile

Once you’re actively editing the file, append the following lines to the end to update your `PATH` :

\# Homebrew
export PATH=/usr/local/bin:$PATH

From there, save the profile. If you’re using nano, you’ll see the shortcut keys at the bottom of the window which demonstrate how to save (write) and exit.

Once you’re back in bash, source your bash profile:

$ source ~/.bash\_profile

### Step #3: Install OpenCV prerequisites using Homebrew

In this section we’ll ensure that Python 3.6 is installed. We’ll also install prerequisites for building OpenCV from source.

#### Install Python 3.6

**It is _extremely_ important to use Python 3.6.** By default High Sierra and Mojave are coming with Python 3.7 now. It sounds good, _**but**_ Python 3.7 is unsupported by Keras/TensorFlow (both are used often on this blog) and thus are not a good choice for OpenCV either.

These commands will install Python 3.6.5\_1:

$ brew install https://raw.githubusercontent.com/Homebrew/homebrew-core/f2a764ef944b1080be64bd88dca9a1d80130c558/Formula/python.rb
$ brew switch python 3.6.5\_1

Be sure to copy the entire command + URL.

Let’s verify:

$ python3
Python 3.6.5 (default, Jun 17 2018, 12:13:06) 
\[GCC 4.2.1 Compatible Apple LLVM 9.1.0 (clang-902.0.39.2)\] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

Great! I can see that we have Python 3.6.5 installed now.

Let’s verify one more thing:

$ which python3
/usr/local/bin/python3

If you see `/usr/local/bin/python3` you are using the _Homebrew_ Python (which is what we desire). If you see `/usr/bin/python3` then you are using the _system_ Python and you likely need to fix your bash profile and/or source it.

**Take the time now to verify you are using the _Homebrew_ version of Python and _not_ the system version.**

#### Install other prerequisites

OpenCV requires a few prerequisites to be installed before we compile it. These packages are related to either (1) tools used to build and compile, (2) libraries used for image I/O operations (i.e., loading various image file formats from disk such as JPEG, PNG, TIFF, etc.) or (3) optimization libraries.

To install these prerequisites for OpenCV on macOS execute the following commands:

$ brew install cmake pkg-config
$ brew install jpeg libpng libtiff openexr
$ brew install eigen tbb

A tool you’ll learn to love is `wget` . All `wget` does is download files from the command line. We can use Homebrew to install wget:

$ brew install wget

### Step #4: Install Python dependencies for OpenCV 4

We’re going to install the Python dependencies for OpenCV 4 in this step.

Taking advantage of the `wget` tool that we just installed, let’s download and install pip (a Python package manager):

$ wget https://bootstrap.pypa.io/get-pip.py
$ sudo python3 get-pip.py

Now that we have pip installed, we can install [virtualenv](https://virtualenv.pypa.io/en/stable/) and [virtualenvwrapper](https://virtualenvwrapper.readthedocs.io/en/latest/), two tools for managing virtual environments. Python virtual environments are a best practice for Python development and I _**strongly urge**_ you to take full advantage of them.

Each week, I receive countless emails and blog comments about problems that can be avoided with use of virtual environments. Virtual environments are different than virtual machines despite the similar name.

To learn about virtual environments, [I suggest you give this article a read](https://realpython.com/python-virtual-environments-a-primer/).

Let’s install `virtualenv` and `virtualenvwrapper` , then do a bit of cleanup:

$ sudo pip3 install virtualenv virtualenvwrapper
$ sudo rm -rf ~/get-pip.py ~/.cache/pip

From there, we need to edit our bash profile again so that these two tools work properly.

Fire up nano (or your preferred text editor):

$ nano ~/.bash\_profile

And then add these lines to the end of the file:

\# virtualenv and virtualenvwrapper
export WORKON\_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER\_PYTHON=/usr/local/bin/python3
source /usr/local/bin/virtualenvwrapper.sh

[![Image 5](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20377'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2018/07/install-opencv4-macos-bashprofile.jpg)

**Figure 3:** Using the `nano` text editor to edit the `~/.bash_profile` prior to installing OpenCV 4 for macOS.

Pro-tip: You can use bash commands to append to the file without opening an editor:

$ echo -e "\\n# virtualenv and virtualenvwrapper" >> ~/.bash\_profile
$ echo "export WORKON\_HOME=$HOME/.virtualenvs" >> ~/.bash\_profile
$ echo "export VIRTUALENVWRAPPER\_PYTHON=/usr/local/bin/python3" >> ~/.bash\_profile
$ echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bash\_profile

Then source the file:

$ source ~/.bash\_profile

You’ll see several lines printed in your terminal indicating that virtualenvwrapper is setup.

The `virtualenvwrapper` tool provides us a number of terminal commands:

*   `mkvirtualenv <env_name> <options>` : Used to “make a virtual environment”
*   `rmvirtualenv <env_name>` : Destroys a virtual environment
*   `workon <env_name>` : Activates a virtual environment
*   `deactivate` : Deactivates the current virtual environment
*   _You’ll want to [read the docs](https://virtualenvwrapper.readthedocs.io/en/latest/command_ref.html) for more information._

Let’s take advantage of the first command to create a Python virtual environment for OpenCV:

$ mkvirtualenv cv -p python3
Running virtualenv with interpreter /usr/local/bin/python3
Using base prefix '/usr/local/Cellar/python/3.6.5\_1/Frameworks/Python.framework/Versions/3.6'
New python executable in /Users/admin/.virtualenvs/cv/bin/python3.6
Also creating executable in /Users/admin/.virtualenvs/cv/bin/python
Installing setuptools, pip, wheel...
done.
virtualenvwrapper.user\_scripts creating /Users/admin/.virtualenvs/cv/bin/predeactivate
virtualenvwrapper.user\_scripts creating /Users/admin/.virtualenvs/cv/bin/postdeactivate
virtualenvwrapper.user\_scripts creating /Users/admin/.virtualenvs/cv/bin/preactivate
virtualenvwrapper.user\_scripts creating /Users/admin/.virtualenvs/cv/bin/postactivate
virtualenvwrapper.user\_scripts creating /Users/admin/.virtualenvs/cv/bin/get\_env\_details

Notice that `cv` is the name of our environment and that I am creating a Python 3 (controlled by the `-p python3` switch) environment.

_**Important:** Take note in the output that Python 3.6 is being utilized for the environment as well (highlighted)._

You may name your environment differently if you’d like. I actually like to name my environments like so:

*   `py3cv4`
*   `py3cv3`
*   `py2cv2`
*   etc.

Here my `py3cv4` virtual environment can be used for Python 3 + OpenCV 4. My `py3cv3` virtual environment is used for Python 3 and OpenCV 3. And my `py2cv2` environment can be used to test legacy Python 2.7 + OpenCV 2.4 code. These virtual environment names are easy to remember and allow me to switch between OpenCV + Python versions (nearly) seamlessly.

Next, let’s install NumPy while we’re inside the environment.

Chances are, the environment is already active (indicated by `(cv)` preceding your bash prompt). Just in case, let’s `workon` (activate) the environment:

$ workon cv

[![Image 6](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20445'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2018/08/install-opencv4-macos-cv.jpg)

**Figure 4:** We are “in” the `cv` virtual environment as is denoted by `(cv)` before the bash prompt. This is necessary prior to installing packages and compiling OpenCV 4.

Each time you wish to use the environment or install packages into it you should use the `workon` command.

Now that our environment is activated, we can install NumPy:

$ pip install numpy

### Step #5: Compile OpenCV 4 for macOS

Compiling from source gives you the most control over your build as opposed to package managers such as pip, Homebrew, and Anaconda.

Package managers are convenient for sure and I’ll cover them in a future installation tutorial, I just want to give you fair warning — while they appear easy on the surface, you won’t be able to get the latest revision of OpenCV and in some cases it won’t work well with virtual environments. You also might be missing features.

I still compile from source all the time and you should definitely learn how if you’re serious about working with OpenCV.

#### Download OpenCV 4

Let’s download OpenCV.

First, navigate to our home folder and download both [opencv](https://github.com/opencv/opencv) and [opencv\_contrib](https://github.com/opencv/opencv_contrib). The contrib repo contains extra modules and functions which we frequently use here on the PyImageSearch blog. **You should be installing the OpenCV library with the additional contrib modules as well.**

When you’re ready, just follow along to download both the `opencv` and `opencv_contrib` code:

$ cd ~
$ wget -O opencv.zip https://github.com/opencv/opencv/archive/4.0.0.zip
$ wget -O opencv\_contrib.zip https://github.com/opencv/opencv\_contrib/archive/4.0.0.zip

_**Update 2018-11-30:**_ _[OpenCV 4.0.0 is officially released](https://opencv.org/opencv-4-0-0.html) and I have updated the download URLs above._

From there, let’s unzip the archives:

$ unzip opencv.zip
$ unzip opencv\_contrib.zip

I also like to rename the directories:

$ mv opencv-4.0.0 opencv
$ mv opencv\_contrib-4.0.0 opencv\_contrib

If you skip renaming the directories, don’t forget to update the CMake paths next.

Now that `opencv` and `opencv_contrib` are downloaded and ready to go, let’s set up our environment.

#### Compile OpenCV4 from source

Now that `opencv` and `opencv_contrib` are downloaded and ready to go, let’s employ CMake to setup our compile and Make to perform the compilation.

Navigate back to your OpenCV repo and create + enter a `build` directory:

$ cd ~/opencv
$ mkdir build
$ cd build

Now we’re ready for CMake. **Be sure to use the `workon`** **command before executing the `cmake` command as shown:**

$ workon cv
$ cmake -D CMAKE\_BUILD\_TYPE=RELEASE \\
    -D CMAKE\_INSTALL\_PREFIX=/usr/local \\
    -D OPENCV\_EXTRA\_MODULES\_PATH=~/opencv\_contrib/modules \\
    -D PYTHON3\_LIBRARY=\`python -c 'import subprocess ; import sys ; s = subprocess.check\_output("python-config --configdir", shell=True).decode("utf-8").strip() ; (M, m) = sys.version\_info\[:2\] ; print("{}/libpython{}.{}.dylib".format(s, M, m))'\` \\
    -D PYTHON3\_INCLUDE\_DIR=\`python -c 'import distutils.sysconfig as s; print(s.get\_python\_inc())'\` \\
    -D PYTHON3\_EXECUTABLE=$VIRTUAL\_ENV/bin/python \\
    -D BUILD\_opencv\_python2=OFF \\
    -D BUILD\_opencv\_python3=ON \\
    -D INSTALL\_PYTHON\_EXAMPLES=ON \\
    -D INSTALL\_C\_EXAMPLES=OFF \\
    -D OPENCV\_ENABLE\_NONFREE=ON \\
    -D BUILD\_EXAMPLES=ON ..

_**Update 2018-11-30:** I added a CMake compile flag to enable nonfree algorithms (`OPENCV_ENABLE_NONFREE=ON` ). This is required for OpenCV 4 if you want access to patented algorithms for educational purposes._

_**Note:** For the above CMake command, I spent considerable time creating, testing, and refactoring it. **It is effectively self-configuring with no work required on your part.** I’m confident that it will save you time and frustration if you use it exactly as it appears._

Once CMake has finished, scroll up (ever so slightly) until you see the following information in your terminal:

[![Image 7](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20427'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2018/08/install-opencv4-macos-cmake.jpg)

**Figure 5:** Inspecting CMake output while installing OpenCV 4 on macOS is important. Be sure that your `cv` virtual environment is being used for Python and NumPy.

Your output should look very similar to mine. **What you’re looking for is to ensure that your Python 3 interpreter and NumPy in the virtual environment will both be used.** If you don’t see this, then you likely executed CMake without being “in” the `cv` virtual environment (or whatever you named your Python virtual environment). If that’s the case, no worries — just delete the `build` directory, activate the virtual environment with the `workon` command, and re-run CMake.

Next, scroll up a little further to check that your output matches mine where I’ve highlighted:

[![Image 8](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20414'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2018/08/install-opencv4-macos-nonfree.jpg)

Figure 6: Verify that the “Non-free algorithms” will be installed in OpenCV 4.

Provided that your CMake output is good to go you can kick off the compilation via:

$ make -j4

_**Note:**_ _The -j4 argument is optional and will instruct `make` to take advantage of 4 CPU cores. You can adjust the numerical value or leave the argument off completely. It doesn’t happen often, but sometimes race conditions may prevent the compile from finishing — at which point you should execute `make` without the flag._

Take a break while the compilation continues, but don’t forget to come back and create symbolic links.

When `make` is finished, you should see this:

[![Image 9](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20383'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2018/08/install-opencv4-macos-make.jpg)

**Figure 6:** Compiling OpenCV 4 on macOS has reached 100%. We’re now ready to link OpenCV 4 into our `cv` environment.

If you’ve reached 100%, then there is one additional command to install OpenCV 4 prior to **Step #6**:

$ sudo make install

### Step #6: Sym-link OpenCV 4 on macOS to your virtual environment `site-packages`

Now let’s create what is called a “symbolic link”. We need a link _from_ our `cv` virtual environment `site-packages` _to_ our system `site-packages` where OpenCV was installed. Effectively, OpenCV will then be “linked” into the virtual environment allowing you to import it into Python scripts as well as the command line interpreter.

Before we make a symbolic link to link OpenCV 4 into our Python virtual environment, let’s determine our Python version:

$ workon cv
$ python --version
Python 3.6

Using the Python version, we can easily navigate to the correct `site-packages` directory next (although I do recommend tab-completion in the terminal).

_**Update 2018-12-20:** The following paths have been updated. Previous versions of OpenCV installed the bindings in a different location (`/usr/local/lib/python3.6/site-packages` ), so be sure to take a look at the paths below carefully._

At this point, your Python 3 bindings for OpenCV should reside in the following folder:

$ ls /usr/local/python/cv2/python-3.6
cv2.cpython-36m-darwin.so

Let’s rename them to simply `cv2.so` :

$ cd /usr/local/python/cv2/python-3.6
$ sudo mv cv2.cpython-36m-darwin.so cv2.so

_**Pro-tip:**_ _If you are installing OpenCV 3 and OpenCV 4 alongside each other, instead of renaming the file to `cv2.so` , you might consider naming it `cv2.opencv4.0.0.so` and then in the next sub-step sym-link appropriately from that file to `cv2.so` as well._

Our last sub-step is to sym-link our OpenCV `cv2.so` bindings into our `cv` virtual environment:

$ cd ~/.virtualenvs/cv/lib/python3.6/site-packages/
$ ln -s /usr/local/python/cv2/python-3.6/cv2.so cv2.so

Important considerations:

1.  Take the time to understand how sym-linking works — you might [read this article](https://www.howtogeek.com/297721/how-to-create-and-use-symbolic-links-aka-symlinks-on-a-mac/).
2.  Be sure your paths are correct. **I recommend tab-completion rather than copy/paste.**

_**I can’t stress this point enough:**_ The `cv` Python virtual environment is _**entirely independent and sequestered**_ from the default Python version on your system. Any Python packages in the _global directories_ _will not_ be available to the `cv` virtual environment. Similarly, any Python packages installed in `site-packages` of `cv` _will not_ be available to the global install of Python. Keep this in mind when you’re working in your Python virtual environment and it will help avoid a lot of confusion and headaches.

#### Install `imutils`

I also recommend that you install my very own [imutils](https://pyimagesearch.com/2015/02/02/just-open-sourced-personal-imutils-package-series-opencv-convenience-functions/) package if you’ll be frequently visiting my blog as we use it quite often:

$ workon cv
$ pip install imutils

### Step #7: Test your macOS + OpenCV 3 install

It is always important to test your OpenCV install to make sure the proper links have been made.

I like to fire up a Python shell in the virtual environment and check that it imports without error and that the version matches my intention:

$ workon cv
$ python
>>> import cv2
>>> cv2.\_\_version\_\_
'4.0.0'
>>> exit()

[![Image 10](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20437'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2018/08/install-opencv4-macos-verified.jpg)

**Figure 7:** To test that OpenCV 4 is installed on macOS, fire up Python in your virtual environment and execute `cv2.__version__` after importing `cv2`.

Provided you see no errors and OpenCV 4 is installed, you’re good to go!

### Run your first example with your fresh install of OpenCV 4!

If you’re breaking into the field of computer vision with OpenCV, my guess is that you want to run something cool with OpenCV.

Let’s start by building a simple “document scanner” with OpenCV.

I’m not going to review all the code here — you should [check out the original post](https://pyimagesearch.com/2014/09/01/build-kick-ass-mobile-document-scanner-just-5-minutes/) if you’re interested or when you have more time.

To get started, simply scroll to the _**“Downloads”**_ and grab the source code + images.

From there just enter the following commands to prepare:

$ workon cv
$ unzip document-scanner.zip
$ cd document-scanner
$ tree
.
├── images
│   ├── page.jpg
│   └── receipt.jpg
├── pyimagesearch
│   ├── \_\_init\_\_.py
│   └── transform.py
└── scan.py

2 directories, 5 files

As you can see, there are two sample images, a `pyimagesearch` module (which needs to be downloaded via the _“Downloads”_ section — it is _not_ pip-installable), and the scan script.

We’ll need one additional library added to our `cv` environment: Scikit-image. To install it, simply use pip. While you’re at it, if you didn’t install imutils earlier, go ahead and install it as well:

$ pip install scikit-image imutils

From there you can execute the following command to give OpenCV 4 a workout:

$ python scan.py --image images/receipt.jpg
STEP 1: Edge Detection
STEP 2: Find contours of paper
STEP 3: Apply perspective transform

At the end of each step, you’ll need to press a key — with the window in focus on your desktop.

Sometimes OpenCV’s hides windows under one another, so be sure to drag the OpenCV image windows around to see what is available.

Here’s a screenshot of the last step of document scanning:

[![Image 11](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20437'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2014/08/receipt-scanned.jpg)

**Figure 8:** Applying step 3 of our document scanner, perspective transform. The original image is on the left and the scanned image on the right.

That was fun!

Learn how the code works by reading the [original blog post](https://pyimagesearch.com/2014/09/01/build-kick-ass-mobile-document-scanner-just-5-minutes/).

If you’d like to build additional projects with OpenCV 4, just continue to follow my blog!

### Troubleshooting and FAQ

In this section, I address some common questions and problems that readers encounter when installing OpenCV 4 on macOS.

_**Q.**_ Can I run OpenCV 4 with Python 2.7 on macOS?

_**A.**_ I suggest you stick with Python 3, but if you’re working on legacy projects, I understand that you might want to use Python 2.7. Here is what you need to do:

*   Install Python 2.7 via Homebrew using `brew install python2`
*   You should pip with 2.7 in **Step #3**: `sudo python get-pip.py` and `sudo pip install virtualenv virtualenvwrapper`
*   In **Step #3**, simply create a Python 2.7 environment: `mkvirtualenv cv -p python2.7`
*   Verify CMake output is using your Python 2.7 virtual environment interpreter in **Step #4,** **Figure 5.**

_**Q.**_ Do I need to run brew install Python? Python seems to already be installed on my Mac!

_**A.**_ Yes. Python is installed, but you’ll want Brew’s updated Python and plus Brew will put Python in `/usr/local/bin` which is separate from your system Python.

_**Q.**_ When I execute `mkvirtualenv` or `workon` , I encounter a “command not found error”.

_**A.**_ There a number of reasons why you would be seeing this error message, all of come from to **Step #3:**

1.  First, make sure you have installed `virtualenv` and `virtualenvwrapper` properly using the `pip` package manager. Verify by running `pip freeze` , and ensure that you see both `virtualenv` and `virtualenvwrapper` in the list of installed packages.
2.  Your `~/.bash_profile` file may have mistakes. View the contents of your `~/.bash_profile` file to see the proper `export` and `source` commands are present (check **Step #3** for the commands that should be appended to `~/.bash_profile` ).
3.  You may have forgotten to `source` your `~/.bash_profile` . Make sure you run `source ~/.bash_profile` after editing it to ensure you have access to the `mkvirtualenv` and `workon` commands.

_**Q.**_ When I open a new terminal, logout, or reboot my macOS system, I cannot execute the `mkvirtualenv` or `workon` commands.

_**A.**_ See **#2** from the previous question.

_**Q.**_ I reached the end, however when I run Python in my terminal and try to `import cv2` , I encounter an error. What next?

_**A.**_ Unfortunately this situation is hard to diagnose without being on your system. Many things could have gone wrong. The most likely point of failure is the symbolic linking step. I suggest you do the following to check the symbolic link:

$ cd ~/.virtualenvs/cv/lib/python3.6/site-packages
$ ls -al cv2\*
lrwxr-xr-x  1 admin  \_developer  21 Nov 30 11:31 cv2.so -> /usr/local/python/cv2/python-3.6/cv2.so

What you’re looking for is the `->`  arrow and that it points to a valid path on your system. If the path is invalid, then you have a sym-link issue that you need to fix. You can correct it by deleting the symlink and trying **Step #5** again.

_**Q.**_ Why do I get a “NONFREE” error when trying to use a patented algorithm such as SURF?

_**A.**_ With OpenCV 4 there is a new requirement for compiling. Please refer to **Step #5** where this is addressed for OpenCV 4 with a CMake argument.

**_Q._** Can I install OpenCV with Brew instead of compiling from source?

_**A.**_ Beware! The answer is yes, but it if you plan on using virtual environments you’ll still need symbolic links. I’ll do another writeup on the Homebrew method in the future.

_**Q.**_ Can I install OpenCV 4 with pip into my virtual environment?

_**A.**_ Currently you can’t install OpenCV 4 with pip. But you can install OpenCV 3.4.3. See how to _[pip install opencv](https://pyimagesearch.com/2018/09/19/pip-install-opencv/)._

### What's next? We recommend [PyImageSearch University](https://pyimagesearch.com/pyimagesearch-university/?utm_source=blogPost&utm_medium=bottomBanner&utm_campaign=What%27s%20next%3F%20I%20recommend).

**Course information:**  
84 total classes • 114+ hours of on-demand code walkthrough videos • Last updated: February 2024  
★★★★★ 4.84 (128 Ratings) • 16,000+ Students Enrolled

**I strongly believe that if you had the right teacher you could _master_ computer vision and deep learning.**

Do you think learning computer vision and deep learning has to be time-consuming, overwhelming, and complicated? Or has to involve complex mathematics and equations? Or requires a degree in computer science?

That’s _not_ the case.

All you need to master computer vision and deep learning is for someone to explain things to you in _simple, intuitive_ terms. _And that’s exactly what I do_. My mission is to change education and how complex Artificial Intelligence topics are taught.

If you're serious about learning computer vision, your next stop should be PyImageSearch University, the most comprehensive computer vision, deep learning, and OpenCV course online today. Here you’ll learn how to _successfully_ and _confidently_ apply computer vision to your work, research, and projects. Join me in computer vision mastery.

**Inside PyImageSearch University you'll find:**

*   ✓ **84 courses** on essential computer vision, deep learning, and OpenCV topics
*   ✓ **84 Certificates** of Completion
*   ✓ **114+ hours** of on-demand video
*   ✓ **Brand new courses released _regularly_**, ensuring you can keep up with state-of-the-art techniques
*   ✓ **Pre-configured Jupyter Notebooks in Google Colab**
*   ✓ Run all code examples in your web browser — works on Windows, macOS, and Linux (no dev environment configuration required!)
*   ✓ Access to **centralized code repos for _all_ 536+ tutorials** on PyImageSearch
*   ✓ **Easy one-click downloads** for code, datasets, pre-trained models, etc.
*   ✓ **Access** on mobile, laptop, desktop, etc.

[Click here to join PyImageSearch University](https://pyimagesearch.com/pyimagesearch-university/?utm_source=blogPost&utm_medium=bottomBanner&utm_campaign=What%27s%20next%3F%20I%20recommend)

Summary
-------

Today we installed OpenCV4 with Python bindings on your macOS machine.

I’ll be back with future installation tutorials for OpenCV 4 (including one for the Raspberry Pi). If you’re looking for my other, already published OpenCV install tutorials, [be sure to refer to this page.](https://pyimagesearch.com/opencv-tutorials-resources-guides/)

Stay tuned!

**To download the source code for today’s tutorial, _just enter your email in the form below._**

![Image 12](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20410%20567'%3E%3C/svg%3E)

#### Download the Source Code and FREE 17-page Resource Guide

Enter your email address below to get a .zip of the code and a **FREE 17-page Resource Guide on Computer Vision, OpenCV, and Deep Learning.** Inside you'll find my hand-picked tutorials, books, courses, and libraries to help you master CV and DL!