Skip to content

Zig Environment Setup

This chapter will guide you through installing and configuring the Zig development environment on different operating systems.

Installing Zig

Windows

  1. Visit the Zig official download page
  2. Download the version suitable for your system (usually zig-windows-x86_64-*.zip)
  3. Extract to a directory of your choice (e.g., C:\zig)
  4. Add the Zig directory to your system PATH environment variable

Steps to add to PATH:

cmd
# Temporary addition (valid for current session)
set PATH=%PATH%;C:\zig

# Permanent addition: System Settings -> Advanced System Settings -> Environment Variables

macOS

bash
# Download and extract
curl -O https://ziglang.org/download/0.12.0/zig-macos-aarch64-0.12.0.tar.xz
tar -xf zig-macos-aarch64-0.12.0.tar.xz

# Move to system directory
sudo mv zig-macos-aarch64-0.12.0 /usr/local/zig

# Add to PATH
echo 'export PATH="/usr/local/zig:$PATH"' >> ~/.zshrc
source ~/.zshrc

Linux

bash
# Download and extract
wget https://ziglang.org/download/0.12.0/zig-linux-x86_64-0.12.0.tar.xz
tar -xf zig-linux-x86_64-0.12.0.tar.xz

# Move to system directory
sudo mv zig-linux-x86_64-0.12.0 /usr/local/zig

# Add to PATH
echo 'export PATH="/usr/local/zig:$PATH"' >> ~/.bashrc
source ~/.bashrc

Method 2: Package Manager Installation

macOS (Homebrew)

bash
brew install zig

Linux (Various Distributions)

bash
# Ubuntu/Debian
sudo apt install zig

# Arch Linux
sudo pacman -S zig

# Fedora
sudo dnf install zig

Windows (Chocolatey)

powershell
choco install zig

Method 3: Compile from Source

If you need the latest development version:

bash
git clone https://github.com/ziglang/zig.git
cd zig
mkdir build
cd build
cmake .. -DCMAKE_BUILD_TYPE=Release
make install

Verify Installation

After installation, verify that Zig is correctly installed:

bash
zig version

You should see output similar to:

0.12.0

Development Environment Configuration

Text Editors and IDEs

  1. Install VS Code
  2. Install Zig extension:
    • Open Extensions panel (Ctrl+Shift+X)
    • Search for "Zig Language"
    • Install the official Zig extension

Recommended Settings:

json
{
    "zig.path": "zig",
    "zig.zls.path": "zls",
    "zig.initialLogLevel": "info"
}

Vim/Neovim

vim
" Install zig.vim plugin
Plug 'ziglang/zig.vim'

" Basic configuration
autocmd BufNewFile,BufRead *.zig set filetype=zig
autocmd FileType zig setlocal shiftwidth=4 tabstop=4

Emacs

elisp
;; Install zig-mode
(use-package zig-mode
  :ensure t)

Other Editors

  • Sublime Text: Install Zig syntax highlighting package
  • Atom: Install language-zig package
  • IntelliJ IDEA: Install Zig plugin

Zig Language Server (ZLS)

ZLS provides features like code completion, error checking, and jump to definition.

Install ZLS

bash
# Compile and install from source
git clone https://github.com/zigtools/zls.git
cd zls
zig build -Doptimize=ReleaseSafe

Configure ZLS

Create a .zls.json configuration file:

json
{
    "enable_snippets": true,
    "enable_ast_check_diagnostics": true,
    "enable_autofix": true,
    "enable_import_embedfile_argument_completions": true,
    "warn_style": true,
    "highlight_global_var_declarations": true
}

Creating Your First Zig Project

Project Structure

my-zig-project/
├── src/
│   └── main.zig
├── build.zig
└── build.zig.zon

Initialize Project

bash
# Create project directory
mkdir my-zig-project
cd my-zig-project

# Initialize Zig project
zig init-exe

Hello World Program

Create src/main.zig:

zig
const std = @import("std");

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

Compile and Run

bash
# Build
zig build

# Run
zig run src/main.zig

# Or run the build result directly
./zig-out/bin/my-zig-project

Common Zig Commands

Basic Commands

bash
# Run Zig file
zig run main.zig

# Compile to executable
zig build-exe main.zig

# Compile to library
zig build-lib main.zig

# Test
zig test main.zig

# Format code
zig fmt main.zig

# Check syntax
zig ast-check main.zig

Build Options

bash
# Optimized build
zig build -Doptimize=ReleaseFast

# Debug build
zig build -Doptimize=Debug

# Cross-compile
zig build -Dtarget=x86_64-linux-gnu

Debugging Tools

GDB Debugging

bash
# Compile program with debug information
zig build-exe -O Debug main.zig

# Debug with GDB
gdb ./main

LLDB Debugging

bash
# Debug with LLDB
lldb ./main

Built-in Debugging

Zig provides built-in debugging features:

zig
const std = @import("std");

pub fn main() void {
    std.debug.print("Debug info: {}\n", .{42});

    // Assert
    std.debug.assert(1 + 1 == 2);

    // Panic (program terminates)
    // std.debug.panic("Error occurred!", .{});
}

Performance Profiling

Built-in Performance Profiling

bash
# Enable performance profiling
zig build -Doptimize=ReleaseFast -Dcpu=native

External Tools

  • Valgrind (Linux): Memory checking
  • Instruments (macOS): Performance profiling
  • Intel VTune: CPU performance profiling

Troubleshooting Common Issues

Issue 1: zig command not found

Solution: Check if PATH environment variable is set correctly

Issue 2: Compilation errors

Solution:

  1. Check Zig version compatibility
  2. Read error messages
  3. Refer to official documentation

Issue 3: ZLS not working

Solution:

  1. Ensure ZLS is correctly installed
  2. Check editor configuration
  3. Restart editor

Summary

Now you have successfully configured the Zig development environment! You should be able to:

  • ✅ Install and run the Zig compiler
  • ✅ Configure code editor
  • ✅ Create and run Zig programs
  • ✅ Use basic Zig commands

In the next chapter, we'll learn Zig's basic syntax and begin our actual programming journey.

Content is for learning and research only.