Skip to content

Instantly share code, notes, and snippets.

@Not-Dhanraj
Last active March 13, 2026 06:17
Show Gist options
  • Select an option

  • Save Not-Dhanraj/7a62c05e45d616b217c4b2f06cda17d5 to your computer and use it in GitHub Desktop.

Select an option

Save Not-Dhanraj/7a62c05e45d616b217c4b2f06cda17d5 to your computer and use it in GitHub Desktop.
#!/bin/bash
usage() {
echo "Usage: $0 [options]"
echo "Options:"
echo " --version <VERSION> Specify the Flutter version to install."
echo " --channel <CHANNEL> Specify the Flutter channel (default: stable)."
echo " --replace Replace the existing Flutter installation."
echo " --help Display this help message."
exit 1
}
if [[ "$1" == "--help" ]]; then
usage
fi
echo "Installing required dependencies..."
sudo apt-get update -y
sudo apt-get install -y curl git unzip xz-utils zip libglu1-mesa tar jq
check_command() {
if ! command -v "$1" >/dev/null 2>&1; then
echo "Error: '$1' is not installed and could not be installed automatically."
exit 1
fi
}
REQUIRED_COMMANDS=("curl" "tar" "jq")
for cmd in "${REQUIRED_COMMANDS[@]}"; do
check_command "$cmd"
done
VERSION=""
REPLACE=0
CHANNEL="stable"
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
--version)
VERSION="$2"
shift 2
;;
--replace)
REPLACE=1
shift
;;
--channel)
CHANNEL="$2"
shift 2
;;
*)
echo "Error: Unknown option '$1'."
exit 1
;;
esac
done
SDK_DIR="$HOME/tools/sdk"
if [ ! -d "$SDK_DIR" ]; then
mkdir -p "$SDK_DIR"
fi
FLUTTER_DIR="$SDK_DIR/flutter"
if [ -d "$FLUTTER_DIR" ]; then
if [ "$REPLACE" -eq 1 ]; then
echo "Removing the existing Flutter installation..."
sudo rm -rf "$FLUTTER_DIR"
else
if [ -n "$VERSION" ]; then
echo "Error: Flutter is already installed in '$FLUTTER_DIR'. Use --replace to overwrite."
exit 1
else
read -p "Flutter is already installed. Do you want to replace it? (y/n): " replace_response
if [[ "$replace_response" != "y" && "$replace_response" != "Y" ]]; then
exit 0
else
echo "Removing the existing Flutter installation..."
sudo rm -rf "$FLUTTER_DIR"
fi
fi
fi
fi
ARCHITECTURE=$(uname -m)
if [ "$ARCHITECTURE" == "x86_64" ]; then
FLUTTER_ARCH="x64"
elif [ "$ARCHITECTURE" == "aarch64" ]; then
FLUTTER_ARCH="arm64"
else
echo "Error: Unsupported architecture '$(uname -m)'. Flutter supports x64 and arm64."
exit 1
fi
echo "Fetching Flutter releases list..."
FLUTTER_LINUX_DATA=$(curl -s https://storage.googleapis.com/flutter_infra_release/releases/releases_linux.json)
if [ -n "$VERSION" ]; then
FLUTTER_PATH=$(echo "$FLUTTER_LINUX_DATA" | jq -r --arg ARCH "$FLUTTER_ARCH" --arg VERSION "$VERSION" --arg CHANNEL "$CHANNEL" '.releases[] | select(.dart_sdk_arch == $ARCH and .version == $VERSION and .channel == $CHANNEL) | .archive')
if [ -z "$FLUTTER_PATH" ]; then
echo "Error: Version '$VERSION' not found for channel '$CHANNEL' and architecture '$FLUTTER_ARCH'."
exit 1
fi
FLUTTER_VERSION="$VERSION"
else
FLUTTER_PATH=$(echo "$FLUTTER_LINUX_DATA" | jq -r --arg ARCH "$FLUTTER_ARCH" --arg CHANNEL "$CHANNEL" '.releases | map(select(.dart_sdk_arch == $ARCH and .channel == $CHANNEL)) | map(select(.version)) | .[0].archive')
if [ -z "$FLUTTER_PATH" ] || [ "$FLUTTER_PATH" == "null" ]; then
echo "Error: Could not find a Flutter release for channel '$CHANNEL' and architecture '$FLUTTER_ARCH'."
exit 1
fi
FLUTTER_VERSION=$(echo "$FLUTTER_PATH" | sed -E 's|.*/flutter_linux_([^-]+).*|\1|')
fi
FLUTTER_DOWNLOAD_URL="https://storage.googleapis.com/flutter_infra_release/releases/$FLUTTER_PATH"
FLUTTER_ARCHIVE="$SDK_DIR/flutter.tar.xz"
echo "Downloading Flutter $FLUTTER_VERSION ($CHANNEL / $FLUTTER_ARCH)..."
echo "URL: $FLUTTER_DOWNLOAD_URL"
curl -L "$FLUTTER_DOWNLOAD_URL" -o "$FLUTTER_ARCHIVE"
echo "Extracting Flutter..."
tar -xf "$FLUTTER_ARCHIVE" -C "$SDK_DIR"
echo "Cleaning up..."
rm "$FLUTTER_ARCHIVE"
echo ""
echo "Flutter $FLUTTER_VERSION installed successfully to '$FLUTTER_DIR'."
echo "To use Flutter, add the following line to your shell profile (e.g., .bashrc or .zshrc):"
echo ""
echo " export PATH=\"\$PATH:$SDK_DIR/flutter/bin\""
echo ""
echo "Then restart your terminal or run: source ~/.bashrc"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment