Install Zig on Ubuntu 24.04

Install Zig on Ubuntu 24.04

Zig is a modern programming language focused on speed, safety, and simplicity. It gives the full control over low-level details and is great for building reliable system software. This tutorial demonstrates how to install Zig on Ubuntu 24.04.

Install Zig

Get the latest Zig version from GitHub:

ZIG_VERSION=$(curl -s "https://api.github.com/repos/ziglang/zig/releases/latest" | grep -Po '"tag_name": "\K[0-9.]+')

Download the Zig archive:

wget -qO zig.tar.xz https://ziglang.org/download/${ZIG_VERSION}/zig-x86_64-linux-${ZIG_VERSION}.tar.xz

Create installation directory and extract files to it:

sudo mkdir /opt/zig
sudo tar xf zig.tar.xz --strip-components=1 -C /opt/zig

Create a symbolic link to make zig command available system-wide:

sudo ln -s /opt/zig/zig /usr/local/bin/zig

Verify the installation by checking Zig version:

zig version

The tar.xz file is no longer needed, remove it:

rm -rf zig.tar.xz

Testing Zig

Create a main.zig file:

nano main.zig

Paste the following code into main.zig:

const std = @import("std");

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

Compile the Zig file into an executable named test:

zig build-exe main.zig -femit-bin=test

Run the compiled program:

./test

Uninstall Zig

To completely uninstall Zig, remove its installation directory:

sudo rm -rf /opt/zig

Remove symbolic link:

sudo rm -rf /usr/local/bin/zig

We can also remove Zig cache directories:

sudo rm -rf /root/.cache/zig
rm -rf ~/.cache/zig

Leave a Comment

Cancel reply

Your email address will not be published.