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
- Go to the official download page at ziglang.org/download and pick the 0.15.x archive for your operating system and processor.
- Extract the archive anywhere you like, for example
~/tools/zig-0.15.2. - Add that folder to your
PATHso thezigcommand 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:
zig version0.15.2
Running a single file
Create a file named hello.zig:
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:
zig run hello.zigHello from a file!
To keep the executable, use zig build-exe instead. It writes a binary named after the file into the current folder:
zig build-exe hello.zig
./helloHello 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 |
zig build-exe hello.zig -O ReleaseFastStarting 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:
mkdir greeter && cd greeter
zig initinfo: 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/.
zig build run # build and run the executable
zig build test # run all tests
zig build -Doptimize=ReleaseSafeEditor 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?
- zig build
- zig run hello.zig
- zig exec hello.zig
- zig init
Which build mode is the default?
- ReleaseFast
- ReleaseSafe
- Debug
- ReleaseSmall
Which file created by zig init contains the build script?
- build.zig
- build.zig.zon
- src/root.zig
- Makefile