Skip to content

Instantly share code, notes, and snippets.

@bplasmeijer
Last active February 10, 2026 08:46
Show Gist options
  • Select an option

  • Save bplasmeijer/1133345e6e8bad136904563cd2f2a061 to your computer and use it in GitHub Desktop.

Select an option

Save bplasmeijer/1133345e6e8bad136904563cd2f2a061 to your computer and use it in GitHub Desktop.
Download the latest K9S CLI or other tools using a standardized method.

k9s Installation Script Example

A simple bash script to install k9s - the Kubernetes terminal UI

Features

  • ✅ Automatically fetches the latest release version from GitHub
  • ✅ Installs to ~/.local/bin by default
  • ✅ Supports custom version and installation directory
  • ✅ Displays installation status and version info

Usage

Basic Installation (Latest Version)

./k9s.sh

Specific Version

./k9s.sh 0.27.4

Custom Installation Directory

./k9s.sh 0.27.4 /usr/local/bin

Script

#!/bin/bash
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/_lib.sh"

GITHUB="derailed/k9s"
VERSION=${1:-"$(get_latest_release $GITHUB)"}
INSTALL_DIR=${2:-"$HOME/.local/bin"}
CMD=k9s
NAME="k9s terminal UI for Kubernetes"

pre_run

curl -sSL "https://github.com/$GITHUB/releases/download/v${VERSION}/k9s_Linux_amd64.tar.gz" | \
  tar -zx -C "$INSTALL_DIR" $CMD

post_run version

Shared Library (_lib.sh)

This script uses a shared _lib.sh library that provides:

  • get_latest_release <repo> - Fetches the latest GitHub release version
  • pre_run - Prepares installation (creates directory, checks PATH)
  • post_run <flag> - Displays version info after installation

_lib.sh Functions

get_latest_release() {
  if [ $# -lt 2 ]; then PREFIX="v"; else PREFIX=$2; fi

  curl --silent "https://api.github.com/repos/$1/releases/latest" |
  grep '"tag_name":' | sed -E "s/.*\"$PREFIX([^\"]+)\".*/\1/"
}

pre_run() {
  echo -e "\e[34m»»» 📦 \e[32mInstalling \e[33m$NAME \e[35mv$VERSION\e[0m ..."
  if [ -z "$INSTALL_DIR" ]; then return; fi

  echo -e "\e[34m»»» 📂 \e[32mTarget directory for binary: \e[35m$INSTALL_DIR"

  if [[ :$PATH: == *:"$INSTALL_DIR":* ]] ; then
    echo -e "\e[34m»»» ✅ \e[32mPATH is good"
  else
    echo -e "\e[34m»»» 💥 \e[31mInstall directory in not in PATH. Temporarily adding it!"
    echo -e "\e[34m»»» 📢 \e[31mNOTE! Amend your shell startup scripts to make this change permanent:\n\t\e[37mexport PATH=\$PATH:$INSTALL_DIR"
    export PATH="$PATH:$INSTALL_DIR"
  fi

  mkdir -p "$INSTALL_DIR"
}

post_run() {
  VERFLAG=${1:-"--version"}
  echo -e "\n\e[34m»»» 💾 \e[32mInstalled to: \e[33m$(which $CMD)"
  echo -e "\e[34m»»» 💡 \e[32mVersion details: \e[39m$($CMD $VERFLAG)"
}

How It Works

  1. Get Latest Version - Queries GitHub API for the latest k9s release
  2. Prepare - Creates ~/.local/bin if needed, checks PATH configuration
  3. Download & Extract - Downloads the Linux tarball and extracts the binary
  4. Verify - Displays the installation path and version

Requirements

  • bash 4.0+
  • curl
  • tar
  • grep & sed

License

MIT

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment