How to compile chess engines and programs for Linux ?

Updated: Jun 21


This article was inspired by MartinPL1977. Thank you :)


Your Linux and favorite distribution. You have perfected it down to the smallest detail. It looks beautiful, it is functional, the programs do their job. The whole system for you works better than any Windows or macOS machine.


You are proud of it and would like to be able to use modern engines and useful chess software. But... not everything you are interested in is ready to work immediately. It has to be compiled.


Is it difficult ?

Does it require specialized knowledge ?

How to do that ?


I will try to answer the questions posed above.


This article is for those who understand how Linux works and know what commands typed at the Linux command line do.


First I have some good informations for you. Compiling engines and programs in most cases is not difficult and comes down to typing just a few commands in the command line.


Typically, properly and independently compiled chess engines run slightly faster on the same computer on Linux than the same engines on a Windows computer.

Why is this the case ?

The easiest way to understand this is to use the example of buying a suit. When you buy - even in the best store - a suit, you choose a particular size. The suit fits and it's fine.


On the other hand, when you go to a tailor, who will tailor the suit to your exact measurements, you will not only get a suit that fits, but one that is exactly tailored to your measurements. And this tailor-made suit will fit you perfectly.


Without going into technical details - it is similar with compilation of chess software, which due to its specialized nature - works best on the machine on which it was compiled. It is not a coincidence that at important competitions where chess engines have participated and are participating, they are run on Linux - in the environment where they were previously compiled.


How much difference in speed can there be between an engine compiled for a particular machine versus the same engine not optimized for that machine ?

The difference can range from a fraction of a percent to several percent.


Is it much or is it little ? It depends.

During usual chess engine activities, such as chess game analysis or training applications - it does not matter much.

But if the engine participates in competitions or it is important to get as much power as possible or you use it to analyze difficult positions in correspondence games or looking for an effective novelty in a chess opening - then the speed higher by "just" a fraction of a percent will be important, it may increase the chance of a draw or even victory in an important chess game.


You don't need specialized knowledge to compile a chess engine or program properly. You don't even need to know how to program. However, it is worth reading with concentration and understanding, because usually in the sources of the program there is a file describing the steps of compilation of the given engine or chess program.



Ok. So without further ado, what are we going to compile ?


First, we'll take a compilation of two free modern and powerful chess engines, Berserk and Stockfish. Then we will take a compilation of the best free chess database program Scid vs PC.



What do you need to compile ?


1. Linux Distribution.

It doesn't really matter what distribution it is. It will compile the same way on Arch, Debian, Fedora, Gentoo, Ubuntu or any other distribution. It doesn't really matter if the Linux distribution you are using uses an AMD, ARM or other processor.


2. Compiler.

We will compile software written in C / C++ so we need a compiler called GCC (GNU Compiler Collection).


3. Source code.

It takes source code in file/s form for the GCC compiler to be able to create a working engine / program.


4. Anything else ?

In some cases, depending on, for example, the installed version of the GCC compiler and the Linux distribution libraries, you may need to install components specified by the compiler.


5. Time. How long does it take to compile ?

On a modern computer, it usually takes tens of seconds to compile.

Even on older computers, e.g. 10-15 years old, compiling a chess engine will take from tens of seconds to several minutes. The compilation of a program like Scid vs. PC may take a little longer because of its larger size.



Will the compilation be described in detail ?


All compilations will be explained and presented in such a way that the Reader, by following all compilation steps - will be able to compile the software described below by himself.

Due to the nature of the compilation process and the required knowledge of the Reader, the compilation descriptions will not address detailed issues of how Linux works and the effects of executing command line commands.



Workshops for compilation.


I am using Debian GNU/Linux 10 (buster).

This distribution is supported until 2024 (LTS - Long Term Support); it is a very stable and high quality distribution with frequent updates.

Debian runs on my computer with ARM64 processors.


The compilation will be performed by GCC version 8.3.0.


If you do not have the GCC compiler installed, I would refer you to the help system of the Linux distribution you are using. There you will get information how to install GCC.


In Debian, the following steps are required to install GCC:


1. Update the packages list:

sudo apt update

2. Install the build-essential package by running:

sudo apt install build-essential

3. You can skip this step.

You would like also to install the manual pages that includes documentation about using GNU/Linux for (among others) compiling.

sudo apt-get install manpages-dev

4. To confirm that the GCC compiler is successfully installed type:

gcc --version


A compilation of the Berserk chess engine.


Berserk is licensed under the GPL v3.0.


We will be compiling the latest publicly available version of this engine.

The Berserk source code is available on GitHub at: https://github.com/jhonnold/berserk


To compile Berserk we will use the information on this engine's page.


1.

In the first step, we will select the directory where the source code will reside.


Type:

cd

Of course, you must press the Enter key after typing each command.


2.

In this, step we will download the source code.


Type the command:

git clone https://github.com/jhonnold/berserk


If the download does not start, it is likely that git is not installed on your system - then install git and repeat step #2.



How to install git
sudo apt install git


On my internet connection, the download took about 30 seconds.


3.

Type:

cd berserk/src


4.

Type the command:

make basic

Now the compilation will take place.

If any errors occur during compilation, the engine will not compile and the GCC compiler will show messages on the screen.


Unfortunately, the compilation failed.

Why? Look carefully at the messages the compiler gave you.

GCC suggests that the stdlib.h library should be added to the nn.c file.


Open the nn.c file in any text file editor - I used the system Text Editor.


In line number 20, type:

#include <stdlib.h>

Save the file and then close it.


Go back to the command prompt and type again: make basic

make basic

Success!

Berserk chess engine has been compiled :)


So let's see if it works.

Type:

./berserk

And then type:

uci

Confirm with the Enter key.


Perfect !!

The latest Berserk 20220603 is ready to go :)


When you type:

quit

(Enter)


the engine will be switched off.


Below I share the source code and compiled Berserk 20220603.

Berserk 20220603_For Linux
.zip
Download ZIP • 99.60MB


A compilation of the Stockfish chess engine.


Stockfish is open source (GPLv3 license).

We will be compiling the latest publicly available version of this engine.


We start similarly to the previous case. We select a directory and download the source code.


1.

Type the command:

git clone https://github.com/official-stockfish/Stockfish.git

Good. The Stockfish engine source code has been downloaded.


2.

Type the command:

cd Stockfish/src


3.

Type the command:

make

Stockfish can be compiled on many different architectures. The make command will list all available architectures.


It is important to choose the correct architecture to which Stockfish should be compiled.

4.

For my architecture, the appropriate compile command is:

make -j build ARCH=armv8

or:

make -j profile-build ARCH=armv8 COMP=gcc

(Faster build with profile-guided optimization, thank you to user kramnik for pointing this out.)



After some time, the compilation is finished.


5.

One more step. Type:

make strip

and confirm with the Enter key.



Perfect!

The Stockfish chess engine is ready to go :)


Let's see if it works by typing:

./stockfish

And then type:

uci

Confirm with the Enter key.


Oh yes, Stockfish 090622 works!


When you type:

quit

(Enter)


The engine will be switched off.


Below I share the source code and compiled Stockfish 090622.

Stockfish 100622_For Linux
.zip
Download ZIP • 65.62MB


Scid vs. PC program compilation


Scid vs. PC is free software licensed under the GNU General Public License version 2.0 (GPLv2).


Scid vs. PC is a program not inferior in functionality to professional database programs such as Chess Assistant and ChessBase.

With two previously compiled chess engines available (Berserk & Stockfish), it would be nice to use them with good software.


If you want to learn how to use this program in various purposes, I invite you, dear reader, to the course area: Scid vs. PC - Efficient work with a chess database

If you decide before compilation that you want to use the already compiled Scid vs. PC prepared for installation in a .DEB package, you can download it from here: https://sourceforge.net/projects/scidvspc/files/linux%20packages/


It is worth noting that the Scid vs. PC developers state that:

"These are third party packages, not compiled by the ScidvsPC team."


So you download at your own risk.

Also, in the above pasted location, there is no .DEB package for the latest version of Scid vs. PC 4.23 ( as of 2022-06-09 ).


Ok, let's get started then!


1.

After selecting the directory where the compilation will take place, we download the Scid vs. PC source code from: http://scidvspc.sourceforge.net/


2.

After unpacking the source, go to the command line and type:

cd scid_vs_pc-4.23/


3.

In the next step, execute the command:

./configure

This is a very important step before compilation begins. There will be a check that all the necessary requirements are met for the compilation of this serious project to be done correctly.


4.

Great.

We have received important information.