Cyberithub

How to Install and Use Zig Programming Language on Ubuntu or Debian Linux

Advertisements

In this article, we will see how to install and use zig programming language on Ubuntu or Debian based linux systems. If you are looking for an alternative of C programming language with better features like built-in safety, ideal cross-compilation, high performance etc. then zig programming language is the one to choose. It is great for performance oriented applications such as for game designing, embedded os development, networking application and for other similar kind of applications. It is famous for providing safety mechanism to prevent any sort of memory corruption.

For error handling, zig programming language uses error unions instead of exceptions. It does not employ any garbage collector like other programming languages, instead it provides explicit memory allocation/deallocation using standard allocators. It has so many other features that I can go on and on. But for now, you can check all its features on its official website. What we are going to do here is that we are going to learn how to install and use this programming language on ubuntu or debian based linux platforms.

 

How to Install and Use Zig Programming Language on Ubuntu or Debian Linux

How to Install and Use Zig Programming Language on Ubuntu or Debian Linux

Also Read: How to Install nvidia-smi on Ubuntu or Debian Linux

Step 1: Prerequisites

a) You should have a running Ubuntu or Debian Linux Server.

b) You should have sudo or root access to run privileged commands.

c) You should have snap utility installed in your system in case you would like to install zig utility from snap store.

d) You should also have wget utility available in your server.

 

Step 2: Update Your Server

Let's first check and install all latest available package updates and bugfixes using sudo apt update && sudo apt upgrade command as shown below. This will keep your system stable and secure.

cyberithub@ubuntu:~$ sudo apt update && sudo apt upgrade

 

Step 3: Install Zig 

There are multiple ways to install zig utility on Ubuntu or Debian based linux systems. You can choose any of the below methods depending on your requirements.

a) Using Snap

You can install zig through package manager such as snap by using sudo snap install zig --classic --beta command as shown below. To know more about snap utility, check 36 Popular Snap command examples in Linux for Beginners

cyberithub@ubuntu:~$ sudo snap install zig --classic --beta
zig (beta) 0.14.0 from Jay Petacat (jayschwa) installed

b) Using Pre-built binary

You have to visit the download page and grab the latest tarball using wget utility as shown below.

cyberithub@ubuntu:~$ wget https://ziglang.org/builds/zig-linux-x86_64-0.15.0-dev.45+24db007cd.tar.xz
--2025-03-13 02:33:15-- https://ziglang.org/builds/zig-linux-x86_64-0.15.0-dev.45+24db007cd.tar.xz
Resolving ziglang.org (ziglang.org)... 65.109.105.178, 2a01:4f9:3051:4bd2::2
Connecting to ziglang.org (ziglang.org)|65.109.105.178|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 49100292 (47M) [application/octet-stream]
Saving to: ‘zig-linux-x86_64-0.15.0-dev.45+24db007cd.tar.xz’

zig-linux-x86_64-0.15.0-dev.45+2 100%[==========================================================>] 46.83M 2.05MB/s in 23s

2025-03-13 02:33:38 (2.08 MB/s) - ‘zig-linux-x86_64-0.15.0-dev.45+24db007cd.tar.xz’ saved [49100292/49100292]

Then extract the tarball by using tar -xf zig-linux-x86_64-0.15.0-dev.45+24db007cd.tar.xz command as shown below. To know more about tar command, check 20+ Practical tar command examples for Linux Administrators

cyberithub@ubuntu:~$ tar -xf zig-linux-x86_64-0.15.0-dev.45+24db007cd.tar.xz

Switch to extracted directory using cd zig-linux-x86_64-0.15.0-dev.45+24db007cd command as shown below.

cyberithub@ubuntu:~$ cd zig-linux-x86_64-0.15.0-dev.45+24db007cd

You can find the pre-built binary file here which you can verify by running ./zig version command as shown below.

cyberithub@ubuntu:~/zig-linux-x86_64-0.15.0-dev.45+24db007cd$ ./zig version
0.15.0-dev.45+24db007cd

