Skip to content

Instantly share code, notes, and snippets.

@mehd-io
Last active March 13, 2024 13:35
Show Gist options
  • Save mehd-io/f9d50c33fbdceeda3113fc40bcd93a31 to your computer and use it in GitHub Desktop.
Save mehd-io/f9d50c33fbdceeda3113fc40bcd93a31 to your computer and use it in GitHub Desktop.
Script to install duckdb (Linux/Macos)
#!/usr/bin/env bash
set -e
CDN="https://github.com/duckdb/duckdb/releases/download"
INSTALL_DIR="$HOME/.local/bin"
# Function to initialize platform specifics
initPlatform() {
OS=$(uname -s | tr '[:upper:]' '[:lower:]')
ARCH=$(uname -m)
case "$OS-$ARCH" in
darwin-arm64) PLATFORM="osx-arm64" ;;
darwin-x86_64) PLATFORM="osx-amd64" ;;
linux-x86_64) PLATFORM="linux-amd64" ;;
*)
echo "Unsupported platform: OS=$OS ARCH=$ARCH"
exit 1
;;
esac
}
# Function to download and install DuckDB
installDuckDB() {
VERSION=${1:-latest}
if [ "$VERSION" = "latest" ]; then
# Fetch the latest version number dynamically. Update this to match DuckDB's release tagging pattern.
VERSION=$(curl -sSL https://api.github.com/repos/duckdb/duckdb/releases/latest | grep 'tag_name' | cut -d '"' -f 4 | sed 's/v//')
fi
FILENAME="duckdb_cli-${PLATFORM}.zip"
URL="$CDN/v${VERSION}/${FILENAME}"
echo "Downloading DuckDB $VERSION for $PLATFORM..."
curl -sSL -o duckdb.zip "$URL" || { echo "Failed to download DuckDB from $URL"; exit 1; }
if [ ! -f duckdb.zip ] || [ "$(wc -c <duckdb.zip)" -eq 0 ]; then
echo "Downloaded file is not valid, please check the version and try again."
rm -f duckdb.zip
exit 1
fi
unzip -o duckdb.zip -d "$INSTALL_DIR" || { echo "Failed to unzip the downloaded file. It might not be a valid ZIP archive."; exit 1; }
rm duckdb.zip
echo "DuckDB installed successfully to $INSTALL_DIR"
}
# Function to uninstall DuckDB
uninstallDuckDB() {
if [ -f "$INSTALL_DIR/duckdb" ]; then
rm -f "$INSTALL_DIR/duckdb"
echo "DuckDB uninstalled successfully from $INSTALL_DIR"
else
echo "DuckDB is not installed in $INSTALL_DIR"
fi
}
# Main script logic based on input arguments
case "$1" in
install)
initPlatform
installDuckDB "$2"
;;
uninstall)
uninstallDuckDB
;;
*)
echo "Usage: $0 <install|uninstall> [version]"
exit 1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment