Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Zig Environment Installation

Zig Environment Installation

When configuring the development environment for the Zig programming language, you need to install the Zig compiler and set up the relevant development tools.

The following are the steps to configure Zig on different operating systems:

Install using a package manager

Windows

Zig is available on Chocolatey:

choco install zig

Windows (winget)



winget install zig.zig

Windows (scoop):

scoop install zig

MacOS

Homebrew installation:

brew install zig

Linux

Ubuntu (snap)

Stable version installation:

snap install zig --classic --beta

Fedora:



dnf install zig

FreeBSD:



pkg install lang/zig

After the configuration is complete, you can start developing with the Zig programming language.

Installing from source

We can also download the source code to compile and install.

Zig source package download address:https://ziglang.org/zh/download/.

After downloading, use the tar command to extract:



tar -xvf zig-macos-aarch64-0.13.0.tar.xz

Then enter the source package to perform the subsequent compilation and installation:

cd zig-macos-aarch64-0.13.0

You can also clone the source code from Zig's GitHub repository:



git clone https://github.com/ziglang/zig.git

Dependencies

- Can be installed using the system package manager, or built from source.

Instruction

  1. In the Zig source directory, create a build directory:

    mkdir build
    
    

cd build 2.

Run cmake:

cmake ..
3.

Build and install Zig:

make install

Please note CMAKE_PREFIX_PATH this convenient cmake variable. CMake will prioritize searching for LLVM and other dependencies in this location.

These steps will generate stage3/bin/zig, which is the Zig compiler built by Zig itself.

macOS + Homebrew

For macOS, use Homebrew:

  1. In the Zig source directory, create a build directory:

    mkdir build
    
    

cd build 2.

Run cmake with static LLVM enabled:

cmake .. -DZIG_STATIC_LLVM=ON -DCMAKE_PREFIX_PATH="$(brew --prefix llvm@18);$(brew --prefix zstd)"

3.

Build and install Zig:

make install

FreeBSD

For FreeBSD:

  1. Use pkg to install the necessary dependencies:

    sudo pkg install -qyr FreeBSD devel/llvm18 devel/cmake archivers/zstd textproc/libxml2 archivers/lzma
    
    
2.

In the Zig source directory, create a build directory:

mkdir build

cd build

3.

Run cmake with static LLVM enabled:

cmake .. -DZIG_STATIC_LLVM=ON -DCMAKE_PREFIX_PATH="/usr/local/llvm18;/usr/local"

4.

Build and install Zig:

make install

These steps will install the Zig compiler on your system.