Skip to content

Instantly share code, notes, and snippets.

@ageitgey
Last active January 6, 2026 05:36
Show Gist options
  • Select an option

  • Save ageitgey/1ac8dbe8572f3f533df6269dab35df65 to your computer and use it in GitHub Desktop.

Select an option

Save ageitgey/1ac8dbe8572f3f533df6269dab35df65 to your computer and use it in GitHub Desktop.

Install dlib and face_recognition on a Raspberry Pi

Instructions tested with a Raspberry Pi 2 with an 8GB memory card. Probably also works fine on a Raspberry Pi 3.

Steps

Download the latest Raspbian Jessie Light image. Earlier versions of Raspbian won't work.

Write it to a memory card using Etcher, put the memory card in the RPi and boot it up.

Log in. Default username / password is pi / raspberry.

Set up Wifi (if you are using Wifi) according to the Raspberry Pi instructions.

Run sudo raspi-config and configure the basics:

  • Set up your keyboard layout (It defaults to a British keyboard layout)
  • Change default user password
  • Enable the Raspberry Pi camera (if you have one attached)
  • Configure gpu memory split under 'Advanced'. Set it up '16'.
  • Save changes and reboot.

Install required libraries with these commands:

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install build-essential \
    cmake \
    gfortran \
    git \
    wget \
    curl \
    graphicsmagick \
    libgraphicsmagick1-dev \
    libatlas-dev \
    libavcodec-dev \
    libavformat-dev \
    libboost-all-dev \
    libgtk2.0-dev \
    libjpeg-dev \
    liblapack-dev \
    libswscale-dev \
    pkg-config \
    python3-dev \
    python3-numpy \
    python3-pip \
    zip
sudo apt-get clean

Install the picamera python library with array support (if you are using a camera):

sudo apt-get install python3-picamera
sudo pip3 install --upgrade picamera[array]

