In this post, we will provide step by step instructions on how to install Dlib on MacOS and OSX. The installation instructions are slightly different for different versions of the operating system and XCode. Please choose the right one for you.
Install Dlib on MacOS >= 10.11 & XCode >= 8
Step 1: Install OS libraries
brew cask install xquartz
brew install gtk+3 boost
brew install boost-python --with-python3
Step 2: Install Python 2 and/or Python 3
brew install python python3
brew link python
brew link 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.
Step 3: Install Python libraries
We are going to use a Virtual Environment to install Python libraries. It is generally a good practice to separate your project environment and global environment.
# Install virtual environment
pip install virtualenv virtualenvwrapper
echo "# Virtual Environment Wrapper"
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
# 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
# quit virtual environment
deactivate
Step 4: Install Dlib
Step 4.1: Compile C++ library
brew install dlib
Step 4.2: Compile Python module
Now activate “facecourse-py2” or “facecourse-py3” virtual environment based on whether you were following the process for Python2 or Python3.
############ For Python 2 ############
workon facecourse-py2
############ For Python 3 ############
workon facecourse-py3
We will now install Dlib using pip.
pip install dlib
Installation is complete. You can now exit from virtualenv using deactive.
deactivate
Install Dlib on MacOS 10.10 & XCode < 8
Step 1: Install OS libraries
brew cask install xquartz
brew install gtk+3 boost
brew install boost-python --with-python3
Step 2: Install Python 2 and/or Python 3
brew install python python3
brew link python
brew link 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.
Step 3: Install Python libraries
We are going to use a Virtual Environment to install Python libraries. It is generally a good practice to separate your project environment and global environment.
# Install virtual environment
pip install virtualenv virtualenvwrapper
echo "# Virtual Environment Wrapper"
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
# 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
# quit virtual environment
deactivate
Step 4: Compile DLib
Step 4.1: Compile C++ binary
Dlib uses few C++11 features which XCode 7 does not support. We have patched Dlib v19.4 by replacing this feature with Dlib’s internal function. This patched version of Dlib compiles with XCode 7. So if you are using XCode 7, use branch v19.4-thread-local-patch of our Dlib fork in your projects.
Davis King, creator of Dlib, recommends using CMake for using Dlib in your code. But if you want to use Dlib as a library follow these steps:
git clone https://github.com/vaibhawchandel/dlib.git
cd dlib
git checkout v19.4-thread-local-patch
mkdir build
cd build
cmake ..
cmake --build . --config Release
make install
cd ..
Now you can use pkg-config to provide path to Dlib’s include directory and link Dlib library file.
pkg-config --libs --cflags dlib-1
Step 4.2: Compile Python module
Now activate “facecourse-py2” or “facecourse-py3” virtual environment based on whether you were following the process for Python2 or Python3.
############ For Python 2 ############
workon facecourse-py2
############ For Python 3 ############
workon facecourse-py3
Now run the following command to build Python module.
python setup.py install
For consistency we have installed Python and C++ binaries of Dlib using same source code.
Since we installed dlib in facecource-py3/facecourse-py2 virtualenv, dlib was installed only in this virtualenv and not globally. So whenever you want to use dlib, you have to activate the virtualenv using workon command as we used earlier in this post.
Now exit from virtualenv
deactivate