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.
Great tutorial! Thanks: https://youtu.be/jKkoFTgjn2k
Hello Satya Mallick. Your tutorials are really great and they have helped me to improve my skills in CV and ML. I followed the instructions of this post and installed the dlib library in my ubuntu OS. I had installed the opencv for python and c++ and the dlib for python before.
know I have the library in this path:
[email protected]:/usr/local/include$ ls
dlib opencv opencv2 python3.6m
pkg-config –libs –cflags dlib-1
-I/usr/local/include -I/usr/include/libpng12 -L/usr/local/lib -ldlib -lpng12
I want to use the library for the landmark detection in Qt project. I need both opencv and dlib. But after running the code Qt complains about Error 2 and the qmake could not find the correct path. I could not solve the problem.
I run this code by adding the existing source and adding the dlib include path and in the release mode the speed is acceptable. The opencv installed library is fine but I can not use the dlib installed library. Here is my .pro file. Any help would be really really appreciated.
QT += core
QT -= gui
TARGET = dlibTest
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
INCLUDEPATH += /usr/local/include/opencv -I/usr/local/include -L/usr/local/lib
LIBS += `pkg-config opencv –libs` `pkg-config –cflags dlib-1`
QMAKE_CXXFLAGS += -std=c++11
I would have a better idea if you can post error message too.
btw shouldn’t LIBS be
LIBS += `pkg-config --libs opencv` `pkg-config --libs dlib-1`
Hello and thanks for replying.
You right. I changed it and this is my .pro file:
QT += core
QT -= gui
TARGET = dlibTest
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
INCLUDEPATH += /usr/local/include/opencv -I/usr/local/include
INCLUDEPATH += /usr/local/include
LIBS += `pkg-config opencv –libs`
LIBS += -I/usr/local/include
LIBS += -I/usr/include/libpng12
LIBS += -L/usr/local/lib -ldlib -lpng12
QMAKE_CXXFLAGS += -std=c++11
But now I have three errors 🙂
The errors are:
g++ -m64 -o dlibTest main.o -L/usr/lib/x86_64-linux-gnu `pkg-config opencv –libs` -I/usr/local/include -I/usr/include/libpng12 -L/usr/local/lib -ldlib -lpng12 -lQtCore -lpthread
/usr/bin/ld: main.o: undefined reference to symbol ‘cblas_sscal’
//usr/lib/libcblas.so.3: error adding symbols: DSO missing from command line
Makefile:102: recipe for target ‘dlibTest’ failed
collect2: error: ld returned 1 exit status
make: *** [dlibTest] Error 1
11:26:33: The process “/usr/bin/make” exited with code 2.
Error while building/deploying project dlibTest (kit: Desktop)
When executing step “Make”
11:26:33: Elapsed time: 00:01.
How should I add these libraries to the .pro file?
salam,
fahmidi chejori library haro add koni?
The dlib isn’t available at the url link. I am getting the following error
[email protected]:~$ wget http://dlib.net/files/dlib-19.4.tar.bz2
–2017-06-23 23:34:01– http://dlib.net/files/dlib-19.4.tar.bz2
Resolving proxy (proxy)… failed: Name or service not known.
wget: unable to resolve host address ‘proxy’
[email protected]:~$
Thank you for sharing this library. It’s really useful.
Hello and thanks for this great site! I found problems when using `sudo make install` specifically with CUDA support.
$ sudo make install
…
[ 50%] Linking CXX shared library libdlib.so
/usr/bin/ld: cannot find -lcudart_static
…
After enabling trace mode in make: `sudo make –trace install` I found that I had to edit the file `builddlibCMakeFilesdlib_shared.dirlink.txt` using absolute paths for the libraries, like this:
-lcudart_static to /usr/local/cuda/lib64/libcurat_static.a
and the same for cudnn, curand, cusolver…
I suppose this file is autogenerated by cmake so we need to change CMakelists.txt…
I am getting error when I am trying to run command –
python setup.py install
CMake Warning at /usr/share/cmake-3.5/Modules/FindBoost.cmake:725 (message):
Imported targets not available for Boost version
Call Stack (most recent call first):
/usr/share/cmake-3.5/Modules/FindBoost.cmake:763 (_Boost_COMPONENT_DEPENDENCIES)
/usr/share/cmake-3.5/Modules/FindBoost.cmake:1332 (_Boost_MISSING_DEPENDENCIES)
/home/vaibhav/Desktop/Dlib_facial_detection/dlib-19.6/dlib/cmake_utils/add_python_module:74 (FIND_PACKAGE)
CMakeLists.txt:9 (include)
— Could NOT find Boost
— Found PythonLibs: /usr/lib/x86_64-linux-gnu/libpython2.7.so (found suitable version “2.7.12”, minimum required is “2.6”)
— *****************************************************************************************************
— To compile Boost.Python yourself download boost from boost.org and then go into the boost root folder
— and run these commands:
— ./bootstrap.sh –with-libraries=python
— ./b2
— sudo ./b2 install
— *****************************************************************************************************
CMake Error at /home/vaibhav/Desktop/Dlib_facial_detection/dlib-19.6/dlib/cmake_utils/add_python_module:116 (message):
Boost python library not found.
Call Stack (most recent call first):
CMakeLists.txt:9 (include)
— Configuring incomplete, errors occurred!
See also “/home/vaibhav/Desktop/Dlib_facial_detection/dlib-19.6/tools/python/build/CMakeFiles/CMakeOutput.log”.
error: cmake configuration failed!
thanks!!!!
Hello
Thanks for this tutorial
I’ve installed Dlib on ubuntu. But when I try to compile my program via g++, it gives me some undefined references to cblas_dgemm and dgesvd_
I added -lopenblas and -llapack to the flags. But the errors remain same!
could you please help?
hi sir i have doubt how to install dlib in aws ?
do the process above but pip install dlib that option will running longer time but could not install please help me ?