Title: How to install dlib - PyImageSearch

URL Source: https://pyimagesearch.com/2017/03/27/how-to-install-dlib/

Published Time: 2017-03-27T14:00:43+00:00

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

Two weeks ago I [interviewed Davis King](https://pyimagesearch.com/2017/03/13/an-interview-with-davis-king-creator-of-the-dlib-toolkit/), the creator and chief maintainer of the [dlib library](http://dlib.net/).

Today I am going to demonstrate how to install dlib with Python bindings on both _macOS_ and _Ubuntu_.

I _**highly encourage**_ you to take the time to install dlib on your system over the next couple of days.

Starting next week we’ll be diving head first into one of dlib’s core computer vision implementations — _**facial landmark detection**_.

I’ll be demonstrating how to use facial landmarks for:

*   Face part (i.e., eyes, nose, mouth, etc.) extraction
*   Facial alignment
*   Blink detection
*   _…and much more._

But it all starts with getting dlib installed!

**To learn how to install dlib with Python bindings on your system, _just keep reading._**

Developed by [Davis King](https://github.com/davisking), the dlib C++ library is a cross-platform package for threading, networking, numerical operations, machine learning, computer vision, and compression, placing a strong emphasis on _extremely high-quality_ and _portable code_. The documentation for dlib is also quite _fantastic_.

From a computer vision perspective, dlib has a number of state-of-the-art implementations, including:

*   Facial landmark detection
*   Correlation tracking
*   Deep metric learning

Over the next few weeks we’ll be exploring some of these techniques (especially facial landmark detection), so definitely take the time now to get dlib configured and installed on your system.

### Step #1: Install dlib prerequisites

The dlib library only has four primary prerequisites:

*   **[Boost](http://www.boost.org/):** Boost is a collection of peer-reviewed (i.e., very high quality) C++ libraries that help programmers not get caught up in reinventing the wheel. Boost provides implementations for linear algebra, multithreading, basic image processing, and unit testing, just to name a few.
*   **[Boost.Python](http://www.boost.org/doc/libs/1_57_0/libs/python/doc/index.html):** As the name of this library suggests, Boost.Python provides interoperability between the C++ and Python programming language.
*   **[CMake](https://cmake.org/):** CMake is an open-source, cross-platform set of tools used to build, test, and package software. You might already be familiar with CMake if you have used it to compile OpenCV on your system.
*   **[X11](https://en.wikipedia.org/wiki/X_Window_System)/[XQuartx](https://www.xquartz.org/):** Short for _“X Window System”_, X11 provides a basic framework for GUI development, common on Unix-like operating systems. The macOS/OSX version of X11 is called _XQuartz_.

I’ll show you how to install each of these prerequisites on your Ubuntu or macOS machine below.

#### Ubuntu

Installing CMake, Boost, Boost.Python, and X11 can be accomplished easily with `apt-get` :

$ sudo apt-get install build-essential cmake
$ sudo apt-get install libgtk-3-dev
$ sudo apt-get install libboost-all-dev

I assume you already have `pip` (for managing, installing, and upgrading Python packages) installed on your machine, but if not, you can install `pip` via:

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

After completing these steps, continue to Step #2.

#### macOS

In order to install Boost, Boost.Python, and CMake on macOS, you’ll be using the [Homebrew package manager](https://brew.sh/). Think of Homebrew as a similar equivalent of Ubuntu’s `apt-get` only for macOS.

If you haven’t already installed Homebrew, you can do so by executing the following commands:

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

_**Hint:** You can check if Homebrew is already installed on your machine by executing the `brew` command in your terminal. If you get a `brew: command not found` error, then Homebrew is not installed on your machine._

Now that Homebrew is installed, open up your `~/.bash_profile` file (create it if it doesn’t exist):

$ nano ~/.bash\_profile

And update your `PATH` variable to check for packages installed by Homebrew _before_ checking the rest of your system:

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

After updating your `~/.bash_profile` file, it should look similar to mine:

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

**Figure 1:** After updating your _~/.bash\_profile_ file, yours should look similar to mine.

We now need to reload the contents of the `~/.bash_profile` file via the `source` command:

$ source ~/.bash\_profile

This command only needs to be executed _once_. Alternatively, you can open up a new terminal window which will _automatically_ `source` the `~/.bash_profile` for you.

Next, let’s install Python 2.7 and Python 3:

$ brew install python
$ brew install python3

We can then install CMake, Boost, and Boost.Python:

$ brew install cmake
$ brew install boost
$ brew install boost-python --with-python3

The `--with-python3` flag ensures that Python 3 bindings for Boost.Python are compiled as well — Python 2.7 bindings are compiled by default.

Once you start the `boost-python` install, consider going for a nice walk as the build can take a bit of time (10-15 minutes).

As a sanity check, I would suggest validating that you have both `boost` and `boost-python` installed before proceeding:

$ brew list | grep 'boost'
boost
boost-python

As you can see from my terminal output, both Boost and Boost.Python have been successfully installed.

The last step is to install the [XQuartz window manager](https://www.xquartz.org/) so we can have access to X11. XQuartz is easy to install — just download the `.dmg` and run the install wizard. _**After installing, make sure you logout and log back in!**_

_**Fun Fact**: XQuartz used to be installed by_ default _on OSX 10.5-10.7. We now need to manually install it._

Now that we have our prerequisites installed, let’s continue to our next (optional) step.

### Step #2: Access your Python virtual environment (optional)

If you have followed any of my PyImageSearch tutorials on [installing OpenCV](https://pyimagesearch.com/opencv-tutorials-resources-guides/), then you are likely using Python virtual environments.

Using Python’s [virtualenv](https://virtualenv.pypa.io/en/latest/) and [virtualenvwrapper](https://virtualenvwrapper.readthedocs.io/en/latest/) libraries, we can create _separate, independent_ Python environments for each project we are working on — this is considered a **best practice** when developing software in the Python programming language.

_**Note:** I’ve already discussed Python virtual environments many times before on the PyImageSearch blog so I won’t spend any more time discussing them here today — if you would like to read more about them, please see any of my [installing OpenCV tutorials](https://pyimagesearch.com/opencv-tutorials-resources-guides/)._

If you would like to install dlib into a _pre-existing_ Python virtual environment, use the `workon` command:

$ workon <your virtualenv name>

For example, if I wanted to access a Python virtual environment named `cv` , I would use the command:

$ workon cv

Notice how my terminal window has changed — the text `(cv)` now appears before my prompt, indicating that I am in the `cv` Python virtual environment:

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

**Figure 2:** I can tell that I am in the _“cv”_ Python virtual environment by validating that the text _“(cv)”_ appears before my prompt.

Otherwise, I can create an entirely separate virtual environment using the `mkvirtualenv` command — the command below creates a Python 2.7 virtual environment named `py2_dlib` :

$ mkvirtualenv py2\_dlib

While this command will create a Python 3 virtual environment named `py3_dlib` :

$ mkvirtualenv py3\_dlib -p python3

Again, please keep in mind that using Python virtual environments are _optional_, but highly recommended if you are doing any type of Python development.

For readers that have followed my previous OpenCV install tutorials here on the PyImageSearch blog, please make sure you access your Python virtual environment before proceeding to Step #3 (as you’ll need to install the Python prerequisites + dlib into your virtual environment).

### Step #3: Install dlib with Python bindings

The dlib library doesn’t have any real Python prerequisites, but if you plan on using dlib for any type of computer vision or image processing, I would recommend installing:

*   [NumPy](http://www.numpy.org/)
*   [SciPy](https://scipy.org/)
*   [scikit-image](http://scikit-image.org/)

These packages can be installed via `pip` :

$ pip install numpy
$ pip install scipy
$ pip install scikit-image

Years ago, we had to compile dlib manually from source (similar to how we install OpenCV). However, we can now use `pip` to install dlib as well:

$ pip install dlib

This command will download the dlib package from PyPI, automatically configure it via CMake, and then compile and install it on your system.

Provided you have the CMake, Boost, Boost.Python, and X11/XQuartz installed on your system, the command should exit without error (leaving you with a successful dlib install).

I would suggest going out for a nice cup of coffee as this step can take 5-10 minutes for the compile to finish.

After coming back, you should see that dlib has been successfully installed:

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

**Figure 3:** The dlib library with Python bindings on macOS have been successfully installed.

The same goes for my Ubuntu install as well:

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

**Figure 4:** Installing dlib with Python bindings on Ubuntu.

### Step #4: Test out your dlib install

To test out your dlib installation, just open up a Python shell (making sure to access your virtual environment if you used them), and try to import the `dlib` library:

$ python
Python 3.6.0 (default, Mar  4 2017, 12:32:34) 
\[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)\] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import dlib
>>>

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

**Figure 5:** Testing out my dlib + Python install on macOS and Python 3.6.

If you’ve installed dlib into the same Python virtual environment that you installed OpenCV, you can access OpenCV as well via your `cv2` bindings. Here is an example on my Ubuntu machine:

$ python
Python 2.7.12 (default, Jul  1 2016, 15:12:24) 
\[GCC 5.4.0 20160609\] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import dlib
>>> import cv2
>>> cv2.\_\_version\_\_
'3.1.0'
>>>

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

**Figure 6:** Validating that I can import both dlib and OpenCV into the same Python shell.

Congratulations, you now have dlib installed on your system!

### 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
-------

In today’s blog post I demonstrated how to install the dlib library with Python bindings on Ubuntu and macOS.

Next week we’ll start exploring how to use dlib; specifically, **facial landmark detection**.

**You won’t want to miss this tutorial, so to be notified when the next post is published, _be sure to enter your email address in the form below!_**

See you next week!

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

#### Join the PyImageSearch Newsletter and Grab My FREE 17-page Resource Guide PDF

Enter your email address below to **join the PyImageSearch Newsletter** and **download my FREE 17-page Resource Guide PDF** on Computer Vision, OpenCV, and Deep Learning.