On shared machines, clusters, or restricted servers, I often do not have root access. That does not mean I need to give up useful tools like GitHub CLI.

The simple approach is to install gh under ~/.local:

mkdir -p ~/.local/bin ~/.local/opt

Then download the official Linux tarball from the GitHub CLI releases page, extract it into ~/.local/opt/gh, and symlink the binary:

ln -sf ~/.local/opt/gh/bin/gh ~/.local/bin/gh

Finally, make sure ~/.local/bin is in PATH:

export PATH="$HOME/.local/bin:$PATH"

The following code snippet can be used to automate the installation of gh without root privileges:

mkdir -p ~/.local/bin ~/.local/opt
cd /tmp

ARCH="$(uname -m)"
case "$ARCH" in
  x86_64) GH_ARCH="amd64" ;;
  aarch64|arm64) GH_ARCH="arm64" ;;
  armv7l) GH_ARCH="armv6" ;;
  *) echo "Unsupported architecture: $ARCH"; exit 1 ;;
esac

VERSION="$(curl -fsSL https://api.github.com/repos/cli/cli/releases/latest \
  | grep '"tag_name":' \
  | sed -E 's/.*"v([^"]+)".*/\1/')"

curl -fL -o "gh_${VERSION}_linux_${GH_ARCH}.tar.gz" \
  "https://github.com/cli/cli/releases/download/v${VERSION}/gh_${VERSION}_linux_${GH_ARCH}.tar.gz"

tar -xzf "gh_${VERSION}_linux_${GH_ARCH}.tar.gz"

rm -rf "$HOME/.local/opt/gh"
mv "gh_${VERSION}_linux_${GH_ARCH}" "$HOME/.local/opt/gh"

ln -sf "$HOME/.local/opt/gh/bin/gh" "$HOME/.local/bin/gh"

echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.bashrc
export PATH="$HOME/.local/bin:$PATH"

gh --version

This keeps the install local, clean, removable, and independent of system packages. It is also a good pattern for many CLI tools on machines where sudo is unavailable.