Installing dlib on Raspberry Pi 5 with Python 3 using terminal

In this guide, we’ll walk through the steps to install the dlib library on a Raspberry Pi 5 using Python 3 (compatible with Raspbian OS or ubunto versions from 18 to 23). This includes installing the necessary build tools, Python dependencies, compiling dlib from source, and setting up virtual environments for Python.

Step 1: Install Required Build Tools for Ubuntu

Install the required build tools and libraries:

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

Step 2: Install python3 dependencies

Install the Python 3 development tools and pip:

sudo apt-get install python3-dev python3-pip

Upgrade pip and install numpy:

sudo -H pip3 install -U pip numpy

Step 3: Compile dlib from source.

Download the dlib source code:

wget http://dlib.net/files/dlib-19.9.tar.bz2

Extract the tarball:

tar xvf dlib-19.9.tar.bz2

Navigate to the extracted directory:

cd dlib-19.9/

Create the build directory:

mkdir build

Enter the build directory:

cd build

Configure the build using CMake:

cmake ..

Build dlib:

cmake --build . --config Release

Install dlib:

sudo make install

Update the shared library cache:

sudo ldconfig
cd ..

Verify the installation:

pkg-config --libs --cflags dlib-1

Step 4: Install virtual environment

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

Install virtualenv and virtualenvwrapper for Python 3:

sudo pip3 install virtualenv virtualenvwrapper

Update .bashrc to include virtual environment commands:

echo "# Python's Virtual Environment Wrapper" >> ~/.bashrc
echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bashrc
source ~/.bashrc

Step 5: installing the dlib library

Create a virtual environment (e.g., named “Saber”):

mkvirtualenv Saber -p python3
workon Saber

Install necessary 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

Alternatively, if you compiled dlib from source, move to the dlib root directory:

cd dlib-19.9
python setup.py install

quit virtual environment

deactivate

When attempting to install Python packages using pip, you might encounter this error externally-managed-environment. To resolve this, follow this link

**Congratulations! your Python Dlib library is successfully installed.** {" "}