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
@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