Building Zig on Reform

I wanted to try out the Zig programming language, so I installed it on my Reform. I had a little trouble building it, so these are some notes for anyone who might be interested (including myself in the future).

Zig isn’t available in the debian sid repositories right now. It’s a language that’s still under development and changing rapidly, so I decided it was best to build it directly from the source anyway. That way I’m ready to build nightly releases if I want them later.

This guide installs Zig 0.8.1. This is the latest release right now. I noted places where the instructions might be different for other versions.

For official instructions on building Zig, see Building Zig from Source.

1. Install the tools and libraries
Install git, clang and cmake if you don’t have them already. For Zig 0.8.1, install LLVM 12 and related libraries.

$ sudo apt install cmake clang git
$ sudo apt install liblld-12-dev libclang-12-dev libllvm12

Each Zig release is tied to the latest LLVM release. Future Zig releases will need a later version of LLVM. If you want to install the latest Zig, check Building Zig from Source for the LLVM version required for the current Zig version, then change each “12” in the packages above to the correct version of LLVM.

2. Add a swap file
Zig currently needs over 10GB of memory to compile. This is more than the Reform has, so the build will fail. See more information on issue 6485 - zig0 takes too much RAM to build zig1.o and Troubleshooting Build Issues: High memory requirements.

One way to get around this is to add some virtual memory as a swap file. I found that 8GB was enough. After compiling, you can remove the swap file (see step 6 below).

$ sudo fallocate -l 8GB /swapfile
$ sudo chmod 600 /swapfile
$ sudo mkswap /swapfile
$ sudo swapon /swapfile

3. Get the Zig source code
In a folder of your choice, download the Zig source repository.

$ git clone https://github.com/ziglang/zig
$ cd zig

4. Check out the version you want.
To install 0.8.1, checkout the tag with that name.

$ git checkout 0.8.1

If you want the latest nightly build, make sure you’re on the master branch instead, and make sure it’s up to date.

$ git checkout master
$ git pull

5. Build Zig
This will build Zig. Note that “make install” only builds the install structure within the current build folder. It doesn’t attempt to install to any protected locations like /usr/bin. We’ll move the files into place in a later step.

$ mkdir build
$ cd build
$ cmake ..
$ make install

6. Remove the swap file
After the build is successful, you can remove the temporary swap file that we created in step 2.

$ sudo swapoff /swapfile
$ sudo rm /swapfile

7. Make a ~/bin directory, if you don’t have one
I use the ~/bin directory for any software that I build myself. If you don’t have a ~/bin directory, you’ll need to add it to your path after creating it so that you can run programs in that directory. You can reload the default profile file (or log out and log back in) to add it to your path automatically.

$ mkdir ~/bin
$ source ~/.profile

8. Copy the Zig files into place
I chose to put the Zig files into a sub-directory of ~/bin, so that I could have multiple versions installed in parallel. After copying the files, create a symlink to the zig binary.

$ mkdir ~/bin/zig-0.8.1
$ cp bin lib ~/bin/zig-0.8.1
$ ln -s ~/bin/zig-0.8.1/bin/zig ~/bin/zig

9. Make sure it’s working
Run the zig binary to make sure it’s on your path and all the files are in the right place. “zig version” should print the zig version number. “zig targets” should print a long, long list of supported hardware architectures. If either command doesn’t work, zig might not be on your path, or the files might not be installed correctly.

$ zig version
$ zig targets
3 Likes