c) Using Source Code

You have to first download the latest source code from GitHub repo using git clone https://github.com/ziglang/zig.git command as shown below.

cyberithub@ubuntu:~$ git clone https://github.com/ziglang/zig.git
Cloning into 'zig'...
remote: Enumerating objects: 311563, done.
remote: Counting objects: 100% (231/231), done.
remote: Compressing objects: 100% (87/87), done.
remote: Total 311563 (delta 174), reused 148 (delta 144), pack-reused 311332 (from 3)
Receiving objects: 100% (311563/311563), 314.89 MiB | 4.97 MiB/s, done.
Resolving deltas: 100% (237173/237173), done.
Updating files: 100% (17092/17092), done.

Switch to cloned directory using cd zig command as shown below.

cyberithub@ubuntu:~$ cd zig

Then create a directory called build using mkdir build command as you can see below.

cyberithub@ubuntu:~/zig$ mkdir build

Switch to directory using cd build command as shown below.

cyberithub@ubuntu:~/zig$ cd build/

Then run cmake .. command.

cyberithub@ubuntu:~/zig/build$ cmake ..
-- The C compiler identification is GNU 11.4.0
-- The CXX compiler identification is GNU 11.4.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring zig version 0.15.0-dev.56+d0911786c
..............................

After building the source code, you just have to install the binaries by using make install command as shown below.

cyberithub@ubuntu:~/zig/build$ make install

 

Step 4: Check Version

You can check the current installed version by using zig version command as shown below.

cyberithub@ubuntu:~$ zig version
0.14.0

 

Step 5: Initialize a Project

Now that zig utility is installed, let's initialize a sample zig project and understand how to work with this programming language. let's first create a directory called cyberithub-example, switch to it and then create the project structure by using zig init command as shown below.

cyberithub@ubuntu:~$ mkdir cyberithub-example
cyberithub@ubuntu:~$ cd cyberithub-example/
cyberithub@ubuntu:~/cyberithub-example$ zig init
info: created build.zig
info: created build.zig.zon
info: created src/main.zig
info: created src/root.zig
info: see `zig build --help` for a menu of options

To build the project, run zig build command as shown below. This will compile the source code in main.zig based on build.zig file and create an executable under zig-out/bin directory.

cyberithub@ubuntu:~/cyberithub-example/src$ zig build

To execute the binary generated, switch back to cyberithub-example directory and then  run ./zig-out/bin/cyberithub_example command as shown below. You will see project binary is getting executed without any error. This confirms zig programming language is working as expected in your system.

cyberithub@ubuntu:~/cyberithub-example$ ./zig-out/bin/cyberithub_example
All your codebase are belong to us.
Run `zig build test` to run the tests.

 

Step 6: Uninstall Zig

Once you are done using zig programming language in your system, you can choose to remove zig utility from your system by any of the below methods depending on how you installed it.

a) Using snap

In case, you installed zig through snap utility then for removal run sudo snap remove zig command as shown below. This will remove the utility from your system.

cyberithub@ubuntu:~$ sudo snap remove zig
zig removed

b) Using Prebuilt binary

In case, you have been using pre-built binary utility then for removal, you just have to delete the binary from location where you kept it. In our case, we kept the utility in our extracted directory so to remove the utility we just deleted it from there as you can see below.

cyberithub@ubuntu:~/zig-linux-x86_64-0.15.0-dev.45+24db007cd$ rm -rf zig

c) Using Source code

If you installed zig utility from source code then for removal you have to manually delete all the installed files by using below removal commands. This should remove all zig related files from your system.

cyberithub@ubuntu:~$ sudo rm -rf /usr/local/bin/zig
cyberithub@ubuntu:~$ sudo rm -rf /usr/local/lib/zig
cyberithub@ubuntu:~$ sudo rm -rf /usr/local/include/zig
cyberithub@ubuntu:~$ sudo rm -rf /usr/local/share/zig

Leave a Comment