Home

Step-by-Step Guide to Installing and Running Zig on a Raspberry Pi

61 views

Running Zig on a Raspberry Pi can be an exciting project, given the Raspberry Pi's popularity as a versatile small computer and Zig's reputation for its performance and compile-time capabilities. Here’s a step-by-step guide to getting Zig up and running on a Raspberry Pi:

Prerequisites

  • A Raspberry Pi (preferably a Raspberry Pi 4 for better performance)
  • A working installation of Raspberry Pi OS (Raspbian)
  • An Internet connection

Step 1: Update Your System

Before installing Zig, make sure your system is up-to-date:

sudo apt update
sudo apt full-upgrade
sudo reboot

Step 2: Install Required Dependencies

Ensure that you have essential build tools and dependencies:

sudo apt install build-essential git cmake tar wget

Step 3: Download and Install Zig

You can either download a precompiled binary or build Zig from source. Using a precompiled binary is the easiest method.

Method 1: Using Precompiled Binary

  1. Navigate to the Zig download page: ziglang.org/download.

  2. Choose the appropriate version for your Raspberry Pi (ARM architecture) and download it. For example:

    wget https://ziglang.org/download/0.9.0/zig-linux-armv6k.tar.xz
    
  3. Extract the archive:

    tar -xf zig-linux-armv6k.tar.xz
    
  4. Move the Zig directory to a convenient location, for example /opt:

    sudo mv zig-linux-armv6k /opt/zig
    
  5. Add Zig to your PATH by modifying your .bashrc or .zshrc file:

    echo 'export PATH=/opt/zig:$PATH' >> ~/.bashrc
    source ~/.bashrc
    
  6. Verify the Zig installation:

    zig version
    

Method 2: Building from Source

If you prefer to build from source, it can be more time-consuming but ensures you are running the latest code.

  1. Clone the Zig repository:

    git clone https://github.com/ziglang/zig.git
    cd zig
    
  2. Initialize and update the submodules:

    git submodule update --init --recursive
    
  3. Build Zig using CMake:

    mkdir build
    cd build
    cmake ..
    make -j4  # Adjust the number based on your Pi's cores
    
  4. Once the build is complete, add the compiled Zig binary to your PATH as described above.

Step 4: Running Your First Zig Program

Create a simple Zig program to test your installation.

  1. Create a file named hello.zig:

    const std = @import("std");
    
    pub fn main() void {
        std.debug.print("Hello, world!\n", .{});
    }
    
  2. Compile and run the program:

    zig run hello.zig
    

If everything is set up correctly, you should see "Hello, world!" printed to the terminal.

Conclusion

You've successfully installed Zig on your Raspberry Pi and ran your first Zig program. Enjoy exploring the capabilities of Zig on your Raspberry Pi!