Skip to content

Instantly share code, notes, and snippets.

@rijieli
Last active February 15, 2025 03:00
Show Gist options
  • Save rijieli/a83d42a9d86717c66eb78ad870e23014 to your computer and use it in GitHub Desktop.
Save rijieli/a83d42a9d86717c66eb78ad870e23014 to your computer and use it in GitHub Desktop.
#!/bin/zsh
INSTALL_DIR="$HOME/utils"
OLLAMA_BIN="$INSTALL_DIR/ollama"
mkdir -p "$INSTALL_DIR"
get_installed_version() {
if [ -x "$OLLAMA_BIN" ]; then
"$OLLAMA_BIN" -v 2>&1 | grep "client version is" | grep -oE '[0-9.]+$'
else
echo ""
fi
}
get_latest_version() {
curl -s https://api.github.com/repos/ollama/ollama/releases/latest | jq -r '.tag_name' | sed 's/^v//'
}
get_download_url() {
local url=$(curl -s https://api.github.com/repos/ollama/ollama/releases/latest | \
jq -r '.assets[] | select(.name | contains("ollama-darwin.tgz")) | .browser_download_url')
if [ -z "$url" ]; then
echo "Error: Could not retrieve latest release URL."
exit 1
fi
echo "$url"
}
installed_version=$(get_installed_version)
latest_version=$(get_latest_version)
echo "Installed Ollama version: $installed_version"
echo "Latest Ollama version: $latest_version"
if [[ "$installed_version" == "$latest_version" ]]; then
echo "Ollama is already up to date."
exit 0
elif [ -z "$latest_version" ]]; then
echo "Could not determine latest version from GitHub."
exit 1
else
if [[ "$installed_version" == "" ]]; then
echo "Ollama is not installed. Installing..."
else
echo "A newer version is available. Updating..."
fi
temp_dir=$(mktemp -d)
temp_file="$temp_dir/ollama.tgz"
curl -L "$(get_download_url)" -o "$temp_file"
tar -xzf "$temp_file" -C "$temp_dir"
mv "$temp_dir/ollama" "$OLLAMA_BIN"
chmod +x "$OLLAMA_BIN"
rm -rf "$temp_dir"
echo "Ollama $(if [[ "$installed_version" == "" ]]; then echo "installed"; else echo "updated"; fi) successfully!"
ollama -v
fi
@rijieli
Copy link
Author

rijieli commented Feb 15, 2025

Before using this script, please change INSTALL_DIR="$HOME/utils" to the folder where your Ollama binary file is located.

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