Install CMake, Python, DLib, Cuda on WSL 2

Homan Huang
4 min readJul 13, 2020

--

In this part, you’ll learn how to install Dlib on WSl 2+Ubuntu. So you can recognize a human face. Nvidia Cuda can accelerate C or Python by GPU power. And we can test Cuda with Docker. Here is the menu.

🐯1. Install CMake
🐰2. Install DLib with Python 3
🚁3. +NVidia CUDA 11 Support

🐯1. Install CMake

CMake is a great C-tool compiler. Especially, CMake-Gui, it can let you compile source code on GUI level. Let’s install Cmake on WSL-2.

$ sudo apt-get -y install cmake
[sudo] password for homanadmin:
Reading package lists... Done
Building dependency tree
...
Processing triggers for man-db (2.9.1-1) ...
Processing triggers for libc-bin (2.31-0ubuntu9) ...

Where is CMake?

$ which cmake
/usr/bin/cmake

What have we installed?

$ cmake --version
cmake version 3.16.3
CMake suite maintained and supported by Kitware (kitware.com/cmake).

We just installed a CLI version. Let’s install GUI version, too.

$ sudo apt-get -y install cmake-qt-gui
Reading package lists... Done
...
Processing triggers for desktop-file-utils (0.24-1ubuntu3) ...

Open CMake on Ubuntu and let’s compare the one on Windows 10.

They have the same interface.

🐰2. Install DLib with Python 3

OpenBlas: Linear algebra optimizations

$ sudo apt-get install libopenblas-dev liblapack-dev -y
Reading package lists... Done
Building dependency tree
...
Unpacking libopenblas0-pthread:amd64 (0.3.8+ds-1ubuntu0.20.04.1) ...

Processing triggers for libc-bin (2.31-0ubuntu9) ...

Python 3: If you have not installed it, we need Python3.

$ sudo apt-get install python3 python3-dev python3-pip -y

Use pip3 of Python to install NumPy.

$ pip3 install numpy
Collecting numpy
...
suppress this warning, use --no-warn-script-location.
Successfully installed numpy-1.19.0

Finally, let’s get Dlib.

~$ sudo pip3 install dlib
Collecting dlib
...
Successfully built dlib
Installing collected packages: dlib
Successfully installed dlib-19.20.0

🚁3. +NVidia CUDA 11 Support

If you have a fast Graphic card like Nvidia GeForce Card, you can add its CUDA support to the Dlib.

Here is the CUDA installation:

🚀 Download the installation pin file:

$ wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/cuda-ubuntu2004.pin...cuda-ubuntu2004.pin 100%[===================>]     190  --.-KB/s    in 0s2020-07-11 15:51:39 (16.3 MB/s) - ‘cuda-ubuntu2004.pin’ saved [190/190]$ sudo mv cuda-ubuntu2004.pin /etc/apt/preferences.d/cuda-repository-pin-600

🚀Get a key file:

$ sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/7fa2af80.pub...
gpg: Total number processed: 1
gpg: imported: 1

🚀Get the package:

$ sudo add-apt-repository "deb http://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/ /"
...
Fetched 731 kB in 2s (309 kB/s)
Reading package lists... Done

🚀Update and install Cuda:

$ sudo apt-get update
...
Reading package lists... Done
$ sudo apt-get -y install cuda
...

Processing triggers for initramfs-tools (0.136ubuntu6.2) ...

CUDA is ready.

Next: Add CUDA to the Dlib source code.

🎢Clone Dlib from Git at mysoft folder: (If you don’t have one, please make one.)

$ cd mysoft
$ git clone https://github.com/davisking/dlib.git
Cloning into 'dlib'...
remote: Enumerating objects: 48162, done.
remote: Total 48162 (delta 0), reused 0 (delta 0), pack-reused 48162
Receiving objects: 100% (48162/48162), 22.78 MiB | 1.78 MiB/s, done.
Resolving deltas: 100% (33985/33985), done.

🎢Edit setup.py at dlib folder:

~/mysoft$ cd dlib
~/mysoft/dlib$ l
CMakeLists.txt MANIFEST.in dlib/ examples/ setup.py
ISSUE_TEMPLATE.md README.md docs/ python_examples/ tools/
~/mysoft/dlib$ nano setup.py

In nano, ctrl+shift+_ : 300 (jump to the end)

Add this line:

os.environ["CC"] = "gcc-9"

🎢Prepare the build folder:

$ mkdir build
$ cd build

Let’s make a library:

$ cmake .. -DCUDA_HOST_COMPILER=/usr/bin/gcc-9 -DCMAKE_PREFIX_PATH=/usr/lib/x86_64-linux-gnu/ -DDLIB_USE_CUDA=1 -DUSE_AVX_INSTRUCTIONS=1 -DUSE_F16C=1
...
-- Build files have been written to: /home/homanadmin/mysoft/dlib/build

Build release:

$ cmake --build . --config Release
...
[ 99%] Building CXX object dlib/CMakeFiles/dlib.dir/image_saver/save_jpeg.cpp.o
[100%] Linking CXX static library libdlib.a
[100%] Built target dlib

🎢Go back to dlib folder and add Python extensions:

$ cd ..
$ sudo python3 setup.py install --record files.txt --set USE_AVX_INSTRUCTIONS=1 --set DLIB_USE_CUDA=1 --compiler-flags "-DCUDA_HOST_COMPILER=/usr/bin/gcc-9"
...
Installed /usr/local/lib/python3.8/dist-packages/dlib-19.20.99-py3.8-linux-x86_64.egg
Processing dependencies for dlib==19.20.99
Finished processing dependencies for dlib==19.20.99
writing list of installed files to 'files.txt'

It’s ready. Let’s test Dlib:

$ python3
Python 3.8.2 (default, Apr 27 2020, 15:53:34)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import dlib
>>> dlib.__version__
'19.20.0'
>>> exit()
$

Done!

--

--

Homan Huang
Homan Huang

Written by Homan Huang

Computer Science BS from SFSU. I studied and worked on Android system since 2017. If you are interesting in my past works, please go to my LinkedIn.

Responses (2)