Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?

How to install dlib v19.9 or newer (w/ python bindings) from github on macOS and Ubuntu

Pre-reqs:

  • Have Python 3 installed. On macOS, this could be installed from homebrew or even via standard Python 3.6 downloaded installer from https://www.python.org/download. On Linux, just use your package manager.
  • On macOS:
    • Install XCode from the Mac App Store (or install the XCode command line utils).
    • Have homebrew installed
  • On Linux:
    • For a full list of apt packages required, check out the example Dockerfile and copy what's installed there.
    • These instructions assume you are using Ubuntu 16.04 or newer. If you are using 14.04, you can try these installation instructions instead to work around the old CMake version.
  • These instructions assume you don't have an nVidia GPU and don't have Cuda and cuDNN installed and don't want GPU acceleration (since none of the current Mac models support this).

Clone the code from github:

git clone https://github.com/davisking/dlib.git

Build the main dlib library (optional if you just want to use Python):

cd dlib
mkdir build; cd build; cmake ..; cmake --build .

Build and install the Python extensions:

cd ..
python3 setup.py install

At this point, you should be able to run python3 and type import dlib successfully.

Copy link

ghost commented Mar 15, 2019

thanks for ageitgey that build such amazing project.
here I just want to show a issue that I solved in my machine, maybe can help someone.
I just follow the step to build dlib from source and then build face_recognition, when I run example, it shows me Illegal Instruction. When I first changed set(USE_SSE4_INSTRUCTIONS ON CACHE BOOL "Use SSE4 instructions")
to: set(USE_SSE2_INSTRUCTIONS ON CACHE BOOL "Use SSE2 instructions")
and recompile dlib again, but find it still the same problem.
Then I use cat /proc/cpuinfo, find my flag:
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts mmx fxsr sse sse2 ss ht syscall nx rdtscp lm constant_tsc arch_perfmon pebs bts nopl xtopology tsc_reliable nonstop_tsc aperfmperf pni pclmulqdq ssse3 cx16 sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer aes hypervisor lahf_lm tsc_adjust dtherm arat

I found my cpu support sse2, also sse4, so the steps above did not help me. and also I found my cpu doesn't support avx, so when I compile again with following command and then I success. hope can help you

cmake .. -DDLIB_USE_CUDA=0; cmake --build .

python3 setup.py install --no USE_AVX_INSTRUCTIONS --no DLIB_USE_CUDA

try : sudo python3 setup.py install

@JJJJJJJerk
Copy link

there is a solution for centos 7

  • sudo yum install python36u* -y
  • sudo pip3 install face_recognition

@enochkan
Copy link

this is probably the most concise instructions for installing dlib from source I've found so far. Good job.

@vishivishvish
Copy link

You forgot to mention you need to do brew install cmake after you get brew. Without cmake you can't proceed with the installation.

@qinxiaoxiang
Copy link

I install dlib success
but when I import dlib , I got a Exception:

Reason: Image not Found

@Imomov
Copy link

Imomov commented Aug 17, 2019

if required login-pswd, then clone in this way:
git clone git@github.com:davisking/dlib.git

if you have a python version of 3.7.x then instead of
python3 setup.py install
do
python3.7 setup.py install

@Imomov
Copy link

Imomov commented Aug 17, 2019

In ubuntu just do: sudo apt install libdlib18

thanks, helped

@dharamhbtik
Copy link

When I am installing on CentOS 7 then it is giving me below error. Tried all ways available on internet but it fails everytime.

