Skip to content

Instantly share code, notes, and snippets.

@innovatorved
Created November 29, 2025 12:41
Show Gist options
  • Select an option

  • Save innovatorved/2a74f4cd2cfe64215ad4a73d4b520d21 to your computer and use it in GitHub Desktop.

Select an option

Save innovatorved/2a74f4cd2cfe64215ad4a73d4b520d21 to your computer and use it in GitHub Desktop.
A robust script to setup whisper.cpp: clone, detect platform, build, and download model.
#!/bin/bash
# setup_whisper.sh
# A robust script to setup whisper.cpp: clone, detect platform, build, and download model.
set -e # Exit immediately if a command exits with a non-zero status.
set -o pipefail # Return value of a pipeline is the status of the last command to exit with a non-zero status
# --- Helper Functions ---
log() {
echo -e "\033[1;32m[SETUP] $1\033[0m"
}
warn() {
echo -e "\033[1;33m[WARN] $1\033[0m"
}
error() {
echo -e "\033[1;31m[ERROR] $1\033[0m"
exit 1
}
cleanup() {
# This function is called on exit.
# You can add cleanup logic here if needed (e.g., removing temp files).
:
}
trap 'error "An unexpected error occurred on line $LINENO. Exiting."' ERR
trap cleanup EXIT
check_dependency() {
if ! command -v "$1" &> /dev/null; then
error "$1 is not installed. Please install it and try again."
fi
}
# --- 1. Dependency Checks ---
log "Checking dependencies..."
check_dependency git
check_dependency cmake
check_dependency make
# Check for a C++ compiler
if ! command -v g++ &> /dev/null && ! command -v clang++ &> /dev/null; then
error "No C++ compiler found (g++ or clang++). Please install one."
fi
# Check for download tools (curl or wget required for model download script usually)
if ! command -v curl &> /dev/null && ! command -v wget &> /dev/null; then
error "Neither curl nor wget found. Please install one of them for downloading models."
fi
# --- 2. Clone Repository ---
REPO_URL="https://github.com/innovatorved/whisper.cpp.original.git"
DIR_NAME="whisper.cpp"
BRANCH="develop"
if [ -d "$DIR_NAME" ]; then
log "Directory '$DIR_NAME' already exists."
# Check if it is a valid git repo
if [ -d "$DIR_NAME/.git" ]; then
log "Verifying it is the correct repository..."
cd "$DIR_NAME"
REMOTE_URL=$(git config --get remote.origin.url || echo "")
cd .. # Go back to root to perform operations if needed
# Normalize URLs for comparison (remove .git suffix, handle https vs ssh if needed - keeping simple for now)
# We check if the REMOTE_URL contains the repo name or user/repo to be somewhat flexible but safe.
if [[ "$REMOTE_URL" != *"$REPO_URL"* && "$REMOTE_URL" != *"innovatorved/whisper.cpp.original"* ]]; then
warn "Directory '$DIR_NAME' exists but points to a different remote: $REMOTE_URL"
warn "Expected: $REPO_URL"
BACKUP_NAME="${DIR_NAME}.bak.$(date +%s)"
warn "Backing up existing directory to '$BACKUP_NAME' and cloning fresh..."
mv "$DIR_NAME" "$BACKUP_NAME"
log "Cloning repository..."
git clone "$REPO_URL" "$DIR_NAME"
cd "$DIR_NAME"
log "Checking out branch '$BRANCH'..."
if ! git checkout "$BRANCH"; then
error "Failed to checkout branch '$BRANCH'."
fi
else
# Remote matches, proceed with update
cd "$DIR_NAME"
log "Fetching updates..."
if ! git fetch origin; then
error "Failed to fetch updates."
fi
log "Checking out branch '$BRANCH'..."
if ! git checkout "$BRANCH"; then
error "Failed to checkout branch '$BRANCH'."
fi
log "Pulling latest changes..."
if ! git pull origin "$BRANCH"; then
error "Failed to update repository. You may have local changes or merge conflicts. Please resolve them manually."
fi
fi
else
error "Directory '$DIR_NAME' exists but is not a git repository. Please remove it or rename it to proceed."
fi
else
log "Cloning repository..."
git clone "$REPO_URL" "$DIR_NAME"
cd "$DIR_NAME"
log "Checking out branch '$BRANCH'..."
if ! git checkout "$BRANCH"; then
error "Failed to checkout branch '$BRANCH'."
fi
fi
# --- 3. Platform Detection & Build Configuration ---
OS=$(uname -s)
ARCH=$(uname -m)
BUILD_ARGS=""
log "Detected Platform: OS=$OS, Arch=$ARCH"
if [ "$OS" = "Darwin" ]; then
log "macOS detected."
if [ "$ARCH" = "arm64" ]; then
log "Apple Silicon detected. Metal support is enabled by default."
else
log "Intel Mac detected."
fi
# Standard build for macOS works for both Intel and Apple Silicon (Metal)
BUILD_ARGS=""
elif [ "$OS" = "Linux" ]; then
log "Linux detected."
if command -v nvidia-smi &> /dev/null; then
log "NVIDIA GPU detected. Enabling CUDA support."
BUILD_ARGS="-DGGML_CUDA=1"
else
log "No NVIDIA GPU detected. Using CPU-only build."
BUILD_ARGS=""
fi
else
log "Unknown or unsupported OS: $OS. Attempting standard build..."
BUILD_ARGS=""
fi
# --- 4. Build ---
log "Configuring build with CMake..."
# Clean build directory if it exists to ensure fresh configuration
if [ -d "build" ]; then
log "Cleaning previous build..."
rm -rf build
fi
if ! cmake -B build $BUILD_ARGS; then
error "CMake configuration failed."
fi
log "Building project..."
if ! cmake --build build -j --config Release; then
error "Build failed."
fi
# --- 5. Download Model ---
MODEL="base"
MODEL_PATH="models/ggml-$MODEL.bin"
DOWNLOAD_SCRIPT="./models/download-ggml-model.sh"
if [ -f "$MODEL_PATH" ]; then
log "Model '$MODEL' already exists at $MODEL_PATH."
else
if [ ! -f "$DOWNLOAD_SCRIPT" ]; then
error "Download script '$DOWNLOAD_SCRIPT' not found. The repo might be incomplete."
fi
log "Downloading model '$MODEL'..."
# The download script usually uses curl or wget. We checked for them earlier.
if ! bash "$DOWNLOAD_SCRIPT" "$MODEL"; then
error "Failed to download model '$MODEL'."
fi
fi
# --- 6. Verification ---
CLI_PATH="./build/bin/whisper-cli"
if [ ! -f "$CLI_PATH" ]; then
error "Build failed: $CLI_PATH not found."
fi
log "Verifying build..."
if ! "$CLI_PATH" -h > /dev/null 2>&1; then
error "Verification failed. The executable did not run correctly."
fi
log "Setup completed successfully!"
echo ""
echo "You can now run whisper.cpp. Example:"
echo " $CLI_PATH -m $MODEL_PATH -f samples/jfk.wav"
echo ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment