#!/bin/bash # ============================================================================== # anki-install.sh # # Installs or updates Anki on Linux using Ankitects' current GitHub release. # # Usage: # Local: bash anki-install.sh # curl | bash: curl -fsSL https://curl-pipe-bash.watzinger.biz/anki-install.sh | bash # # Packaging note (important, and why this script needed rewriting): # Anki 25.07 through 25.09.x shipped a small self-updating "launcher" per # release *series* (e.g. anki-launcher-25.09-linux.tar.zst), with patch # releases (25.09.1..25.09.4) applied in-app rather than via a new tarball. # As of 26.05 (16 Jun 2026), Anki dropped that launcher model entirely and # went back to shipping one full, self-contained, arch-specific tarball per # release, e.g. anki-26.05-linux-x86_64.tar.zst / anki-26.05-linux-aarch64.tar.zst. # There is no "series" concept anymore - the latest GitHub release tag IS # the version to install, with no series-mapping indirection needed. # # What it does: # - Resolves the latest stable Anki release from GitHub # - Reads the installed version from `anki --version` (if available) # - Skips installation if that version is already installed # - Downloads the official Linux tarball for the local architecture # - Verifies the SHA-256 checksum when GitHub publishes one for the asset # - Runs Anki's own bundled install.sh (installs to /usr/local by default) # - Cleans up temp files on exit, even on failure # # Requirements: curl, tar, zstd (or tar with built-in zstd support), sha256sum # ============================================================================== set -euo pipefail # ── Configuration ────────────────────────────────────────────────────────────── GITHUB_API_LATEST_RELEASE="https://api.github.com/repos/ankitects/anki/releases/latest" TMP_DIR="$(mktemp -d)" case "$(uname -m)" in x86_64|amd64) ARCH="x86_64" ;; aarch64|arm64) ARCH="aarch64" ;; *) echo "[ERROR] Unsupported architecture: $(uname -m)" >&2; exit 1 ;; esac # ── Cleanup on exit ──────────────────────────────────────────────────────────── cleanup() { rm -rf "$TMP_DIR" } trap cleanup EXIT # ── Helpers ──────────────────────────────────────────────────────────────────── err() { echo "[ERROR] $*" >&2; } info() { echo "[INFO] $*"; } # ── Dependency check ─────────────────────────────────────────────────────────── check_dependencies() { local missing=() for cmd in curl tar sha256sum; do command -v "$cmd" &>/dev/null || missing+=("$cmd") done # tar needs zstd support - either built-in or via the zstd binary if ! tar --help 2>&1 | grep -qi "zstd\|zst"; then command -v zstd &>/dev/null || missing+=("zstd") fi if [ ${#missing[@]} -gt 0 ]; then err "Missing required tools: ${missing[*]}" err "On Debian/Ubuntu: sudo apt install curl zstd" err "On Fedora/RHEL: sudo dnf install curl zstd" exit 1 fi } # ── Resolve latest release and its Linux asset for our architecture ──────────── get_latest_anki_version() { info "Fetching latest stable Anki release from GitHub..." LATEST_VERSION="" LATEST_ASSET_NAME="" LATEST_ASSET_URL="" LATEST_ASSET_SHA256="" local latest_json latest_json=$(curl -fsSL "$GITHUB_API_LATEST_RELEASE") LATEST_VERSION=$(printf '%s\n' "$latest_json" \ | grep -oP '"tag_name":\s*"\K[^"]+' \ | head -1 || true) if [ -z "$LATEST_VERSION" ]; then err "Could not determine the latest stable Anki release." err "Check https://github.com/ankitects/anki/releases manually." exit 1 fi info "Latest stable Anki release: $LATEST_VERSION" info "Looking for the Linux/$ARCH asset..." # Every release since 26.05 ships one self-contained tarball per arch, # e.g. anki-26.05-linux-x86_64.tar.zst / anki-26.05-linux-aarch64.tar.zst. local name_pattern="^anki-[0-9][^[:space:]]*-linux-${ARCH}[.]tar[.]zst$" local asset_match asset_match=$( printf '%s\n' "$latest_json" \ | grep -oP '"name":\s*"\K[^"]+|"digest":\s*"\Ksha256:[0-9a-f]+|"browser_download_url":\s*"\K[^"]+' \ | awk -v pattern="$name_pattern" ' function flush() { if (name ~ pattern && url != "") { print name "\t" url "\t" digest exit } name = "" url = "" digest = "" } /^anki-/ { if (name != "") { flush() } name = $0 next } /^sha256:/ { digest = substr($0, 8) next } /^https:\/\// { url = $0 next } END { flush() } ' ) if [ -z "$asset_match" ]; then err "Could not find a Linux/$ARCH asset for release $LATEST_VERSION." err "Check https://github.com/ankitects/anki/releases/tag/$LATEST_VERSION manually." exit 1 fi IFS=$'\t' read -r LATEST_ASSET_NAME LATEST_ASSET_URL LATEST_ASSET_SHA256 <<< "$asset_match" info "Latest Linux asset: $LATEST_ASSET_NAME" } # ── Detect installed Anki version ────────────────────────────────────────────── get_installed_anki_version() { if command -v anki &>/dev/null; then # Capture the raw launcher output first so any extra lines can be # explained instead of appearing as a stray line in the log. local version_output version_output=$(anki --version 2>&1 || true) # `anki --version` typically prints e.g. "Anki 26.05". INSTALLED_VERSION=$(printf '%s\n' "$version_output" \ | grep -oP 'Anki \K[0-9]+\.[0-9]+(\.[0-9]+)?' \ | head -1 || true) info "Installed Anki version: ${INSTALLED_VERSION:-unknown}" local extra_version_output extra_version_output=$(printf '%s\n' "$version_output" \ | grep -Fvx "Anki ${INSTALLED_VERSION}" \ | grep -v '^$' || true) if [ -n "$extra_version_output" ]; then info "Additional anki --version output: $extra_version_output" fi else INSTALLED_VERSION="" info "No existing Anki installation found." fi } # ── Download tarball and verify checksum ─────────────────────────────────────── download_and_verify() { local ver="$1" local asset="$2" local tarball_url="$3" local expected_hash="$4" local tarball_path="$TMP_DIR/$asset" info "Downloading Anki $ver from $tarball_url..." # Show a progress bar on a terminal; stay quiet (errors only) when logged. # Note: -s would suppress --progress-bar, so it's only used in the non-tty case. local progress_opts=(--progress-bar) [ -t 2 ] || progress_opts=(-sS) # Abort and retry if the transfer stalls below 10 KB/s for 60s, instead of # hanging silently on a bad CDN node. local stats stats=$(curl -fL "${progress_opts[@]}" \ --retry 3 --connect-timeout 20 --speed-limit 10240 --speed-time 60 \ -w '%{size_download} %{time_total} %{speed_download} %{remote_ip}' \ "$tarball_url" -o "$tarball_path") local size time speed ip read -r size time speed ip <<< "$stats" info "$(awk -v s="$size" -v t="$time" -v v="$speed" -v ip="$ip" 'BEGIN { printf "Downloaded %.1f MB in %.0fs (avg %.2f MB/s) from %s", s/1048576, t, v/1048576, ip }')" if [ -n "$expected_hash" ]; then info "Verifying checksum..." echo "${expected_hash} ${tarball_path}" | sha256sum --check --status info "Checksum verified OK." else info "No checksum published for this asset - skipping verification." fi TARBALL_PATH="$tarball_path" } # ── Install Anki ─────────────────────────────────────────────────────────────── install_anki() { if [ -n "$INSTALLED_VERSION" ] && [ "$LATEST_VERSION" = "$INSTALLED_VERSION" ]; then info "Anki $INSTALLED_VERSION is already the latest version. Nothing to do." return 0 fi info "Requesting sudo access now to avoid prompting later..." sudo -v download_and_verify "$LATEST_VERSION" "$LATEST_ASSET_NAME" "$LATEST_ASSET_URL" "$LATEST_ASSET_SHA256" local extract_dir="$TMP_DIR/anki-extract" mkdir -p "$extract_dir" info "Extracting Anki $LATEST_VERSION..." # Some older tar builds need --use-compress-program=unzstd; try auto first. if ! tar -xaf "$TARBALL_PATH" -C "$extract_dir" 2>/dev/null; then info "Retrying extraction with explicit zstd decompressor..." tar -xaf "$TARBALL_PATH" --use-compress-program=unzstd -C "$extract_dir" fi # The tarball unpacks to a single sub-directory (currently always named # "anki-linux" regardless of version, but we don't rely on that name). local anki_dir anki_dir=$(find "$extract_dir" -mindepth 1 -maxdepth 1 -type d | head -1) if [ -z "$anki_dir" ]; then err "Could not locate extracted Anki directory in $extract_dir" exit 1 fi # Anki's install.sh copies with `cp -v` (~10k lines), so keep its output in # a log outside $TMP_DIR (which gets cleaned up) and only report progress. local install_log install_log=$(mktemp /tmp/anki-install-XXXXXX.log) info "Running Anki's installer from $anki_dir (sudo required)..." info "Installer output is logged to $install_log" if ! ( cd "$anki_dir" sudo bash ./install.sh 2>&1 ) | tee "$install_log" | awk ' NR % 1000 == 0 { printf "[INFO] ... %d lines of installer output so far\n", NR; fflush() } END { printf "[INFO] Installer wrote %d lines of output.\n", NR } '; then err "Anki's installer failed. Last lines of $install_log:" tail -n 20 "$install_log" >&2 exit 1 fi info "Anki $LATEST_VERSION installed successfully." } # ── Main ─────────────────────────────────────────────────────────────────────── main() { check_dependencies get_latest_anki_version get_installed_anki_version install_anki info "Done. Anki $LATEST_VERSION is installed." info "Launch it by running: anki" } main "$@"