Install Dlib on Ubuntu

In this post, we will provide step by step instructions on how to install Dlib on Ubuntu. Step 1: Install OS libraries Step 2: Install Python libraries We will use Virtual Environment to install Python libraries. It is generally a good practice in order to separate your project environment and global

Install Dlib on Ubuntu

Install Dlib on Ubuntu

In this post, we will provide step by step instructions on how to install Dlib on Ubuntu.

Step 1: Install OS libraries

sudo apt-get install build-essential cmake pkg-config
sudo apt-get install libx11-dev libatlas-base-dev
sudo apt-get install libgtk-3-dev libboost-python-dev

Step 2: Install Python libraries

sudo apt-get install python-dev python-pip python3-dev python3-pip
sudo -H pip2 install -U pip numpy
sudo -H pip3 install -U pip numpy

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
sudo pip2 install virtualenv virtualenvwrapper
sudo pip3 install virtualenv virtualenvwrapper
echo "# Virtual Environment Wrapper" >> ~/.bashrc
echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bashrc
source ~/.bashrc

############ 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 3: Compile DLib

Step 3.1: Compile C++ binary

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:

wget http://dlib.net/files/dlib-19.6.tar.bz2
tar xvf dlib-19.6.tar.bz2
cd dlib-19.6/
mkdir build
cd build
cmake ..
cmake --build . --config Release
sudo make install
sudo ldconfig
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 3.2: Compile Python module

Activate Python virtual environment.

############ For Python 2 ############
workon facecourse-py2

############ For Python 3 ############
workon facecourse-py3

Now let’s compile and install Dlib’s Python module.

# move to dlib's root directory
cd dlib-19.6
python setup.py install
# clean up(this step is required if you want to build dlib for both Python2 and Python3)
rm -rf dist
rm -rf tools/python/build
rm python_examples/dlib.so

We have cleaned up few files and directories because Dlib creates Python modules for Python2 and Python3 with the same name. Suppose you ran the setup.py in Python2 virtual environment, it will generate dlib.so in python_examples directory. Now if you deactivate Python2 virtual env, activate Python3 virtual env and run setup.py file, it will replace dlib.so (which was compiled with Python2) in python_examples directory with newer one (which is compiled with Python3). When you will try to run any python_example from within this directory, it will import this dlib.so instead of one located in site-packages or dist-packages directory and throw an error. Although this error won’t occur is a local copy of dlib.so is not present in current directory but it is better to remove local copies to avoid any confusion.

For consistency, we have installed Python and C++ binaries of Dlib using the same source code.

If you are going to use only Python module of Dlib you can also install Python bindings for Dlib using pip.

pip install dlib

Now you can exit from Python virtual environment.

deactivate

Now, whenever you are going to run Python scripts which use Dlib you have to activate the virtual environment using workon command.



Read Next

VideoRAG: Redefining Long-Context Video Comprehension

VideoRAG: Redefining Long-Context Video Comprehension

Discover VideoRAG, a framework that fuses graph-based reasoning and multi-modal retrieval to enhance LLMs' ability to understand multi-hour videos efficiently.

AI Agent in Action: Automating Desktop Tasks with VLMs

AI Agent in Action: Automating Desktop Tasks with VLMs

Learn how to build AI agent from scratch using Moondream3 and Gemini. It is a generic task based agent free from…

The Ultimate Guide To VLM Evaluation Metrics, Datasets, And Benchmarks

The Ultimate Guide To VLM Evaluation Metrics, Datasets, And Benchmarks

Get a comprehensive overview of VLM Evaluation Metrics, Benchmarks and various datasets for tasks like VQA, OCR and Image Captioning.

Subscribe to our Newsletter

Subscribe to our email newsletter to get the latest posts delivered right to your email.

Subscribe to receive the download link, receive updates, and be notified of bug fixes

Which email should I send you the download link?

🎃 Halloween Sale: Exclusive Offer – 30% Off on All Courses.
D
H
M
S
Expired
 

Get Started with OpenCV

Subscribe To Receive

We hate SPAM and promise to keep your email address safe.​