Skip to content

Instantly share code, notes, and snippets.

@taikedz
Last active September 1, 2023 10:36
Show Gist options
  • Save taikedz/7ff43b6f956fb9dcff1c7da3a40d1514 to your computer and use it in GitHub Desktop.
Save taikedz/7ff43b6f956fb9dcff1c7da3a40d1514 to your computer and use it in GitHub Desktop.
Install ZIG

Install Zig

A basic script to pull the latest master, or specified version, of zig

bash install-zig.sh # installs master build as 'zig'
bash install-zig.sh 0.11.0 # installs latest build for this version as 'zig-0.11.0'

# This command is always the latest-installed master build
zig --help

# All others are versioned builds
zig-0.11.0 --help

Data

Actual binaries are stored in ~/.local/var/zig

Executable is symlinked to ~/.local/bin

#!/usr/bin/env bash
mkdir -p ~/.local/var/zig
cd ~/.local/var/zig
arch="$(uname -m)"
zigtag="${1:-master}"
if [[ "$zigtag" != master ]]; then
zigcmd="zig-${zigtag}"
else
zigcmd=zig
fi
get_zig_url_py="
import json
import sys
data = json.load(sys.stdin)
print(data['$zigtag']['${arch}-linux']['tarball'])
"
# ---- Get the URL
zig_url="$(wget https://ziglang.org/download/index.json -O - -q| python3 -c "$get_zig_url_py")"
tarballf="$(basename "$zig_url")"
# ---- Get the tarball
if [[ ! -e "$tarballf" ]]; then
wget "$zig_url" -O "$tarballf"
fi
# ---- Unpack the tarball
zigdir="$(tar tJf "$tarballf"|head -n1)"
if [[ -e "$zigdir" ]]; then
rm -r "$zigdir"
fi
echo "Unpacking $tarballf"
tar xJf "$tarballf"
# ---- Symlink the zig binary
if [[ -L ~/.local/bin/$zigcmd ]]; then
unlink ~/.local/bin/$zigcmd
fi
zigpath="$(readlink -f $zigdir)/zig"
ln -s "$zigpath" ~/.local/bin/$zigcmd
# ---- Validate install
if ! which $zigcmd; then
echo "You need to add ~/.local/bin to your \$PATH"
fi
echo Done.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment