#!/usr/bin/env bash set -euo pipefail IFS=$'\n\t' trap 'echo "[ERROR] Line ${LINENO}: command failed"; exit 1' ERR ARCH=$(uname -m) case "$ARCH" in x86_64|aarch64|arm64) ;; *) echo "[FATAL] Unsupported architecture: $ARCH" exit 2 ;; esac if command -v apt >/dev/null 2>&1; then PKG="curl ca-certificates" sudo apt-get update -y sudo apt-get install -y $PKG fi echo "[STEP] Installing Ollama (official one-liner)" curl -fsSL https://ollama.com/install.sh | sh echo "[STEP] Ensuring ollama service is running" if command -v systemctl >/dev/null 2>&1; then sudo systemctl enable --now ollama || true sudo systemctl status --no-pager ollama || true else # Non-systemd: try launching in background nohup ollama serve >/var/log/ollama-serve.log 2>&1 & sleep 2 fi echo "[STEP] Testing API on localhost:11434" for i in {1..10}; do if curl -fsS http://127.0.0.1:11434/api/version >/dev/null 2>&1; then echo "[OK] Ollama API is reachable." exit 0 fi sleep 1 done echo "[WARN] Ollama API not reachable yet. Check logs: journalctl -u ollama -e" exit 0