ERROR: Command errored out with exit status 1:
command: /usr/local/bin/python3.8 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-zav1mbe0/dlib/setup.py'"'"'; file='"'"'/tmp/pip-install-zav1mbe0/dlib/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(file);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, file, '"'"'exec'"'"'))' install --record /tmp/pip-record-84ahsraj/install-record.txt --single-version-externally-managed --compile
cwd: /tmp/pip-install-zav1mbe0/dlib/
Complete output (54 lines):
running install
running build
running build_py
package init file 'dlib/init.py' not found (or not a regular file)
running build_ext
Traceback (most recent call last):
File "/usr/local/bin/cmake", line 8, in
sys.exit(cmake())
File "/usr/local/lib/python3.8/site-packages/cmake/init.py", line 46, in cmake
raise SystemExit(_program('cmake', sys.argv[1:]))
File "/usr/local/lib/python3.8/site-packages/cmake/init.py", line 42, in _program
return subprocess.call([os.path.join(CMAKE_BIN_DIR, name)] + args)
File "/usr/local/lib/python3.8/subprocess.py", line 340, in call
with Popen(*popenargs, **kwargs) as p:
File "/usr/local/lib/python3.8/subprocess.py", line 854, in init
self._execute_child(args, executable, preexec_fn, close_fds,
File "/usr/local/lib/python3.8/subprocess.py", line 1702, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
PermissionError: [Errno 13] Permission denied: '/usr/local/lib/python3.8/site-packages/cmake/data/bin/cmake'
Traceback (most recent call last):
File "", line 1, in
File "/tmp/pip-install-zav1mbe0/dlib/setup.py", line 223, in
setup(
File "/usr/local/lib/python3.8/site-packages/setuptools/init.py", line 145, in setup
return distutils.core.setup(**attrs)
File "/usr/local/lib/python3.8/distutils/core.py", line 148, in setup
dist.run_commands()
File "/usr/local/lib/python3.8/distutils/dist.py", line 966, in run_commands
self.run_command(cmd)
File "/usr/local/lib/python3.8/distutils/dist.py", line 985, in run_command
cmd_obj.run()
File "/usr/local/lib/python3.8/site-packages/setuptools/command/install.py", line 61, in run
return orig.install.run(self)
File "/usr/local/lib/python3.8/distutils/command/install.py", line 545, in run
self.run_command('build')
File "/usr/local/lib/python3.8/distutils/cmd.py", line 313, in run_command
self.distribution.run_command(command)
File "/usr/local/lib/python3.8/distutils/dist.py", line 985, in run_command
cmd_obj.run()
File "/usr/local/lib/python3.8/distutils/command/build.py", line 135, in run
self.run_command(cmd_name)
File "/usr/local/lib/python3.8/distutils/cmd.py", line 313, in run_command
self.distribution.run_command(command)
File "/usr/local/lib/python3.8/distutils/dist.py", line 985, in run_command
cmd_obj.run()
File "/tmp/pip-install-zav1mbe0/dlib/setup.py", line 129, in run
cmake_version = self.get_cmake_version()
File "/tmp/pip-install-zav1mbe0/dlib/setup.py", line 120, in get_cmake_version
out = subprocess.check_output(['cmake', '--version'])
File "/usr/local/lib/python3.8/subprocess.py", line 411, in check_output
return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
File "/usr/local/lib/python3.8/subprocess.py", line 512, in run
raise CalledProcessError(retcode, process.args,
subprocess.CalledProcessError: Command '['cmake', '--version']' returned non-zero exit status 1.
----------------------------------------
ERROR: Command errored out with exit status 1: /usr/local/bin/python3.8 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-zav1mbe0/dlib/setup.py'"'"'; file='"'"'/tmp/pip-install-zav1mbe0/dlib/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(file);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, file, '"'"'exec'"'"'))' install --record /tmp/pip-record-84ahsraj/install-record.txt --single-version-externally-managed --compile Check the logs for full command output.

@slackyboy
Copy link

Success! Running Slackware Current 64, CMAKE 3.15.4, GCC 9.2.0, Python 3.7.5.

Installed /usr/lib64/python3.7/site-packages/dlib-19.18.99-py3.7-linux-x86_64.egg
Processing dependencies for dlib==19.18.99
Finished processing dependencies for dlib==19.18.99_

Although there are several depracation warnings like this:
dlib/dlib/external/pybind11/include/pybind11/pybind11.h:1831:47: warning: ‘int PyThread_set_key_value(int, void*)’ is deprecated [-Wdeprecated-declarations] 1831 | PyThread_set_key_value(key, tstate);

Big thanks!

@hiwanz
Copy link

hiwanz commented Feb 6, 2020

pip3 install dlib works fine for me on Catalina 10.15.3

@lumanti-rgb
Copy link

how to install dlib on windows?

@perinm
Copy link

perinm commented Jun 2, 2020

mkdir build; cd build; cmake .. -DDLIB_USE_CUDA=0 -DUSE_AVX_INSTRUCTIONS=1; cmake --build .
CMake Error: The source directory "/home/piyush/dlib/build" does not appear to contain CMakeLists.txt.
Specify --help for usage, or press the help button on the CMake GUI.
Error: could not load cache

what should i do??

I just made that mistake because the command failed the first i tried, soi I installed cmake, than the second time I tried your error appeared to me due to me not being at folder /dlib, but /dlib/build

@murugan-bala
Copy link

How to install dlib with CUDA enabled GPU ....? ubuntu 18.04 ,.......thanks in advance

@mstva
Copy link

mstva commented Jul 30, 2020

on Ubuntu, I just copied commands from Docker file and added sudo before apt and it works fine for me and installed dlib

@ashen780dimo
Copy link

On this line :
python3 setup.py install --yes USE_AVX_INSTRUCTIONS --no DLIB_USE_CUDA

I am getting the following error :
Traceback (most recent call last):
File "setup.py", line 39, in
from distutils import log
ImportError: cannot import name 'log'

Can you please tell how to correct it ?

sudo apt-get install python3-distutils
works for me

@mveerara
Copy link

https://www.learnopencv.com/install-dlib-on-windows/ instructions to in install dlib on windows

@viswansh
Copy link

viswansh commented Dec 6, 2020

Hi, how do we cross compile python binding libs on new MAC M1 for x86_64 ? I tried following and still got arm64.

make
cmake -DCMAKE_OSX_ARCHITECTURES=x86_64 ..
cmake --build .
python3 setup.py install

find . -name "*.so" -exec file {} ;
./build/lib.macosx-10.9-x86_64-3.9/_dlib_pybind11.cpython-39-darwin.so: Mach-O 64-bit bundle arm64

How can I build python bindings which are for x86_64 ?

@VlasovKirill
Copy link

VlasovKirill commented Dec 20, 2020

Hi, how do we cross compile python binding libs on new MAC M1 for x86_64 ? I tried following and still got arm64.

make
cmake -DCMAKE_OSX_ARCHITECTURES=x86_64 ..
cmake --build .
python3 setup.py install

find . -name "*.so" -exec file {} ;
./build/lib.macosx-10.9-x86_64-3.9/_dlib_pybind11.cpython-39-darwin.so: Mach-O 64-bit bundle arm64

How can I build python bindings which are for x86_64 ?

Probably I have the same problem with new MAC M1. No matter what I do, I get the same error.

 import dlib
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/dlib/__init__.py", line 19, in <module>
    from _dlib_pybind11 import *
ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/_dlib_pybind11.cpython-39-darwin.so, 2): no suitable image found.  Did find:
	/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/_dlib_pybind11.cpython-39-darwin.so: mach-o, but wrong architecture
	/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/_dlib_pybind11.cpython-39-darwin.so: mach-o, but wrong architecture

@Amoscui
Copy link

Amoscui commented Nov 24, 2021

Hi, How can I use dlib to run on the MAC M1 ? I tried many methods, but they all got this error.

import dlib
ImportError: dlopen(/Users/xxxx/miniforge3/envs/py39/lib/python3.9/site-packages/_dlib_pybind11.cpython-39-darwin.so, 0x0002): tried: '/Users/xxxx/miniforge3/envs/py39/lib/python3.9/site-packages/_dlib_pybind11.cpython-39-darwin.so' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64e')), '/usr/local/lib/_dlib_pybind11.cpython-39-darwin.so' (no such file), '/usr/lib/_dlib_pybind11.cpython-39-darwin.so' (no such file)

@Roman-Buckle
Copy link

Hi i am having an issue running the final command, it gets very close to installing and then i am met with this:

Installed /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/dlib-19.22.99-py3.10-macosx-10.9-universal2.egg
Processing dependencies for dlib==19.22.99
Searching for dlib==19.22.99
Reading https://pypi.org/simple/dlib/
No local packages or working download links found for dlib==19.22.99
error: Could not find suitable distribution for Requirement.parse('dlib==19.22.99')

If anyone knows how to solve this issue that would be great!

@chencang
Copy link

cmake -DCMAKE_CXX_COMPILER=/usr/bin/g++ -DCMAKE_C_COMPILER=/usr/bin/gcc ..

see:https://stackoverflow.com/questions/70524164/cmake-c-compiler-not-set-after-enablelanguage?noredirect=1&lq=1

@tsfanning21
Copy link

I am also receiving an error towards the end of the python3 setup: similar to above but a different version:

Installed /Library/Python/3.8/site-packages/dlib-19.24.99-py3.8-macosx-10.14-arm64.egg
Processing dependencies for dlib==19.24.99
Searching for dlib==19.24.99
Reading https://pypi.org/simple/dlib/
No local packages or working download links found for dlib==19.24.99
error: Could not find suitable distribution for Requirement.parse('dlib==19.24.99')

@JM-command
Copy link

2 warnings generated.
5 warnings generated.
1 warning generated.
12 warnings generated.
7 warnings generated.
4 warnings generated.

bruh...

@copbint
Copy link

copbint commented Jul 6, 2022

I am not familiar with pip, but this simple command work on ubuntu 20.04:

pip install dlib-binary

referenced from: https://pypi.org/project/dlib-binary/

then we can import dlib in python3:

root@copbint:~# python3
Python 3.8.10 (default, Mar 15 2022, 12:22:08)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import dlib
>>>

@tcoln
Copy link

tcoln commented Aug 16, 2022

I am not familiar with pip, but this simple command work on ubuntu 20.04:

pip install dlib-binary

referenced from: https://pypi.org/project/dlib-binary/

then we can import dlib in python3:

