This is post #3 of a 5-part series on Darknet. If you've not yet read the summary, I suggest you start there.


Darknet is a command-line tool used to train and test neural networks. It is free, in every sense of the word.

Installing Darknet

The original Darknet repository hasn't been updated in several years. The web site has a little bit of information, and points to the repo on GitHub.

Instead, I recommend one of the many forks to the original Darknet. Specifically, I use the popular fork by AlexeyAB, available here. It contains some significant updates. It is important to note that it also differs from the original Darknet, so beware if you are following old tutorials for Darknet. For example, AlexeyAB's fork and the original Darknet do not handle labels the same way.

To get and build AlexeyAB's fork of Darknet, run the following commands:

cd ~/ git clone https://github.com/AlexeyAB/darknet.git cd darknet/ make

If this doesn't work, stop here and check your development environment, there is no use continuing with this post if you cannot build the vanilla version of Darknet.


Next thing we're going to do is enable a few flags in the Makefile and build again. Edit ~/darknet/Makefile and change these lines near the top of the file:


Re-run make to see if this still works. In the end, you should have a darknet and libdarknet.so file.

CUDA Support

If you have a Nvidia GPU and have installed the CUDA sdk, then we can tell Darknet to take advantage of the GPU. Edit the makefile one last time, and make the following additional edits:

Only do this if you have a NVidia GPU and you've installed the NVidia CUDA SDK!

You'll immediately know if you don't have the CUDA sdk installed, because the build will fail with errors related to the CUDA runtime:

include/darknet.h:39:10: fatal error: cuda_runtime.h: No such file or directory #include <cuda_runtime.h>

If you are using CUDA, remember to add both /usr/local/cuda/bin and /usr/local/cuda/NsightCompute... to your path, as well as /usr/local/cuda/lib64 to LD_LIBRARY_PATH. For those of you like myself using fish, here are the commands I ran:

set -U fish_user_paths /usr/local/cuda/bin $fish_user_paths set -U fish_user_paths /usr/local/cuda/NsightCompute-1.0 $fish_user_paths set -Ux LD_LIBRARY_PATH /usr/local/cuda/lib64

Running Darknet on the CPU to train a network is extremely slow. I would even call it unusable to train a decent network. What takes days on a CPU can be done in minutes or a few short hours with a GPU. Once the network is trained, then you can use it on a system with just a CPU, but for training you really need access to a system with a GPU and the CUDA sdk. Amazon EC2 is a great resource.

Build And Copy

Once you have Makefile configured correctly, run the following commands:

make sudo cp libdarknet.so /usr/local/lib/ sudo cp include/darknet.h /usr/local/include/ sudo ldconfig

You should now have the file ~/darknet/darknet, and when you run it without any parameters, you'll see this:

darknet on the cli

Go to post #4 of the series to train a neural network with Darknet.

Last modified: 2019-10-22
Stéphane Charette, stephanecharette@gmail.com