Install Zig Programming Language on Linux

Zig is an open-source, imperative, statically typed, compiled programming language designed by Andrew Kelley in 2016. Zig’s programming language has made some big claims by its designer that Zig utilizes C libraries better than C itself.

You heard it correctly! First, its own programming language, and it can also compile the C/C++ program from its compiler, which we are going to learn today.

Install Zig Programming Language on Linux

Zig is available in some Linux repositories except Debian and Ubuntu. You can also use PPA (Unofficial maintained), but I would recommend installing it using the snap package manager, or you can also use the source file as shown below.

Snap Package

Ensure that your system has snapd running in the background. After that, execute one of the below commands to install the beta or edge version of the Zig programming language on Linux.

Latest Beta release

$ snap install zig --classic --beta

Latest build from Git Master Branch

$ snap install zig --classic --edge

The direct stable version is not available to install, but you can go with the beta release. I would recommend you stay out of the edge version for the risk of being less stable.

Arch/Manjaro

Arch users can directly install it from the arch repository using the default Pacman package manager, as shown below.

$ pacman -Sy zig

Fedora

The DNF command can install on your system, as shown below.

$ dnf install zig

NixOS

$ nix-env -i zig

Source File

You don’t have to build or compile it, and it already comes in executable format. Just download and create a symbolic link for the binary file at the “/usr/bin” directory to make it executable from anywhere for that user, as shown below.

$ wget https://ziglang.org/builds/zig-linux-x86_64-0.10.0-dev.898+b23f10b42.tar.xz

After download, use the below command to extract the file.

$ tar -xf zig-linux-x86_64-0.10.0-dev.898+b23f10b42.tar.xz

Now enter into the directory and link it to the user bin directory as shown below.

Note: After linking do not remove the source file, otherwise it will be broken link and Zig command will not be executed.

$ cd zig-linux-x86_64-0.10.0-dev.898+b23f10b42
$ sudo ln -s $(pwd)/zig /usr/bin/

Now you can use the below command after the installation to check whether it is working.

$ zig version
0.9.1

Usage

We will create the program in c (I don’t know the Zig programming language) and then compile and run it using the Zig compiler.

Creating C Program

First, we will create a new file with “myprog” and add the “.c” extension at the end, as shown below.

$ nano myprog.c

Inserting Code

Once the file is created, we can add some basic C code. In this case, it will print “Hello TRENDOCEANS” after compilation.

#include <stdio.h>

int main() {
	printf("Hello TRENDOCEANS");
	return 0;
}

Compiling

Save/Close after adding the above code in the file, and then execute the below command to compile.

$ zig cc myprog.c
Hello TRENDOCEANS

If you want to compile the c++ program file, replace the extension with “.cpp” and use the below command.

$ zig c++ myprog.cpp

Below is the help command output, where you can see all other options that can be used along with Zig.

$ zig help
info: Usage: zig [command] [options]

Commands:

  build            Build project from build.zig
  init-exe         Initialize a `zig build` application in the cwd
  init-lib         Initialize a `zig build` library in the cwd

  ast-check        Look for simple compile errors in any set of files
  build-exe        Create executable from source or object files
  build-lib        Create library from source or object files
  build-obj        Create object from source or object files
  fmt              Reformat Zig source into canonical form
  run              Create executable and run immediately
  test             Create and run a test build
  translate-c      Convert C code to Zig code

  ar               Use Zig as a drop-in archiver
  cc               Use Zig as a drop-in C compiler
  c++              Use Zig as a drop-in C++ compiler
  dlltool          Use Zig as a drop-in dlltool.exe
  lib              Use Zig as a drop-in lib.exe
  ranlib           Use Zig as a drop-in ranlib

  env              Print lib path, std path, cache directory, and version
  help             Print this help and exit
  libc             Display native libc paths file or validate one
  targets          List available compilation targets
  version          Print version number and exit
  zen              Print Zen of Zig and exit

General Options:

  -h, --help       Print command-specific usage


Removing Zig Programming Language

If you are done and want to remove it from the system, use one of the below methods depending on which way you used it while installing for your distribution.

Snap package

$ snap remove zig

Arch/Manjaro

$ pacman -Rs zig

Fedora

$ dnf remove zig

Source file

$ rm /usr/bin/zig

I hope this helps!

Leave a Reply