How to install Stockfish on Mac M2?

Get your specific HIARCS/Junior support questions answered here as well as up-to-the-minute news!

Moderators: Watchman, Mark Uniacke, mrudolf

e4-homie
Member
Posts: 29
Joined: Fri Sep 01, 2023 10:52 pm

How to install Stockfish on Mac M2?

Post by e4-homie »

I followed the instructions provided by mrudolf (viewtopic.php?p=101259#p101259):
1. Goto https://brew.sh/ and follow the instructions to install Homebrew.
2. Open terminal and type: brew install stockfish
I installed brew from their website. Then ran: brew install stockfish, but the terminal output is:

Code: Select all

zsh: command not found: brew
e4-homie
Member
Posts: 29
Joined: Fri Sep 01, 2023 10:52 pm

Re: How to install Stockfish on Mac M2?

Post by e4-homie »

Resolved. Reading is fundamental...Terminal shows two additional commands to enter, described below

Code: Select all

- Run these two commands in your terminal to add Homebrew to your PATH:
    (echo; echo 'eval "$(/opt/homebrew/bin/brew shellenv)"') >> /Users/dj2/.zprofile
    eval "$(/opt/homebrew/bin/brew shellenv)"
ujo
Member
Posts: 7
Joined: Wed Nov 03, 2021 3:02 pm

Re: How to install Stockfish on Mac M2?

Post by ujo »

My way:
Install Apple developer tools (Xcode)
Download SF e.g. https://github.com/official-stockfish/S ... /sf_16.zip or git clone or whatever you prefer to get the sourcecode
Extract the zip
Enter the src directory
make profile-build COMP=clang ARCH=apple-silicon
Enjoy your own just self build binary!
Ruyter
Member
Posts: 70
Joined: Sat Aug 21, 2021 6:42 am

Re: How to install Stockfish on Mac M2?

Post by Ruyter »

without Xcode
open terminal
type cc and return
install command line tools
and after use the make file in a terminal
make build ARCH=apple-silicon
Want to play a game ?
Im colfax64 on Lichess also
HiarcsApple
Member
Posts: 33
Joined: Sat Dec 09, 2023 10:16 pm

Re: How to install Stockfish on Mac M2?

Post by HiarcsApple »

e4-homie wrote: Sat Sep 16, 2023 5:45 pm I followed the instructions provided by mrudolf (viewtopic.php?p=101259#p101259):
1. Goto https://brew.sh/ and follow the instructions to install Homebrew.
2. Open terminal and type: brew install stockfish
I installed brew from their website. Then ran: brew install stockfish, but the terminal output is:

Code: Select all

zsh: command not found: brew
You need Terminal + Github Stockfish Source Code :D

1.download the code (zip) and unpack it: https://github.com/official-stockfish/Stockfish
2.open Finder and push your download (stockfish-master) from you download folder to your user folder (the tiny house icon)
3.open Terminal and write: cd /Users/me/Stockfish-master/src
It's the path to the src which you have downloaded.
You only need to change "me"
Or go into the src folder and copy and paste the path into Terminal.
4.hit Enter
5.copy and paste: make -j profile-build COMP=clang ARCH=apple-silicon
6.hit Enter
7.copy and paste: ./stockfish compiler
8.hit Enter
9.now go into src and you will see Stockfish exec (black / green icon)
10.open Hiarcs Chess Explorer PRO, use the path to select Stockfish exec and have fun.

:arrow: It should be around 2.3x faster than brew
e4-homie
Member
Posts: 29
Joined: Fri Sep 01, 2023 10:52 pm

Re: How to install Stockfish on Mac M2?

Post by e4-homie »

Thanks, HiarcsApple! When you say it is ~2x faster than Brew, do you mean the number of positions Stockfish can analyze? If so, I will try out your detailed instructions. Thank you so much.
gads
Member
Posts: 33
Joined: Wed Nov 29, 2023 9:40 am

Re: How to install Stockfish on Mac M2?

Post by gads »

e4-homie wrote: Fri Dec 15, 2023 1:52 am Thanks, HiarcsApple! When you say it is ~2x faster than Brew, do you mean the number of positions Stockfish can analyze? If so, I will try out your detailed instructions. Thank you so much.
I think he means NPS
HiarcsApple
Member
Posts: 33
Joined: Sat Dec 09, 2023 10:16 pm

Re: How to install Stockfish on Mac M2?

Post by HiarcsApple »

e4-homie wrote: Fri Dec 15, 2023 1:52 am Thanks, HiarcsApple! When you say it is ~2x faster than Brew, do you mean the number of positions Stockfish can analyze? If so, I will try out your detailed instructions. Thank you so much.
(brew install stockfish --HEAD = 4 Mn/s)
(make build = 5 Mn/s)
(make -j profile-build ARCH=apple-silicon = 9 Mn/s)
(make profile-build COMP=clang ARCH=apple-silicon = 9 Mn/s)
(make -j profile-build COMP=clang ARCH=apple-silicon = 9.3 Mn/s)
User avatar
SedarPL
Member
Posts: 69
Joined: Wed Mar 15, 2017 3:17 pm
Location: Poland
Contact:

Re: How to install Stockfish on Mac M2?

Post by SedarPL »

https://www.chessengeria.eu/files

You can find many chess engines, include Stockfish. All for free.
Regards, Darius
https://chessengeria.eu
e4-homie
Member
Posts: 29
Joined: Fri Sep 01, 2023 10:52 pm

Re: How to install Stockfish on Mac M2?

Post by e4-homie »

Analyzing the same middlegame position, there is a difference between the Brew installation vs. building from source. Stockfish 16 with Brew results in 4.4 M nps whereas Stockfish 16 built from source, per HiarcsApple's instructions above, results in 9.1 M nps.
HiarcsApple
Member
Posts: 33
Joined: Sat Dec 09, 2023 10:16 pm

Re: How to install Stockfish on Mac M2?

Post by HiarcsApple »

e4-homie wrote: Fri Dec 15, 2023 3:16 pm Analyzing the same middlegame position, there is a difference between the Brew installation vs. building from source. Stockfish 16 with Brew results in 4.4 M nps whereas Stockfish 16 built from source, per HiarcsApple's instructions above, results in 9.1 M nps.
Merry Christmas :mrgreen:

Your results are fine.

The results will be often slightly different due to many reasons like:
Hardware (M1 vs M1 PRO vs M1 MAX vs M2 vs... vs M3 vs... / 32 vs 64 vs 128 GB RAM, RAM speed / tested on different positions like different middlegame positions and different time used / Stockfish support ARMv8, ARMv8.5, ARMv9, ARMv9.2 later ARMv9.5....)
Note that the net size which Stockfish is using also changed and will change in the future very often (hardware gets stronger = net size gets bigger).
I hope that you've tested Stockfish 16 brew which is indeed Stockfish 16 and the correct Stockfish 16 build from source, not the newest Stockfish-master which is the so called Stockfish 17 development version.

Brew installations was the first shot from Stockfish developers to have an easy and fast way to run Stockfish on new Apple M1 devices.

You can also run a benchmark on your hardware using Stockfish exec (double click on the icon and type the bench... and hit enter:
10 is the number of the CPU cores, so you need to change it to the number of the cpu cores you have like 8 or 10 or 12 or 16 or to 1 if you want the results of 1 core.
Stockfish Developer: bench 16 10 13 default depth
Ipman chess Stockfish 14.1: bench 1024 10 26 default depth nnue
https://ipmanchess.yolasite.com/amd--in ... ckfish.php

Note that it doesn't make much sense to compare Apple vs Intel and AMD results with old Stockfish 14.1 due to many reasons like:
The Stockfish net size was doubled = you will see half of the speed in reality.
Apple is using ARM cores and Intel+AMD doesn't.
Stockfish speed improvements on Intel+AMD devices are not really available in the last years, because the developers have done probably all possible speed improvements since Stockfish 1 and finished some years ago. At the beginning of the development / search for speed improvements, you saw often a speedup rain every day. Of course the speed improvements become smaller and smaller over the years.
Now the Stockfish developers could try to find speed improvements when using different Apple ARM CPUs. (But unfortunately at the moment only one of them has an Apple MacBook).
It wouldn't wonder me when they improved Stockfishs speed on Intel and AMD CPUs by a total of 500% or even 1000% in the last 15 years.
So now you know what we can expect from ARM CPUs.
...

Example:
Implement AffineTransformSparseInput for armv8
Implements AffineTransformSparseInput layer for the NNUE evaluation
for the armv8 and armv8-dotprod architectures. We measured some nice
speed improvements via 10 runs of our benchmark:

armv8, Cortex-X1 : 18.5% speed-up
armv8, Cortex-A76 : 13.2% speed-up
armv8-dotprod, Cortex-X1 : 27.1% speed-up
armv8-dotprod, Cortex-A76 : 12.1% speed-up
armv8, Cortex-A72, Raspberry Pi 4 : 8.2% speed-up (thanks Torom!)
https://github.com/Joachim26/StockfishN ... 8b571223bf
https://github.com/official-stockfish/S ... /pull/4719


Feel free to compare:
Author: AndrovT
Date: Sun Aug 6 21:22:37 2023 +0200
Timestamp: 1691349757

Implement AffineTransformSparseInput for armv8

Implements AffineTransformSparseInput layer for the NNUE evaluation
for the armv8 and armv8-dotprod architectures. We measured some nice
speed improvements via 10 runs of our benchmark:

armv8, Cortex-X1 : 18.5% speed-up
armv8, Cortex-A76 : 13.2% speed-up
armv8-dotprod, Cortex-X1 : 27.1% speed-up
armv8-dotprod, Cortex-A76 : 12.1% speed-up
armv8, Cortex-A72, Raspberry Pi 4 : 8.2% speed-up (thanks Torom!)

closes https://github.com/official-stockfish/S ... /pull/4719

No functional change
see source
https://abrok.eu/stockfish/?page=6

and

Author: ppigazzini
Date: Sun Aug 6 21:17:33 2023 +0200
Timestamp: 1691349453

Add new CPU archs in CI Tests workflow

Add CPU archs: armv8-dotprod, riscv64 and ppc64le.
The last two archs are built using QEMU multiarch docker container.

References:
https://docs.docker.com/build/building/multi-platform/
https://github.com/docker/setup-buildx-action
https://github.com/docker/setup-qemu-action
https://github.com/tonistiigi/binfmt
https://stackoverflow.com/questions/724 ... a-containe

closes https://github.com/official-stockfish/S ... /pull/4718

No functional change
see source

Results:

NEW:
Total time (ms) : 1067
Nodes searched : 1350831
Nodes/second : 1266008

vs

OLD:
Total time (ms) : 1874
Nodes searched : 1350831
Nodes/second : 720827

https://github.com/official-stockfish/S ... /pull/4719
That's a speed up of 75.63%.
They also had a +5% speed improvement before.
It's much faster compared to non Apple ARM CPUs.
It happens some time after they doubled the net size.
Now you know how difficult it is to compare Apple vs Intel and AMD CPUs at ipmanchess with very old Stockfish vs newest Stockfish and newest hardware.
HiarcsApple
Member
Posts: 33
Joined: Sat Dec 09, 2023 10:16 pm

Re: How to install Stockfish on Mac M2?

Post by HiarcsApple »

HiarcsApple wrote: Sat Dec 09, 2023 10:59 pm
How to install Stockfish on Apple M1, M1 PRO, M1 MAX, M2..., M3...

You need Terminal + Github Stockfish Source Code :D

1.download the code (zip) and unpack it: https://github.com/official-stockfish/Stockfish
2.open Finder and push your download (stockfish-master) from you download folder to your user folder (the tiny house icon)
3.open Terminal and write: cd /Users/me/Stockfish-master/src
It's the path to the src which you have downloaded.
You only need to change "me"
Or go into the src folder and copy and paste the path into Terminal.
4.hit Enter
5.copy and paste: make -j profile-build COMP=clang ARCH=apple-silicon
6.hit Enter
7.copy and paste: ./stockfish compiler
8.hit Enter
9.now go into src and you will see Stockfish exec (black / green icon)
10.open Hiarcs Chess Explorer PRO, use the path to select Stockfish exec and have fun.

:arrow: It should be around 2.3x faster than brew
How to install LC0 on Apple M1, M1 PRO, M1 MAX, M2..., M3...

GPU using Metal:
1.Download LC0-master Code: https://github.com/LeelaChessZero/lc0
2.Download LC0 net BT4-1024x15x32h-swa-2745000.pb.gz https://drive.google.com/file/d/1vL4CZg ... L4GuG/view
(or another net from here: https://training.lczero.org/networks/?show_all=0)
3.Push it into the LC0 folder.
4. cd /Users/me/lc0-master/src
5. ./build.sh

After that you can use in the same window:
lc0 benchmark
and
lc0 backendbench

I pushed the BT4 net into the build folder, into the release folder where my LC0 exec is.

Note that you should open LC0 settings in the Deep HIARCS Chess Explorer Pro Premium and change: Threads = 2 -> Threads = 1
This will make LC0 nearly 40% faster on Apple devices.

Note to use BT4 nets you will need: Multihead arch metal #1916: https://github.com/LeelaChessZero/lc0/pull/1916 from almaudoh:multihead-arch-metal: https://github.com/almaudoh/lc0/tree/mu ... arch-metal here you can download the code. This is step 1.
See step 4. I've mentioned above. You need to change cd /Users/me/lc0-master/src into cd /Users/me/lc0-multihead-arch-metal/src
-> You probably need to do this only once in your live, because it will take some more days for the LC0 developers to push Multihead arch metal #1916 into the official LC0-Master.
Alexander
Member
Posts: 26
Joined: Thu Jun 08, 2023 9:47 pm

Re: How to install Stockfish on Mac M2?

Post by Alexander »

Thank you for this advice. Stockfish is running much faster on my oldish intel mac machine now. Previously I got about 600-700k nodes/sec, now I get 1-1.2 million. Makes a big difference.
e4-homie
Member
Posts: 29
Joined: Fri Sep 01, 2023 10:52 pm

Re: How to install Stockfish on Mac M2?

Post by e4-homie »

Agreed - Significant speed improvement!
Oceanblue31
Posts: 1
Joined: Mon Apr 01, 2024 4:04 am

Re: How to install Stockfish on Mac M2?

Post by Oceanblue31 »

I've tried this and get this prompt in the terminal: zsh: bad CPU type in executable:

When I try to add stockfish using the exec file in SRC a popup says the provided path is not a valid UCI engine.

Help?
Post Reply