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
FYI, I ran into issues while installing dlib for python3.
I had to install boost and boost-python using:
brew install boost
brew install boost-python –with-python3
Source: http://www.pyimagesearch.com/2017/03/27/how-to-install-dlib/
Thanks, Sandeep. We had tested these instructions on MacOS Siera from scratch. Will test again and update instructions.
Tested. We had missed that instruction. We have updated the blog also. Thanks for pointing it out. .
I am getting following error in step 4
Durgesh-MacBook:site-packages durgeshtrivedi$ mkvirtualenv facecourse-py3 -p python3
Running virtualenv with interpreter /usr/local/bin/python3
Using base prefix ‘/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6’
New python executable in facecourse-py3/bin/python3.6
Not overwriting existing python script facecourse-py3/bin/python (you must use facecourse-py3/bin/python3.6)
Installing setuptools, pip…
Complete output from command /Users/durgeshtrived…se-py3/bin/python3.6 -c “import sys, pip; sys…d”] + sys.argv[1:]))” setuptools pip:
Traceback (most recent call last):
File “”, line 1, in
File “”, line 961, in _find_and_load
File “”, line 950, in _find_and_load_unlocked
File “”, line 646, in _load_unlocked
File “”, line 616, in _load_backward_compatible
File “/Library/Python/2.7/site-packages/virtualenv_support/pip-1.5.1-py2.py3-none-any.whl/pip/__init__.py”, line 9, in
File “”, line 961, in _find_and_load
File “”, line 950, in _find_and_load_unlocked
File “”, line 646, in _load_unlocked
File “”, line 616, in _load_backward_compatible
File “/Library/Python/2.7/site-packages/virtualenv_support/pip-1.5.1-py2.py3-none-any.whl/pip/log.py”, line 9, in
File “”, line 961, in _find_and_load
File “”, line 950, in _find_and_load_unlocked
File “”, line 646, in _load_unlocked
File “”, line 616, in _load_backward_compatible
File “/Library/Python/2.7/site-packages/virtualenv_support/pip-1.5.1-py2.py3-none-any.whl/pip/_vendor/pkg_resources.py”, line 1422, in
AttributeError: module ‘importlib._bootstrap’ has no attribute ‘SourceFileLoader’
—————————————-
…Installing setuptools, pip…done.
Traceback (most recent call last):
File “/Library/Python/2.7/site-packages/virtualenv.py”, line 2338, in
main()
File “/Library/Python/2.7/site-packages/virtualenv.py”, line 824, in main
symlink=options.symlink)
File “/Library/Python/2.7/site-packages/virtualenv.py”, line 992, in create_environment
install_wheel(to_install, py_executable, search_dirs)
File “/Library/Python/2.7/site-packages/virtualenv.py”, line 960, in install_wheel
‘PIP_NO_INDEX’: ‘1’
File “/Library/Python/2.7/site-packages/virtualenv.py”, line 902, in call_subprocess
% (cmd_desc, proc.returncode))
OSError: Command /Users/durgeshtrived…se-py3/bin/python3.6 -c “import sys, pip; sys…d”] + sys.argv[1:]))” setuptools pip failed with error code 1
I am not even able to figure out why it is pointing to python2.7 in place of 3.6 which is also installed
and even pip version in 9.0.1
Hi,
Thank you for these informations.
I work with OS X 10.11 and Xcode 7.3 and i continue to have this C++11 error in my build even if i work in you patched branch. Anyway it succed to Compile C++ binary but when i run the python install it gives me 2 errors at 89% with the CMakeFiles/dlib_.dir/src/other.cpp.o file (reference to ‘list’ is ambiguous).
Any chance you see what happen ?
I got the same error. Were you able to resolve it?
Just for anyone else who is struggling with this. I got installed dlib by upgrading xcode to 9.x latest version. After this things went smooth.
Thanks, Manish. This will really help other people.
I upgraded xcode to the 9.2. but, it does not help.
/Users/tdong/git/dlib/tools/python/src/other.cpp:72:11: error: reference to ‘list’ is ambiguous
const list& assignment
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/list:803:28: note: candidate found by name lookup is ‘std::__1::list’
class _LIBCPP_TEMPLATE_VIS list
^
/usr/local/include/boost/python/list.hpp:57:7: note: candidate found by name lookup is ‘boost::python::list’
class list : public detail::list_base
^
2 errors generated.
make[2]: *** [CMakeFiles/dlib_.dir/src/other.cpp.o] Error 1
make[1]: *** [CMakeFiles/dlib_.dir/all] Error 2
make: *** [all] Error 2
error: cmake build failed!
Can you check version of your xcode? Error seems similar to the one I was getting. Just a thought – after confirming xcode version, just follow the steps starting from item 1. I am sorry I don’t have any better answer
my xcode is 9.2.
here is the message https://www.code-aster.org/forum2/viewtopic.php?id=22975
the problem seems to be caused by a bug in boost-python
works after changing L56: list _max_cost_assignmen to boost::python::list _max_cost_assignment
L72: const list& assignment to const boost::python::list& assignment
I installed Dlib for mac following your guide. I would like to run the webcam_headpose example. Unfortunately there is a problem with X11. Compiler says:
#error “DLIB_NO_GUI_SUPPORT is defined so you can’t use the GUI code. Turn DLIB_NO_GUI_SUPPORT off if you want to use it.”
#error “Also make sure you have libx11-dev installed on your system”
I have quartz installed on my system. Can anyone help me out? Don’t know what to do anymore.
I had the same problem.
xcode-select –install
finally fixed it for me 🙂
I tried to used “pip install dlib”, but I got a cmake error (Looking for pthread.h – not found, Could NOT find Threads (missing: Threads_FOUND) ). I’m just wondering how to solve this issue.
Hi Satya,
What is the reason for using a different forked version of dlib?
I notice this is the extra patch you’ve used: https://github.com/vaibhawchandel/dlib/commit/935af3330567646a48b69ec16fb114047d8fef84
Could you explain the role of it somewhat..
Regards,
Sowjanya.