Install dlib library in Ubuntu

Steps to install dlib library in Ubuntu 18.04

Note: If you have Nvidia graphics card installed. You can greatly increase your dlib performance by installing CUDA and cuDNN. Check my previous blog

How to Install CUDA and cuDNN on Ubuntu Linux

1. Install required build tools for ubuntu

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

2. Install python3 dependencies

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

You can have python lib in two different ways. 1) you can compile it from source or 2) you can install it directly from pip. Issue with pip install is, you cannot use CUDA library with dlib. For that, first you need is to install CUDA on you local machine. Then, you need to compile dlib library from source.

3. Compile dlib from source.

wget http://dlib.net/files/dlib-19.9.tar.bz2
tar xvf dlib-19.9.tar.bz2
cd dlib-19.9/
mkdir build
cd build
cmake ..
cmake --build . --config Release
sudo make install
sudo ldconfig
cd ..
pkg-config --libs --cflags dlib-1

4. Install virtual environment for python2 and python3

Virtual environments in python is a great way to maintain different settings for each project. This way one can avoid package conflicts.

# install virtualenv for python 2
sudo pip2 install virtualenv virtualenvwrapper

# install virtualenv for python 3
sudo pip3 install virtualenv virtualenvwrapper

# update bashrc so that mkvirtualenv, workon and deactivate commands are available in terminal
echo "# Python's Virtual Environment Wrapper" >> ~/.bashrc
echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bashrc
source ~/.bashrc

5. Installing dlib for python 2

# create virtual environment for machine learning
mkvirtualenv ml-py2 -p python2
workon ml-py2
 
# now install python libraries within this virtual environment
pip install numpy scipy matplotlib scikit-image scikit-learn ipython

# if you want to install dlib from pip
pip install dlib
 
# else if you have compiled from source move to dlib's root directory
cd dlib-19.9
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

# quit virtual environment
deactivate

5. dlib library in Python 3

# create virtual environment
mkvirtualenv ml-py3 -p python3
workon ml-py3
 
# now install python libraries within this virtual environment
pip install numpy scipy matplotlib scikit-image scikit-learn ipython
 
# if you want to install dlib from pip
pip install dlib
 
# else if you have compiled from source move to dlib's root directory
cd dlib-19.9
python setup.py install
 
# quit virtual environment
deactivate

Leave a Reply

%d bloggers like this: