In this post, we will provide step by step instructions for installing OpenCV 3.3.0 (C++ and Python) on MacOS and OSX.
Step 1: Install XCode
Install XCode from App Store.
If XCode available on App Store is not compatible with your OS:
- Find XCode version compatible to your OS from this table https://en.wikipedia.org/w/index.php?title=Xcode#Version_comparison_table
- Go to this webpage https://developer.apple.com/download/more/
- Login if you have apple developer account else create your account and login.
- Search for xcode and download the version compatible to your OS.
- Install XCode.
- After installation open XCode, and accept xcode-build license when it asks.
Step 2: Install Homebrew
Launch a terminal from Launchpad. From this step onward, all commands will be run in the terminal.
ruby -e "$(curl -fsSL
https://raw.githubusercontent.com/Homebrew/install/master/install)"
# update homebrew
brew update
# Add Homebrew path in PATH
echo "# Homebrew" >> ~/.bash_profile
echo "export PATH=/usr/local/bin:$PATH" >> ~/.bash_profile
source ~/.bash_profile
# Homebrew recently moved popular formuale to homebrew-core
# So this is not needed anymore to install OpenCV and you can skip this step.
# tap science repo of home
brew brew tap homebrew/science
Step 3: Install Python 2 and Python 3
# If installing python for the first time using Homebrew,
# else skip the 3 lines below and upgrade.
brew install python python3
brew link python
brew link python3
# NOTE : If you have python already installed using homebrew,
# it might ask you to upgrade.
# Upgrade the python using new homebrew formulae.
brew upgrade python
brew upgrade python3
# Check whether Python using homebrew install correctly
which python2 # it should output /usr/local/bin/python2
which python3 # it should output /usr/local/bin/python3
# Check Python versions
python2 --version
python3 --version
Python version (2.6 or 2.7, 3.5 or 3.6) installed on your machine is required to determine path of Python’s site-packages. It will be used later.
NOTE : Recently Homebrew made some changes in Python formula. Earlier homebrew used to install python2 as /usr/local/bin/python. Now it follows these rules:
- Install python2 at /usr/local/bin/python2
- Install python3 at /usr/local/bin/python3
- python command will point to /usr/bin/python. This is the python distribution which comes with your OS and not installed by Homebrew.
We want to use Python installed by Homebrew because it makes installing/managing packages easier. To run python scripts you should run command python2 and python3 for Python 2 & 3 respectively. If you find this annoying and want to use command python to run python2, add following line to ~/.bash_profile.
# Adding this line to end of .bash_profile will make python command
# point to python2
export PATH="/usr/local/opt/python/libexec/bin:$PATH"
This step is recommended not just for this course but in general to keep the python installation clean.
Step 4: Install Python libraries in a Virtual Environment
We will use Virtual Environment to install Python libraries. It is generally a good practice in order to separate your project environment and global environment.
# Install virtual environment
pip install virtualenv virtualenvwrapper
echo "# Virtual Environment Wrapper"
echo "VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python2" >> ~/.bash_profile
echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bash_profile
source ~/.bash_profile
############ For Python 2 ############
# Create virtual environment
mkvirtualenv facecourse-py2 -p python2
workon facecourse-py2
# Now install python libraries within this virtual environment
pip install numpy scipy matplotlib scikit-image scikit-learn ipython pandas
# Quit virtual environment
deactivate
######################################
############ For Python 3 ############
# Create virtual environment
mkvirtualenv facecourse-py3 -p python3
workon facecourse-py3
# Now install python libraries within this virtual environment
pip install numpy scipy matplotlib scikit-image scikit-learn ipython pandas
# Quit virtual environment
deactivate
######################################
Step 5: Install OpenCV
Step 5.1 : Compile & Install OpenCV
Note: Homebrew recently moved many popular formulae to homebrew-core.
Earlier you can install OpenCV 2 using formula name opencv and OpenCV 3 using formula name opencv3.
They have now renamed opencv3 to opencv and opencv to opencv@2.
Various options such as –with-qt, –with-tbb etc are also removed in the updated formula and CUDA support is also dropped.
You can check these github issues 15853, 6126, 16640, 5996
to read more about it.
Homebrew has also made it compulsory to compile and install Python bindings for both Python 2 and Python 3. So if you don’t have Python 3 installed, Homebrew will install it while installing opencv.
# Compile OpenCV 3 with Python2 and Python3 bindings
brew install opencv
Step 5.2 : Add OpenCV’s site-packages path to global site-packages
When brew is done compiling and installing OpenCV3, we will update path of site-packages directory which contains cv2.so file to Homebrew Python’s site-packages directory. Depending upon the Python version you have (2.6/2.7 or 3.5/3.6) these paths would be different.
############ For Python 2 ############
# This is a single line command.
echo /usr/local/opt/opencv/lib/python2.7/site-packages >>
/usr/local/lib/python2.7/site-packages/opencv3.pth
############ For Python 3 ############
# This is a single line command
echo /usr/local/opt/opencv/lib/python3.6/site-packages >>
/usr/local/lib/python3.6/site-packages/opencv3.pth
Step 6: Make OpenCV3 Python symlink in our virtual environment
Path to OpenCV’s Python library will be different depending upon which Python version you have. Double check the exact path and filename on your machine. Use this command to find out the path on your machine.
find /usr/local/opt/opencv3/lib/ -name cv2*.so
############ For Python 2 ############
cd ~/.virtualenvs/facecourse-py2/lib/python2.7/site-packages/
ln -s /usr/local/opt/opencv3/lib/python2.7/site-packages/cv2.so cv2.so
############ For Python 3 ############
cd ~/.virtualenvs/facecourse-py3/lib/python3.6/site-packages/
ln -s /usr/local/opt/opencv3/lib/python3.6/site-packages/cv2.cpython-36m-darwin.so cv2.so
Step 7: Test OpenCV3
# Activate virtual environment
############ For Python 2 ############
workon facecourse-py2
############ For Python 3 ############
workon facecourse-py3
# open ipython (run this command on terminal)
ipython
# import cv2 and print version (run following commands in ipython)
import cv2
print cv2.__version__
# If OpenCV3 is installed correctly, above command should give output 3.3.0
# Press CTRL+D to exit ipython
Now you can exit from Python virtual environment.
deactivate
Now whenever you are going to run Python scripts which use OpenCV you have to activate the virtual environment we created using workon command.