How to install dlib

Two weeks ago I interviewed Davis King, the creator and chief maintainer of the dlib library.

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

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.

How to install dlib

Developed by Davis King, 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:

  • BoostBoost 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: As the name of this library suggests, Boost.Python provides interoperability between the C++ and Python programming language.
  • CMake: 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/XQuartx: 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.


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

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:

After completing these steps, continue to Step #2.


In order to install Boost, Boost.Python, and CMake on macOS, you’ll be using the Homebrew package manager. 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:

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):

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

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

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:

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:

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

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:

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 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, then you are likely using Python virtual environments.

Using Python’s virtualenv and virtualenvwrapper 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.

If you would like to install dlib into a pre-existing Python virtual environment, use the workon  command:

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

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:

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 :

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

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:

These packages can be installed via pip :

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:

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:

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

The same goes for my Ubuntu install as well:

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:

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:

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!


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!

, , , , ,

48 Responses to How to install dlib

  1. Sanket March 27, 2017 at 11:10 am #

    Can you install dlib on Windows? If so, how?

    • Adrian Rosebrock March 28, 2017 at 12:59 pm #

      Please see my reply “Anoynmous” below.

  2. Abkul Orto March 27, 2017 at 11:26 am #

    Hello Adrian,
    Thanks for sharing this amazing and exciting C++ library. Kindly advice the right/stable version of Ubuntu to install.

    • Adrian Rosebrock March 28, 2017 at 12:57 pm #

      I’ve personally tested these instructions with both Ubuntu 14.04 and 16.04. Both work.

  3. Tom March 27, 2017 at 11:39 am #

    Hi Adrian,
    Will pip also install the tool for bounding box annotation?
    If anyone’s interested in installing dlib the compile or pip way on Raspberry Pi, remember to increase your swap memory in “/etc/dphys-swapfile”

    • Adrian Rosebrock March 28, 2017 at 12:57 pm #

      Unfortunately, no. For that you will need to compile dlib from source.

      • Daniella Solomon April 7, 2017 at 7:21 am #

        How much you recommend to increase the memory? it doesn’t work on default
        (I have your built raspberry pi image)

        • Adrian Rosebrock April 8, 2017 at 12:49 pm #

          You can increase memory on the Raspberry Pi by decreasing memory allocated to the GPU via raspi-config.

          • Brian Norman April 26, 2017 at 3:13 am #

            I also had the MemoryError problem installing scipy on my Pi 3.
            Using pip’s –no-cache-dir switch allowed the install to complete 0k.

            It might be worth adding that to your excellent instructions.

          • Adrian Rosebrock April 26, 2017 at 6:50 am #

            Hey Brian — I’m actually doing an entirely separate tutorial for installing dlib on the Raspberry Pi next week (1 May 2017). It will include a few additional tips to help get dlib installed on the Pi.

  4. Zethembiso Msomi March 27, 2017 at 11:46 am #

    Is it possible to install this on a Raspberry Pi?

    • Adrian Rosebrock March 28, 2017 at 12:56 pm #

      Yes, absolutely. Ubuntu and Raspbian are Debian-based so you can actually use the Ubuntu instructions to install dlib on your Raspberry Pi.

  5. Simba March 27, 2017 at 2:25 pm #

    Thanks a lot. Was having trouble installing. looking forward to some tutorials with dlib

    • Adrian Rosebrock March 28, 2017 at 12:55 pm #

      Thanks Simba — and congrats on getting dlib installed.

  6. Anoynmous March 28, 2017 at 2:07 am #

    Can we have a same installation process for windows also please.. Struggling a lot for it.

    • Adrian Rosebrock March 28, 2017 at 12:53 pm #

      I do not have any plans to create a Windows install tutorial nor support Windows on the PyImageSearch blog. Please refer to dlib official site for Windows instructions. When it comes to learning computer vision (or most advanced science techniques), I would encourage you to use a Unix-based system.

      • J. Gravett April 2, 2017 at 5:29 am #

        Wow, that’s not exactly helpful. I guess now ‘I do not have any plans’, either…to spend my money on your products.

        • Adrian Rosebrock April 3, 2017 at 2:01 pm #

          I wish I could offer Windows support, but there are honestly too many logistical issues. I offer almost 200 free tutorials here on the PyImageSearch blog. I hope you enjoy them. If you don’t want to purchase any of the teaching products here on PyImageSearch, you don’t have to — no one is asking you to or forcing you to. Enjoy the content and I hope it helps you on your computer vision journey. But please keep in mind that I am just one person and there are limitations to what I can provide. I’ve found that Unix-based environments are the most useful when building computer vision applications (the exception being if you want to build a Windows app, of course). If you have a Windows-specific question, I am not the right person to ask.

        • Peter Lunk April 19, 2017 at 7:05 am #

          Have you concidered adding an extra HD to your system and making it dual bootable with Ubuntu nstalled aside your windows installation ?
          This is what I did…

  7. Royi March 28, 2017 at 2:36 am #

    Hi Adrian,

    Great post!

    I have a nice idea for you.
    Why don’t you show how to use DLib to learn the 194 Facial Landmarks of the HELEN database (Remember to augment the database by mirroring the images)?

    It would be great and will create a new trained predictor for the users of DLib.

    Thank You.

    • Adrian Rosebrock March 28, 2017 at 12:52 pm #

      The dlib library ships with a facial landmark detector? Are you asking me to demonstrate how to train that detector from scratch? Keep in mind that most of the dlib functionality doesn’t have Python bindings, only C++.

      • Royi March 31, 2017 at 9:18 am #

        Hi Adrian,

        Yes, I was talking about training it for a new job.
        My idea was 194 Points of the HELEN database.

        Thank You.

  8. Michael George March 28, 2017 at 4:50 am #

    Thanks for a great post again!

    I had installed dlib a few days back. I am working in a virtual environment using Anaconda and my experience was that there were a few incompatibilities between the boost in anaconda repository and dlib from pip.

    After toiling for a few days, was able to get dlib working with Anaconda. Had to setup dlib and boost from source.

    So if any one is facing difficulty in working with Anaconda and dlib, I might be able to help.

    Adrian, I am eagerly waiting for your future posts on dlib. Thanks!

    • Adrian Rosebrock March 28, 2017 at 12:51 pm #

      Congrats on getting dlib installed Michael. And thank you for being willing to help Anaconda users.

    • JBeale March 29, 2017 at 11:46 am #

      Good to know. I had Anaconda and couldn’t get dlib going due to a boost problem. Finally, simply re-installed Ubuntu from scratch; after that dlib installed OK per instructions here.

  9. Murthy March 30, 2017 at 3:23 am #

    Thanks Adrian. I installed dlib – on Linux 16.04 – no issues at all.
    Look for ward to facial landmark detection

    • Adrian Rosebrock March 31, 2017 at 1:52 pm #

      Congrats on getting dlib installed Murthy, nice job!

  10. Neeraj Kumar April 1, 2017 at 12:04 pm #

    Dear Adrian,

    Thanks a ton man. Just struck the Ubuntu + OpenCV + Python + Dlib installation and configuration in one go. It’s just because of you.

    Neeraj Kumar

    • Adrian Rosebrock April 3, 2017 at 2:08 pm #

      Thank you for the kind words Neeraj — and congratulations on getting dlib installed.

  11. Tony April 1, 2017 at 5:48 pm #

    Straightforward install due to the very clear instructions, also on Ubuntu 16.04. Thanks Adrian

    • Adrian Rosebrock April 3, 2017 at 2:05 pm #

      Thank you Tony!

  12. Yash Bafna April 4, 2017 at 3:15 am #

    Great post!!
    Very well explained!!

    • Adrian Rosebrock April 5, 2017 at 12:01 pm #

      Thanks Yash! 🙂

  13. Matheus April 7, 2017 at 11:33 am #

    I’m getting “Segmentation fault: 11” when importing dlib in python3 in my Mac =/

    • Adrian Rosebrock April 8, 2017 at 12:47 pm #

      99.9% of the time you’ll see a segmentation fault error when you try to import dlib into a different version of Python than it was compiled against. I would suggest compiling dlib from source rather than using pip.

  14. Anonymous April 8, 2017 at 3:38 pm #

    Hello Adrian,

    I am trying to install dlib library on raspberry pi 3.

    The commands in step 3 to install three packages were failed; installed them using sudo apt-get command.

    In the last step “pip install dlib”, it is stuck there (It was also happened with the three packages installation commands) like:

    $ pip install dlib
    Collecting dlib
    Using cached dlib-19.4.0.tar.gz
    Building wheels for collected packages: dlib
    Running bdist_wheel for dlib … /

    please guide us

    • Adrian Rosebrock April 12, 2017 at 1:51 pm #

      The install is likely not “stuck”. It can take 10 minutes to install dlib on a standard laptop/desktop. On a Raspberry Pi this number can jump to an hour. If you run top you’ll see that your system is busy compiling dlib.

  15. Anil Adhikari April 11, 2017 at 6:38 pm #

    Hello Adrian,
    I followed this tutorial to install dlib in python. Everything was going smoothly until the time of installing dlib. When i ran the command “pip install dlib” it gives two error “Failed building wheel for dlib” and “error: cmake configuration failed!”.
    What could be the reason behind this?
    Thanks in advance.

    • Adrian Rosebrock April 12, 2017 at 1:04 pm #

      It sounds like the internal CMake script could not configure dlib correctly. Did you install all the dependencies without error? You have might to resort to compiling dlib from source.

      • eggs April 15, 2017 at 3:35 pm #

        i tried over and over , while install dlib it failed at %79 building cxx…..
        cmake build failed.

        • Adrian Rosebrock April 16, 2017 at 8:52 am #

          Which operating system are you trying to compile dlib on?

          • eggs April 16, 2017 at 11:51 am #

            raspi 2 – raspian 4.4.50-v7+ – python 2.7.x – opencv 3 – virtualenv cv

          • eggs April 19, 2017 at 11:38 am #

            i tried on your pre-installed package for raspi and i get same error again.

          • Adrian Rosebrock April 19, 2017 at 12:42 pm #

            I’m writing a tutorial dedicated to installing dlib on the Raspberry Pi. It will go live at the end of this month. The tutorial will resolve any issues installing dlib on the Raspberry Pi.

  16. jianbo wang April 24, 2017 at 11:12 am #

    Hello, Adrian Rosebrock
    your blog help me a lot, thanks, and let me help others

    for how to install dlib on windows

    1. install python 3.5 32bit, can be download here dlib from here
    select “dlib-18.17.100-cp35-none-win32.whl (md5)”
    last, run pip install dlib-18.17.100-cp35-none-win32.whl

    it success from my 32bit win7

    • Adrian Rosebrock April 24, 2017 at 12:04 pm #

      Thanks for sharing!


  1. Facial landmarks with dlib, OpenCV, and Python - PyImageSearch - April 3, 2017

    […] week we learned how to install and configure dlib on our system with Python […]

  2. Detect eyes, nose, lips, and jaw with dlib, OpenCV, and Python - PyImageSearch - April 10, 2017

    […] weeks ago I demonstrated how to install the dlib library which we are using for facial landmark […]

Leave a Reply