Zig Environment Setup
This chapter will guide you through installing and configuring the Zig development environment on different operating systems.
Installing Zig
Method 1: Official Download (Recommended)
Windows
- Visit the Zig official download page
- Download the version suitable for your system (usually
zig-windows-x86_64-*.zip) - Extract to a directory of your choice (e.g.,
C:\zig) - 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 VariablesmacOS
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 ~/.zshrcLinux
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 ~/.bashrcMethod 2: Package Manager Installation
macOS (Homebrew)
bash
brew install zigLinux (Various Distributions)
bash
# Ubuntu/Debian
sudo apt install zig
# Arch Linux
sudo pacman -S zig
# Fedora
sudo dnf install zigWindows (Chocolatey)
powershell
choco install zigMethod 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 installVerify Installation
After installation, verify that Zig is correctly installed:
bash
zig versionYou should see output similar to:
0.12.0Development Environment Configuration
Text Editors and IDEs
Visual Studio Code (Recommended)
- Install VS Code
- 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=4Emacs
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=ReleaseSafeConfigure 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.zonInitialize Project
bash
# Create project directory
mkdir my-zig-project
cd my-zig-project
# Initialize Zig project
zig init-exeHello 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-projectCommon 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.zigBuild Options
bash
# Optimized build
zig build -Doptimize=ReleaseFast
# Debug build
zig build -Doptimize=Debug
# Cross-compile
zig build -Dtarget=x86_64-linux-gnuDebugging Tools
GDB Debugging
bash
# Compile program with debug information
zig build-exe -O Debug main.zig
# Debug with GDB
gdb ./mainLLDB Debugging
bash
# Debug with LLDB
lldb ./mainBuilt-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=nativeExternal 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:
- Check Zig version compatibility
- Read error messages
- Refer to official documentation
Issue 3: ZLS not working
Solution:
- Ensure ZLS is correctly installed
- Check editor configuration
- 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.