Skip to content

Instantly share code, notes, and snippets.

@PierpaoloPernici
Last active April 15, 2026 06:39
Show Gist options
  • Select an option

  • Save PierpaoloPernici/45238691866ceeae5cef0e72301b3542 to your computer and use it in GitHub Desktop.

Select an option

Save PierpaoloPernici/45238691866ceeae5cef0e72301b3542 to your computer and use it in GitHub Desktop.
update-llama.sh
#!/bin/bash
set -e
# Colori per il terminale
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m'
print_info() { echo -e "${GREEN}[INFO]${NC} $1"; }
print_warning() { echo -e "${YELLOW}[WARN]${NC} $1"; }
print_error() { echo -e "${RED}[ERROR]${NC} $1"; }
main() {
local target_dir="${1:-.}"
mkdir -p "$target_dir"
cd "$target_dir"
target_dir=$(pwd)
local version_file=".llama_version"
# 1. Recupera dati dall'API di GitHub
print_info "Checking for updates..."
local release_data=$(curl -s https://api.github.com/repos/ggml-org/llama.cpp/releases/latest)
local latest_tag=$(echo "$release_data" | grep '"tag_name"' | sed 's/.*"tag_name": *"\([^"]*\)".*/\1/')
if [ -z "$latest_tag" ]; then
print_error "Could not fetch latest version from GitHub."
exit 1
fi
# 2. Controllo versione locale (Il Check che mancava!)
local current_tag="none"
if [ -f "$version_file" ]; then
current_tag=$(cat "$version_file")
fi
if [ "$latest_tag" == "$current_tag" ]; then
print_info "You are already up to date! (Version: $latest_tag)"
exit 0
fi
print_info "New version detected: $current_tag -> $latest_tag"
# 3. Identifica l'URL per macOS Apple Silicon (arm64)
local download_url=$(echo "$release_data" | grep -o '"browser_download_url": *"[^"]*"' | grep "macos-arm64" | grep "tar.gz" | sed 's/.*"browser_download_url": *"\([^"]*\)".*/\1/' | head -n1)
if [ -z "$download_url" ]; then
print_error "Could not find the arm64 macOS binary in this release."
exit 1
fi
# 4. Download ed Estrazione in cartella temporanea
local temp_dir=$(mktemp -d)
trap 'rm -rf "$temp_dir"' EXIT
print_info "Downloading $latest_tag..."
curl -L "$download_url" -o "$temp_dir/llama.tar.gz"
mkdir -p "$temp_dir/extract"
tar -xzf "$temp_dir/llama.tar.gz" -C "$temp_dir/extract"
# 5. Pulizia profonda dei vecchi residui (per evitare conflitti dyld)
print_info "Cleaning up old version files..."
rm -f llama-* libllama* libggml* libmtmd* rpc-server 2>/dev/null || true
# 6. Installazione nuovi file
print_info "Installing new binaries and libraries..."
find "$temp_dir/extract" -type f -exec mv {} "$target_dir/" \;
chmod +x llama-* 2>/dev/null || true
# 7. Ricostruzione Symlink per le librerie dinamiche (Fondamentale per macOS)
print_info "Rebuilding library symlinks..."
for lib in *.dylib; do
[ -f "$lib" ] || continue
# Se il file è libname.0.0.8797.dylib
if [[ "$lib" =~ ^(lib[^.]+)\.([0-9]+) ]]; then
local base_name="${BASH_REMATCH[1]}.dylib" # libname.dylib
local major_name="${BASH_REMATCH[1]}.${BASH_REMATCH[2]}.dylib" # libname.0.dylib
ln -sf "$lib" "$base_name"
ln -sf "$lib" "$major_name"
# print_info "Linked $lib -> $major_name" # Opzionale: debug dei link
fi
done
# 8. Salvataggio versione e test finale
echo "$latest_tag" > "$version_file"
print_info "Update complete!"
if [ -f "./llama-cli" ]; then
print_info "Final check:"
./llama-cli --version
fi
}
main "$1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment