Installing OpenCV in MacOS High Sierra for python 3

nuwan prabhath
Dec 5, 2017 · 3 min read

When try to install OpenCV in Mac OS High Sierra by following tutorials in the internet, most probably you’ll end up with having errors related to permission denied while installation of virtualenv and virtualenvwrapper. For example following is the most common one that most of people encounter.

IOError: [Errno 13] Permission denied: '/Library/Python/2.7/site-packages/'

Most probably installing any python library using pip might give similar kind of errors. First lets try to understand why this kind of errors occur in Mac OS but not on Linux. In Mac OS, there is new security layer called System Integrity Protection. This will protect changing system files even from the root user. Therefore for the permission relates issues, though you use sudo with pip it might not work. The other thing is python is already installed in Mac OS and above error shows the system installed python location. Most of the time people try to install Open CV and python using brew and though they set path to use python installed by brew, above error can occur. This might occur due to previously installed packages with system python installation and brew try to attach to those.

Following steps will help to resolve those issues.

  1. First of all install Xcode. This is to install C compiler to mac and easiest way to install Xcode is via app store and you’ll able to find ample amount of tutorials how to do this in internet. After that install Xcode command line tools using following command.
sudo xcode-select --install

2. Install homebrew using any tutorial and add following line to at the end of your ~/.bash_profile or ~/.bashrc file. I prefer .bashrc since I use custom terminal and bash_profile won’t always run.

export PATH=/usr/local/bin:$PATH

And then use source command to load the changes in corresponding file.

source ~/.bashrc

3. Then install python using brew using following command and link them.

brew install python python3
brew link python
brew link python3

Note that we have installed both python2 and python3 as well in here it is better since using system python can cause problems in future.

4. Next lets install pip3 for python3. Unlike with normal brew python 2.7 installation, it won’t automatically install with python3. You have to run following command to do that.

brew postinstall python3

5. Then we have to install virtualenv and virtualenvwrapper. This is to create virtual environment for each project therefor dependenies of multiple projects do not collied with each other. This comes handy when two projects needs same python package but with two different versions.

pip3 install virtualenv virtualenvwrapper

After that add following lines to the ~/.bashrc or ~/.bash_profile

export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3
export WORKON_HOME=$HOME/.virtualenvsexport PROJECT_HOME=$HOME/Develsource /usr/local/bin/

6. Install OpenCV using following command. This will install OpenCV globally and later when we create virtual enviornments and we’ll refer this within our virtual environments since we do not need to install it in every environment we create.

brew install opencv

7. Add OpenCV site path to global site path. Please note that python virson number can different in your installation and change accordingly if required.

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

8. Now lets create a virtual environment for OpenCV project and point our previous OpenCV installation so that it can reuse it. To create a virtual environment with the name of cv-py3 run first command. You can give any name you prefer. Second command will switch to that env and third one will exit from env.

mkvirtualenv cv-py3 -p python3
workon cv-py3

After you run the second command it will switch to that environment and what ever python packages you install will be within the envionment. This virtual environment is located in ~/.virtualenvs. You’ll see separate forlder for each environemnt. Now you can install any python library within this environment see following example.

pip install numpy scipy scikit-image matplotlib scikit-learn

Then lets link previously installed OpenCV library to this virtual invironment so we can use it without reinstalling.

cd ~/.virtualenvs/cv-py3/lib/python3.6/site-packages/ln -s /usr/local/opt/opencv@3/lib/python3.6/site-packages/

9. Now lets try running OpenCV

First command will switch to previously created env second will run the python3 and third one will import open cv. If third one run without any errors, installation is success. And you can print the version of it using the forth command.

workon cv-py3
import cv2

You can exit from env using deactivate command.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade