Skip to content

Instantly share code, notes, and snippets.

@ageitgey
Last active November 13, 2024 22:52
Show Gist options
  • Save ageitgey/629d75c1baac34dfa5ca2a1928a7aeaf to your computer and use it in GitHub Desktop.
Save ageitgey/629d75c1baac34dfa5ca2a1928a7aeaf to your computer and use it in GitHub Desktop.

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.

@liwg1995
Copy link

liwg1995 commented Dec 24, 2023 via email

@Cyb3r-0
Copy link

Cyb3r-0 commented Dec 25, 2023

好的。
一旦您得到我的问题相关模块的答案,请尽快回复我。

@shuvo881
Copy link

shuvo881 commented Aug 3, 2024

I got this error:

fatal error: Python.h: No such file or directory
274 | #include <Python.h>
| ^~~~~~~~~~
compilation terminated.
In file included from /home/shuvo/Documents/ML Project/Face_Recognition/dlib/dlib/external/pybind11/include/pybind11/attr.h:13,
from /home/shuvo/Documents/ML Project/Face_Recognition/dlib/dlib/external/pybind11/include/pybind11/detail/class.h:12,
from /home/shuvo/Documents/ML Project/Face_Recognition/dlib/dlib/external/pybind11/include/pybind11/pybind11.h:13,
from /home/shuvo/Documents/ML Project/Face_Recognition/dlib/dlib/../dlib/python/pybind_utils.h:6,
from /home/shuvo/Documents/ML Project/Face_Recognition/dlib/dlib/../dlib/python.h:6,
from /home/shuvo/Documents/ML Project/Face_Recognition/dlib/tools/python/src/opaque_types.h:6,
from /home/shuvo/Documents/ML Project/Face_Recognition/dlib/tools/python/src/vector.cpp:4:
/home/shuvo/Documents/ML Project/Face_Recognition/dlib/dlib/external/pybind11/include/pybind11/detail/common.h:274:10: fatal error: Python.h: No such file or directory
274 | #include <Python.h>
| ^~~~~~~~~~
compilation terminated.
In file included from /home/shuvo/Documents/ML Project/Face_Recognition/dlib/dlib/external/pybind11/include/pybind11/attr.h:13,
from /home/shuvo/Documents/ML Project/Face_Recognition/dlib/dlib/external/pybind11/include/pybind11/detail/class.h:12,
from /home/shuvo/Documents/ML Project/Face_Recognition/dlib/dlib/external/pybind11/include/pybind11/pybind11.h:13,
from /home/shuvo/Documents/ML Project/Face_Recognition/dlib/dlib/../dlib/python/pybind_utils.h:6,
from /home/shuvo/Documents/ML Project/Face_Recognition/dlib/dlib/../dlib/python.h:6,
from /home/shuvo/Documents/ML Project/Face_Recognition/dlib/tools/python/src/opaque_types.h:6,
from /home/shuvo/Documents/ML Project/Face_Recognition/dlib/tools/python/src/matrix.cpp:4:
/home/shuvo/Documents/ML Project/Face_Recognition/dlib/dlib/external/pybind11/include/pybind11/detail/common.h:274:10: fatal error: Python.h: No such file or directory
274 | #include <Python.h>
| ^~~~~~~~~~
compilation terminated.
gmake[2]: *** [CMakeFiles/_dlib_pybind11.dir/build.make:90: CMakeFiles/_dlib_pybind11.dir/src/matrix.cpp.o] Error 1
gmake[2]: *** Waiting for unfinished jobs....
gmake[2]: *** [CMakeFiles/_dlib_pybind11.dir/build.make:76: CMakeFiles/_dlib_pybind11.dir/src/dlib.cpp.o] Error 1
gmake[2]: *** [CMakeFiles/_dlib_pybind11.dir/build.make:118: CMakeFiles/_dlib_pybind11.dir/src/svm_c_trainer.cpp.o] Error 1
gmake[2]: *** [CMakeFiles/_dlib_pybind11.dir/build.make:104: CMakeFiles/_dlib_pybind11.dir/src/vector.cpp.o] Error 1
gmake[1]: *** [CMakeFiles/Makefile2:118: CMakeFiles/_dlib_pybind11.dir/all] Error 2
gmake: *** [Makefile:91: all] Error 2
Traceback (most recent call last):
File "/home/shuvo/Documents/ML Project/Face_Recognition/dlib/setup.py", line 206, in
setup(
File "/home/shuvo/Documents/ML Project/Face_Recognition/venv/lib/python3.10/site-packages/setuptools/init.py", line 108, in setup
return distutils.core.setup(**attrs)
File "/home/shuvo/Documents/ML Project/Face_Recognition/venv/lib/python3.10/site-packages/setuptools/_distutils/core.py", line 184, in setup
return run_commands(dist)
File "/home/shuvo/Documents/ML Project/Face_Recognition/venv/lib/python3.10/site-packages/setuptools/_distutils/core.py", line 200, in run_commands
dist.run_commands()
File "/home/shuvo/Documents/ML Project/Face_Recognition/venv/lib/python3.10/site-packages/setuptools/_distutils/dist.py", line 970, in run_commands
self.run_command(cmd)
File "/home/shuvo/Documents/ML Project/Face_Recognition/venv/lib/python3.10/site-packages/setuptools/dist.py", line 945, in run_command
super().run_command(command)
File "/home/shuvo/Documents/ML Project/Face_Recognition/venv/lib/python3.10/site-packages/setuptools/_distutils/dist.py", line 989, in run_command
cmd_obj.run()
File "/home/shuvo/Documents/ML Project/Face_Recognition/venv/lib/python3.10/site-packages/setuptools/command/install.py", line 92, in run
self.do_egg_install()
File "/home/shuvo/Documents/ML Project/Face_Recognition/venv/lib/python3.10/site-packages/setuptools/command/install.py", line 144, in do_egg_install
self.run_command('bdist_egg')
File "/home/shuvo/Documents/ML Project/Face_Recognition/venv/lib/python3.10/site-packages/setuptools/_distutils/cmd.py", line 316, in run_command
self.distribution.run_command(command)
File "/home/shuvo/Documents/ML Project/Face_Recognition/venv/lib/python3.10/site-packages/setuptools/dist.py", line 945, in run_command
super().run_command(command)
File "/home/shuvo/Documents/ML Project/Face_Recognition/venv/lib/python3.10/site-packages/setuptools/_distutils/dist.py", line 989, in run_command
cmd_obj.run()
File "/home/shuvo/Documents/ML Project/Face_Recognition/venv/lib/python3.10/site-packages/setuptools/command/bdist_egg.py", line 167, in run
cmd = self.call_command('install_lib', warn_dir=False)
File "/home/shuvo/Documents/ML Project/Face_Recognition/venv/lib/python3.10/site-packages/setuptools/command/bdist_egg.py", line 153, in call_command
self.run_command(cmdname)
File "/home/shuvo/Documents/ML Project/Face_Recognition/venv/lib/python3.10/site-packages/setuptools/_distutils/cmd.py", line 316, in run_command
self.distribution.run_command(command)
File "/home/shuvo/Documents/ML Project/Face_Recognition/venv/lib/python3.10/site-packages/setuptools/dist.py", line 945, in run_command
super().run_command(command)
File "/home/shuvo/Documents/ML Project/Face_Recognition/venv/lib/python3.10/site-packages/setuptools/_distutils/dist.py", line 989, in run_command
cmd_obj.run()
File "/home/shuvo/Documents/ML Project/Face_Recognition/venv/lib/python3.10/site-packages/setuptools/command/install_lib.py", line 13, in run
self.build()
File "/home/shuvo/Documents/ML Project/Face_Recognition/venv/lib/python3.10/site-packages/setuptools/_distutils/command/install_lib.py", line 110, in build
self.run_command('build_ext')
File "/home/shuvo/Documents/ML Project/Face_Recognition/venv/lib/python3.10/site-packages/setuptools/_distutils/cmd.py", line 316, in run_command
self.distribution.run_command(command)
File "/home/shuvo/Documents/ML Project/Face_Recognition/venv/lib/python3.10/site-packages/setuptools/dist.py", line 945, in run_command
super().run_command(command)
File "/home/shuvo/Documents/ML Project/Face_Recognition/venv/lib/python3.10/site-packages/setuptools/_distutils/dist.py", line 989, in run_command
cmd_obj.run()
File "/home/shuvo/Documents/ML Project/Face_Recognition/dlib/setup.py", line 130, in run
self.build_extension(ext)
File "/home/shuvo/Documents/ML Project/Face_Recognition/dlib/setup.py", line 171, in build_extension
subprocess.check_call(cmake_build, cwd=build_folder)
File "/usr/lib/python3.10/subprocess.py", line 369, in check_call
raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['cmake', '--build', '.', '--config', 'Release', '--', '-j4']' returned non-zero exit status 2

@liwg1995
Copy link

liwg1995 commented Aug 3, 2024 via email

@genghaifei
Copy link

genghaifei commented Aug 19, 2024

need gcc version 10 and cmake version 3.9

@liwg1995
Copy link

liwg1995 commented Aug 19, 2024 via email

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