TExcelTAD
Home › Zig › Zig Get Started

Zig Get Started

You can follow this whole course in the browser — every ▶ Run button compiles your code with a real Zig 0.15.2 compiler. Sooner or later, though, you will want Zig on your own machine. Installing it is unusually simple: Zig is a single self-contained download with no installer and no dependencies.

Installing Zig

  1. Go to the official download page at ziglang.org/download and pick the 0.15.x archive for your operating system and processor.
  2. Extract the archive anywhere you like, for example ~/tools/zig-0.15.2.
  3. Add that folder to your PATH so the zig command can be found.

Package managers work too:

System Command
macOS (Homebrew) brew install zig
Windows (winget) winget install zig.zig
Linux (snap) snap install zig --classic --beta
Any a version manager such as zvm or anyzig

Tip: Package managers sometimes lag behind or jump ahead. Because the standard library changes between versions, always check which version you got.

Check the installation:

bash · try it yourself
zig version
0.15.2

Running a single file

Create a file named hello.zig:

zig · try it yourself
const std = @import("std");

pub fn main() void {
    std.debug.print("Hello from a file!\n", .{});
}
Hello from a file!

The fastest way to try it is zig run, which compiles the file to a temporary executable and runs it straight away:

bash · try it yourself
zig run hello.zig
Hello from a file!

To keep the executable, use zig build-exe instead. It writes a binary named after the file into the current folder:

bash · try it yourself
zig build-exe hello.zig
./hello
Hello from a file!

Build modes

By default Zig compiles in Debug mode: fast to compile, full safety checks, no optimisation. Pass -O to choose another mode.

Mode Safety checks Speed Typical use
Debug yes slow development (default)
ReleaseSafe yes fast production code that must not misbehave silently
ReleaseFast no fastest benchmarks, games
ReleaseSmall no small binary embedded, WebAssembly
bash
zig build-exe hello.zig -O ReleaseFast

Starting a project with zig init

For anything bigger than one file, create a project. zig init writes a template with a build script, an executable and a small library module:

bash · try it yourself
mkdir greeter && cd greeter
zig init
info: created build.zig
info: created build.zig.zon
info: created src/main.zig
info: created src/root.zig
info: see `zig build --help` for a menu of options
File Purpose
build.zig the build script, written in Zig
build.zig.zon package metadata and dependencies (ZON = Zig Object Notation)
src/main.zig the program's entry point
src/root.zig a library module that main.zig imports

Build and run it with zig build run. The compiled program ends up in zig-out/bin/, and build caches live in .zig-cache/.

bash
zig build run       # build and run the executable
zig build test      # run all tests
zig build -Doptimize=ReleaseSafe

Editor support

Most editors support Zig through ZLS, the Zig Language Server, which gives autocompletion, go-to-definition and inline errors. Pick the ZLS release that matches your Zig version. zig fmt file.zig formats code into the official style — run it often, as nearly all Zig code in the wild uses that style.

Test yourself

Which command compiles a file and runs it immediately without keeping the executable?

  1. zig build
  2. zig run hello.zig
  3. zig exec hello.zig
  4. zig init

Which build mode is the default?

  1. ReleaseFast
  2. ReleaseSafe
  3. Debug
  4. ReleaseSmall

Which file created by zig init contains the build script?

  1. build.zig
  2. build.zig.zon
  3. src/root.zig
  4. Makefile