I’ve been watching the Zig language for a while now. Like some other things I’m interested in, the phrase “good engineering” seems appropriate. The syntax hasn’t stabilized yet, so this session focuses on the 0.9.1 version of the project. Far as I know, anything is subject to change.

manual installation

First things first. Let’s get ourselves downloaded and installed. Download a copy of Zig from their downloads page and extract it.

mkdir ~/zig
cd ~/zig
wget https://ziglang.org/download/0.9.1/zig-linux-x86_64-0.9.1.tar.xz
tar -xf zig-linux-x86_64-0.9.1.tar.xz

Now you can either add that location to your PATH, or you can just put the binary somewhere already on your PATH.

What’s your PATH? It’s the road of life laid before you, which you, oh weary traveler, must trod until the end of your days. May it bring you peace.

Actually, it’s how you’re able to run programs directly from the commandline without specifying their location. Ever think about how you can do cd ~/zig without having to say where the cd program is located? And no, it’s not some builtin thing that just “comes with” your terminal program. Try whereis cd sometime. It’s fun.

Now that we’ve extracted zig, we’re just going to skip adding it to our path. That’s annoying, because it’s another folder we have to remember not to move, and we have to manually edit files. Let’s just dump zig into /usr/local/bin and add a line to our system install script to do the whole thing automatically next time.

You have a system install script, don’t you? A little script that you run when you set up a new machine, that gets everything set up just the way you like it? I love it. Doesn’t require a lot of maintanence if you do it right, and it doesn’t ever have to be totally manual either. Just enough automation that it holds your hand while you get it all running. (I guess, in a sense, it’s your younger self holding your older self’s hand. Now there’s a strange thought.)

sudo install ~/zig/zig-linux-x86_64-0.9.1/zig /usr/local/bin/zig

Then we can just run zig from anywhere: zig build

I don’t think we need the lib folder that goes with the binary. If we do, there’s two options: dump the entire ~/zig/zig-linux-x86_64-0.9.1 folder into /usr/local/bin, or add the folder to the PATH; or just install from the OS package manager.

package manager installation

What OS are you on? I use Void Linux, though I’m working on transitioning to FreeBSD.

# void
sudo xbps-install zig

# freebsd
doas pkg install -y xorg

hello world

Verify the version of zig you installed. Note that Void seems to be running behind; might be this is a good time to be jumping off that ship. I think in the future Alping and FreeBSD might be my go-tos…a nice minimal linux, and a longstanding BSD.

zig version

Now let’s make our first zig program. Create a file called main.zig and put the following inside:

const std = @import("std");

pub fn main() void {
    std.debug.print("Hello, {s}!\n", .{"World"});
}

Then build and run it.

zig run main.zig