Temporarily enable a larger swap file size (so the dlib compile won't fail due to limited memory):

sudo nano /etc/dphys-swapfile

< change CONF_SWAPSIZE=100 to CONF_SWAPSIZE=1024 and save / exit nano >

sudo /etc/init.d/dphys-swapfile restart

Download and install dlib v19.6:

mkdir -p dlib
git clone -b 'v19.6' --single-branch https://github.com/davisking/dlib.git dlib/
cd ./dlib
sudo python3 setup.py install --compiler-flags "-mfpu=neon"

Install face_recognition:

sudo pip3 install face_recognition

Revert the swap file size change now that dlib is installed:

sudo nano /etc/dphys-swapfile

< change CONF_SWAPSIZE=1024 to CONF_SWAPSIZE=100 and save / exit nano >

sudo /etc/init.d/dphys-swapfile restart

Download the face recognition code examples:

git clone --single-branch https://github.com/ageitgey/face_recognition.git
cd ./face_recognition/examples
python3 facerec_on_raspberry_pi.py

Totally Optional: If you want a desktop GUI, install PIXEL:

sudo apt-get install --no-install-recommends xserver-xorg xinit raspberrypi-ui-mods
@robotzero1
Copy link

I've just updated the tutorial using this on my blog. Seems to work fine with Pi OS LITE on a Pi Zero. dlib can be installed now without the compilation so the whole thing is much easier. Instructions here: https://robotzero.one/face-recognition-party-greeter-raspberry-pi/

@magentapenguin
Copy link

magentapenguin commented May 25, 2022

I'm having a problem where I installed Boost (The way the error says to install it), but it says that it could not find it when I run sudo python3 setup.py install --compiler-flags "-mfpu=neon".

I'm using a Raspberry Pi 4 B with python 3.9

Here is the full output:
running install running bdist_egg running build Detected Python architecture: 32bit Detected platform: linux Removing build directory /home/pi/dlib/./tools/python/build Configuring cmake ... /usr/lib/python3.9/subprocess.py:941: RuntimeWarning: line buffering (buffering=1) isn't supported in binary mode, the default buffer size will be used self.stdout = io.open(c2pread, 'rb', bufsize) CMake Warning (dev) in CMakeLists.txt: No project() command is present. The top-level CMakeLists.txt file must contain a literal, direct call to the project() command. Add a line of code such as project(ProjectName) near the top of the file, but after cmake_minimum_required(). CMake is pretending there is a "project(Project)" command on the first line. This warning is for project developers. Use -Wno-dev to suppress it. -- The C compiler identification is GNU 10.2.1 -- The CXX compiler identification is GNU 10.2.1 -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Check for working C compiler: /usr/bin/cc - skipped -- Detecting C compile features -- Detecting C compile features - done -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Check for working CXX compiler: /usr/bin/c++ - skipped -- Detecting CXX compile features -- Detecting CXX compile features - done CMake Warning at /usr/share/cmake-3.18/Modules/FindBoost.cmake:1187 (message): New Boost version may have incorrect or missing dependencies and imported targets Call Stack (most recent call first): /usr/share/cmake-3.18/Modules/FindBoost.cmake:1311 (_Boost_COMPONENT_DEPENDENCIES) /usr/share/cmake-3.18/Modules/FindBoost.cmake:1919 (_Boost_MISSING_DEPENDENCIES) /home/pi/dlib/dlib/cmake_utils/add_python_module:61 (FIND_PACKAGE) CMakeLists.txt:9 (include) CMake Warning at /usr/share/cmake-3.18/Modules/FindBoost.cmake:2018 (message): No header defined for python-py34; skipping header check (note: header-only libraries have no designated component) Call Stack (most recent call first): /home/pi/dlib/dlib/cmake_utils/add_python_module:61 (FIND_PACKAGE) CMakeLists.txt:9 (include) -- Could NOT find Boost (missing: python-py34) (found suitable version "1.74.0", minimum required is "1.41.0") CMake Warning at /usr/share/cmake-3.18/Modules/FindBoost.cmake:1187 (message): New Boost version may have incorrect or missing dependencies and imported targets Call Stack (most recent call first): /usr/share/cmake-3.18/Modules/FindBoost.cmake:1311 (_Boost_COMPONENT_DEPENDENCIES) /usr/share/cmake-3.18/Modules/FindBoost.cmake:1919 (_Boost_MISSING_DEPENDENCIES) /home/pi/dlib/dlib/cmake_utils/add_python_module:63 (FIND_PACKAGE) CMakeLists.txt:9 (include) CMake Warning at /usr/share/cmake-3.18/Modules/FindBoost.cmake:2018 (message): No header defined for python-py35; skipping header check (note: header-only libraries have no designated component) Call Stack (most recent call first): /home/pi/dlib/dlib/cmake_utils/add_python_module:63 (FIND_PACKAGE) CMakeLists.txt:9 (include) -- Could NOT find Boost (missing: python-py35) (found suitable version "1.74.0", minimum required is "1.41.0") CMake Warning at /usr/share/cmake-3.18/Modules/FindBoost.cmake:1187 (message): New Boost version may have incorrect or missing dependencies and imported targets Call Stack (most recent call first): /usr/share/cmake-3.18/Modules/FindBoost.cmake:1311 (_Boost_COMPONENT_DEPENDENCIES) /usr/share/cmake-3.18/Modules/FindBoost.cmake:1919 (_Boost_MISSING_DEPENDENCIES) /home/pi/dlib/dlib/cmake_utils/add_python_module:66 (FIND_PACKAGE) CMakeLists.txt:9 (include) -- Could NOT find Boost (missing: python3) (found suitable version "1.74.0", minimum required is "1.41.0") CMake Warning at /usr/share/cmake-3.18/Modules/FindBoost.cmake:1187 (message): New Boost version may have incorrect or missing dependencies and imported targets Call Stack (most recent call first): /usr/share/cmake-3.18/Modules/FindBoost.cmake:1311 (_Boost_COMPONENT_DEPENDENCIES) /usr/share/cmake-3.18/Modules/FindBoost.cmake:1919 (_Boost_MISSING_DEPENDENCIES) /home/pi/dlib/dlib/cmake_utils/add_python_module:69 (FIND_PACKAGE) CMakeLists.txt:9 (include) -- Could NOT find Boost (missing: python) (found suitable version "1.74.0", minimum required is "1.41.0") -- Found PythonLibs: /usr/lib/arm-linux-gnueabihf/libpython3.9.so (found suitable version "3.9.2", minimum required is "3.4") -- ***************************************************************************************************** -- 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/pi/dlib/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/pi/dlib/tools/python/build/CMakeFiles/CMakeOutput.log". error: cmake configuration failed!

@jmcausing
Copy link

Can anyone update the latest instructions like the one mentioned here? https://gist.github.com/ageitgey/1ac8dbe8572f3f533df6269dab35df65?permalink_comment_id=3030497#gistcomment-3030497

This instructions doesn't mention downloading and compiling opencv. @ageitgey

@mrtass
Copy link

mrtass commented Sep 21, 2022 via email

@mfwz247
Copy link

mfwz247 commented Apr 1, 2023

If you already built dlib 19.6 and found out latest face_recognition requires 19.7 or above just do
pip install face_recognition==1.0.0
It requires dlib>=19.5

@ovidiumoise
Copy link

Hello, is it still relevant in 2024?
I have Raspberry Pi 4 Model B 4G or Raspberry Pi 5 8GB with latest OS (Bookworm).
I tried at least 3 or 4 other alternatives and I'm sick of issues and errors. Did it anyone made it lately?
I will try today on Raspberry Pi4 Model B 4G.

@mardelmariam
Copy link

mardelmariam commented Jun 20, 2024

The library (face_recognition) continues working well for me on a Raspberry Pi 5 with the Raspbian Bookworm OS. However, all steps change due to various changes in the very OS. As I compiled OpenCV first, I changed and prepared many system packages, and prevented me further headaches. After that, I was able to install dlib and face recognition using pip, both at the most recent versions.

@OmarAKhan1
Copy link

The library (face_recognition) continues working well for me on a Raspberry Pi 5 with the Raspbian Bookworm OS. However, all steps change due to various changes in the very OS. As I compiled OpenCV first, I changed and prepared many system packages, and prevented me further headaches. After that, I was able to install dlib and face recognition using pip, both at the most recent versions.

Hi am currently working on a Raspberry Pi 4 with Raspbian Bookworm OS. I am struggling with finding the correct steps to download dlib and face recognition (I successfully downloaded OpenCV). Can you please give me a quick rundown on how you downloaded dlib and face recognition because whenever I try installing them I keep get an error about an "externally managed environment." Any help or guidance would be greatly appreciated! Thank you!!

@Bonzadog
Copy link

Bonzadog commented Jun 22, 2024 via email

@Bonzadog
Copy link

Bonzadog commented Jun 29, 2024 via email

@camoverride
Copy link

Anyone have any luck installing face_recognition on a Raspberry Pi 5 (running Bookworm)? All the instructions on the internet are out of date...

@mardelmariam
Copy link

For anyone who is struggling with the installation of the required libraries: you will need to compile OpenCV, so the OS will run C/C++ files instead of running OpenCV with the Python files. A guide for this step is here. If some errors appear, Google is your friend. After installing OpenCV, you may install dlib, and that should work.

@mardelmariam
Copy link

mardelmariam commented Sep 12, 2024 via email

@LouayTselkim
Copy link

so this is how it worked for me on raspberry pi 3b
i did zram, increased swap file to 2gb, stopped the desktop environment (lightdm) and other services that i don't need and booted into console to save as much ram as possible (i got it to 95mb).
i installed dependencies of opencv:
sudo apt-get install -y build-essential cmake git unzip pkg-config libjpeg-dev libpng-dev libavcodec-dev libavformat-dev libswscale-dev libgtk2.0-dev libcanberra-gtk* libgtk-3-dev libgstreamer1.0-dev gstreamer1.0-gtk3 libgstreamer-plugins-base1.0-dev gstreamer1.0-gl gstreamer1.0-libcamera libxvidcore-dev libx264-dev python3-dev python3-numpy python3-pip libtbbmalloc2 libtbb-dev libv4l-dev v4l-utils libopenblas-dev libatlas-base-dev libblas-dev liblapack-dev gfortran libhdf5-dev libprotobuf-dev libgoogle-glog-dev libgflags-dev protobuf-compiler
and then:
pip install opencv-python pip install imutils
and then:
pip install --no-cache-dir dlib
i took around 3 hours to install but it was successful (try to save as much ram as possible for it)
and then:
'pip install face-recognition'

@JoeyLi-1
Copy link

JoeyLi-1 commented Jan 5, 2026

Hey @LouayTselkim,

Thanks for sharing! That is great!
Can you share the OS version as well? I m on Trixie and am stuck at "libatlas-base-dev". It is not there anymore.
Seems this support it.
I don`t know which is better? Downgrade OS version or try to compile it from source code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment