Skip to main content

How to Play Chess in the Linux Terminal

How to Play Chess in Linux
(Image credit: Tom's Hardware)

Known to be over 1,200 years old, chess  remains a key test of strategy, mental ability, computational power and is generally a good way of passing the time. If you can represent an eight  by eight grid with suitable symbols for the pieces then you can have a game of chess. So why not play chess in your terminal? 

There are a host of accomplished open source chess engines which any front-end that feels the need can plug into. The most common is one called Stockfish, often described as “the strongest conventional chess engine in the world” and who are we to argue? 

How to Install the Chess Engine

The first thing you’ll want to do is install Stockfish. The version in the repository will be fine, and in these Ubuntu 20.04 days that would be the current v11-1 release. Install by typing:

sudo apt-get install -yl stockfish

The terminal front-end we plumped on is called chs and is written in Python 3. Ideally, you’d install this with the standard Python package manager pip. Install that with sudo apt install python3-pip if you don’t have it already. Oh and of course you’ll need Python3. Ubuntu 18.04+ offers 2 and 3 by default, but run a sudo apt update && sudo apt -y upgrade if you don’t already have it, to ensure you’re all up to date. A quick python3 -V should put your mind at ease.

To install chs you can now run:

python3 -m pip install chs

You might notice a WARNING towards the end of the install, suggesting you add the .local/bin directory to the $PATH environmental variable, else your terminal is going to be left confused as to where chs is. To do this type:

export PATH=”$PATH:$HOME/.local/bin”

Now try launching the game by typing chs at the command prompt. However, you may get this error: Uncaught error “FileNotFoundError”, exiting the app.

This is a known bug in the latest release that may be fixed at some point, but was still a problem at publish time. There’s an easy fix that requires editing a line in the Stockfish config file. Use nano or your favourite text editor to open this file, and replace the phython3.X with the point release you’re running – likely 3.6 or 3.8 (find out by typing python3 -V):

nano ~/.local/lib/python3.X/site-packages/chs/engine/stockfish.py

The file path to the Stockfish executable is incorrect for Linux. Towards the top of the file is an if elif else statement. Change the Linux engine_path declaration from stockfish_10_x64_linux to /usr/games/stockfish. Then press Ctrl + O and Ctrl + X to save and quit.

(Image credit: Tom's Hardware)

Type chs and you’ll find yourself staring at a terminal-based chess board!  

Playing Stockfish Chess 

By default chs plays on the easiest Stockfish level (one), but it’s more than a match for our puny brains. You can up the level with  chs level=8  that has it on the hardest setting, if you fancy a challenge. 

If you’re used to playing chess then you might be aware of chess algebraic notation, and this is how you enter moves in chs. Pieces are known by a single letter – Knight (N), Rook (R), Queen (Q), King (K), Bishop (B) – while Pawns are blank. To make a move you simply enter the piece letter and the destination grid coordinate; chs works out the piece you mean. So for example, an opening Pawn e2 to e4 would simply be e4. To move the King’s Knight you’d use Nf3. Move the King’s Bishop out and with your following move you can castle with O-O (not zeros). You’d castle on the Queen’s side with O-O-O. 

(Image credit: Tom's Hardware)

To capture one of your opponent’s pieces you use an ‘x’ between the piece and the move. So in our imaginary game using the white pawn at e4 to take the d5 pawn you would just write xd5. If it were any other piece you’d start this with the piece’s letter. This brings up potential situations where that move could be completed by more than one white pawn – perhaps if there was one also at c4. To eliminate conflicts you’d include the row letter (or file number, whichever removes the conflict) to the start, so exd5, for this example.  

(Image credit: Tom's Hardware)

A couple of special commands let you cheat, erm, we mean revise your game. The back command takes back your last move(s), as far back as you like, while the hint command gives you a bit of advice from the Stockfish engine. That should be enough to get you up and running – there are some further details such as Queening or En Passant, but you’ll work it out when you need to!  

(Image credit: Tom's Hardware)

The default board is annoyingly small, and the default mono typeface in Ubuntu won’t scale the ISO chess piece images with the terminal type. If you want a larger board you have two options. In the terminal window (provided you are on the desktop), you can select Edit>Preferences>Profile and switch on Custom font and use, as an example, the Ubuntu Mono Regular type, which scales to larger point sizes. Alternatively, use the Menu>Scale option or View->Zoom several times (depending on what your terminal window has)  to simply magnify everything.

(Image credit: Tom's Hardware)

GNU Chess 

You’d be right to think that we’ve missed an obvious chess playing option and that’s the Free Software Foundation’s GNU Chess. This is another terminal-based game, but not as visually pleasing as chs. If this is a problem then there’s a GUI that plugs into the GNU Chess engine and you can pick up things from there. It has the advantage of being GPLv3 licensed and is a middle-aged 36 years old.

GNU Chess  (Image credit: Linux Format)

You’ll find out all about GNU Chess over at www.gnu.org/software/ chess. To get up and running in a terminal use:

sudo apt install gnuchess 

Then start it by simply entering gnuchess at the prompt. 

It uses the same standard algebraic moves as we’ve discussed and will present a board after each move. To get the GUI in place type:

sudo apt install xboard

Enter xboard to fire it up.

Alternatively, the Gnome Chess game you’ll find in the Software Centre of most distros is based on the same GNU Chess engine, which as it turns out has a relatively strong rating of 2,813 Elo points (which means it is a challenging opponent).

This article originally appeared in Linux Format Issue 264.