root@copbint:~# python3
Python 3.8.10 (default, Mar 15 2022, 12:22:08)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import dlib
>>>

Thanks, it does work!

@niktheo
Copy link

niktheo commented Sep 1, 2022

Does anyone has a solution to that?

File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/dlib/__init__.py", line 19, in <module> from _dlib_pybind11 import * ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/_dlib_pybind11.cpython-310-darwin.so, 0x0002): symbol not found in flat namespace (_png_do_expand_palette_rgb8_neon)

@ltst-slk
Copy link

ltst-slk commented Sep 3, 2022

@niktheo

Does anyone has a solution to that?

File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/dlib/__init__.py", line 19, in <module> from _dlib_pybind11 import * ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/_dlib_pybind11.cpython-310-darwin.so, 0x0002): symbol not found in flat namespace (_png_do_expand_palette_rgb8_neon)

I have same issue ,
but when I check on repository dlib.
I found some replay for mac m1.

Please uninstall your dlib . And do this Step.

  1. brew install cmake
  2. brew install dlib
  3. pip install dlib

@niktheo
Copy link

niktheo commented Sep 3, 2022

Im still facing the same issue?
Do you have any other ideas ?
Thanks

@KPB98115
Copy link

KPB98115 commented Dec 6, 2022

I have noticed there have some problem with dlib version 19.24.99, there is no 19.24.99 for dlib(only 19.24.0). Is there any comment of this issue?

`[100%] Built target _dlib_pybind11
creating build/bdist.macosx-10.9-universal2
creating build/bdist.macosx-10.9-universal2/egg
creating build/bdist.macosx-10.9-universal2/egg/dlib
copying build/lib.macosx-10.9-universal2-cpython-39/dlib/init.py -> build/bdist.macosx-10.9-universal2/egg/dlib
copying build/lib.macosx-10.9-universal2-cpython-39/_dlib_pybind11.cpython-39-darwin.so -> build/bdist.macosx-10.9-universal2/egg
byte-compiling build/bdist.macosx-10.9-universal2/egg/dlib/init.py to init.cpython-39.pyc
creating stub loader for _dlib_pybind11.cpython-39-darwin.so
byte-compiling build/bdist.macosx-10.9-universal2/egg/_dlib_pybind11.py to _dlib_pybind11.cpython-39.pyc
creating build/bdist.macosx-10.9-universal2/egg/EGG-INFO
copying tools/python/dlib.egg-info/PKG-INFO -> build/bdist.macosx-10.9-universal2/egg/EGG-INFO
copying tools/python/dlib.egg-info/SOURCES.txt -> build/bdist.macosx-10.9-universal2/egg/EGG-INFO
copying tools/python/dlib.egg-info/dependency_links.txt -> build/bdist.macosx-10.9-universal2/egg/EGG-INFO
copying tools/python/dlib.egg-info/not-zip-safe -> build/bdist.macosx-10.9-universal2/egg/EGG-INFO
copying tools/python/dlib.egg-info/top_level.txt -> build/bdist.macosx-10.9-universal2/egg/EGG-INFO
writing build/bdist.macosx-10.9-universal2/egg/EGG-INFO/native_libs.txt
creating dist
creating 'dist/dlib-19.24.99-py3.9-macosx-10.9-universal2.egg' and adding 'build/bdist.macosx-10.9-universal2/egg' to it
removing 'build/bdist.macosx-10.9-universal2/egg' (and everything under it)
Processing dlib-19.24.99-py3.9-macosx-10.9-universal2.egg
creating /Users/kingston/Doc/workplace/py_venv/lab_faceRec/lib/python3.9/site-packages/dlib-19.24.99-py3.9-macosx-10.9-universal2.egg
Extracting dlib-19.24.99-py3.9-macosx-10.9-universal2.egg to /Users/kingston/Doc/workplace/py_venv/lab_faceRec/lib/python3.9/site-packages
Adding dlib 19.24.99 to easy-install.pth file

Installed /Users/kingston/Doc/workplace/py_venv/lab_faceRec/lib/python3.9/site-packages/dlib-19.24.99-py3.9-macosx-10.9-universal2.egg
Processing dependencies for dlib==19.24.99
Searching for dlib==19.24.99
Reading https://pypi.org/simple/dlib/
/Users/kingston/Doc/workplace/py_venv/lab_faceRec/lib/python3.9/site-packages/pkg_resources/init.py:123: PkgResourcesDeprecationWarning: is an invalid version and will not be supported in a future release
warnings.warn(
No local packages or working download links found for dlib==19.24.99
error: Could not find suitable distribution for Requirement.parse('dlib==19.24.99')
`

@priyanshugoel001
Copy link

This is Easiest and best solution provided on internet

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