Skip to content

Instantly share code, notes, and snippets.

@celsojr
Created March 23, 2025 07:19
Show Gist options
  • Save celsojr/0b183ff72deed8a74965dc118f92ac42 to your computer and use it in GitHub Desktop.
Save celsojr/0b183ff72deed8a74965dc118f92ac42 to your computer and use it in GitHub Desktop.
This script automates the installation of the latest .NET SDK for Linux (x64).
#!/bin/bash
# ==============================================================================
# Title: Install & Manage .NET SDK (Latest Version)
# Description: This script automates the installation of the latest .NET SDK
# for Linux (x64). It verifies, downloads, and extracts the
# latest version while keeping `global.json` updated.
# Author: Celso Jr
# GitHub: https://github.com/celsojr
# Version: 1.0
# Date: 2025-03-23
# ==============================================================================
# Features:
# - Fetches the latest .NET SDK release from Microsoft's official API.
# - Asks for user confirmation before downloading & updating.
# - Extracts to ~/.dotnet and cleans up (optional).
# - Updates /global.json while preserving "rollForward": "latestMajor".
# - Displays the last 5 installed SDKs and runtimes.
#
# Usage:
# - Run: `./dotnet_update.sh` (give permissions)
# - Ensure you have `jq` installed (`sudo apt install jq`).
# Define install directory
DOTNET_DIR="$HOME/.dotnet"
# Create the install directory if not present
mkdir -p "$DOTNET_DIR"
# Microsoft .NET Release Metadata URL
JSON_URL="https://dotnetcli.azureedge.net/dotnet/release-metadata/10.0/releases.json"
# Fetch the latest SDK version
LATEST_VERSION=$(curl -s "$JSON_URL" | jq -r '.releases[0].sdk.version')
# Get the download URL for Linux x64
DOWNLOAD_URL=$(curl -s "$JSON_URL" | jq -r '
.releases[0].sdk.files[] |
select(.rid=="linux-x64" and .url and (.url | type == "string") and (.url | test("tar.gz$"))) | .url'
)
# Validate URL
if [[ -z "$DOWNLOAD_URL" || "$DOWNLOAD_URL" == "null" ]]; then
echo "❌ Error: Could not find a valid .NET SDK download URL."
exit 1
fi
# Extract filename from URL
FILENAME=$(basename "$DOWNLOAD_URL")
# Prompt user to confirm download
echo "Latest .NET SDK: $LATEST_VERSION"
echo "File to download: $FILENAME"
read -p "Do you want to proceed with the download? (y/n): " CONFIRM_DOWNLOAD
if [[ "$CONFIRM_DOWNLOAD" != "y" ]]; then
echo "Download canceled."
exit 0
fi
# Download the latest SDK
wget -O "$FILENAME" "$DOWNLOAD_URL"
# Extract into the .dotnet directory
tar zxf "$FILENAME" -C "$DOTNET_DIR"
# Ask if the file should be deleted after extraction
echo
echo "Downloaded archive:"
echo "$FILENAME"
echo
read -p "Do you want to remove the archive after extraction? (y/n): " CONFIRM_DELETE
if [[ "$CONFIRM_DELETE" == "y" ]]; then
rm "$FILENAME"
echo "Deleted archive: $FILENAME"
fi
# List the last 5 SDK installed
echo
echo "Installed SDKs (last 5):"
$DOTNET_DIR/dotnet --list-sdks | tail -n 5
# List the last 5 runtimes installed
echo
echo "Installed Runtimes (last 5):"
$DOTNET_DIR/dotnet --list-runtimes | tail -n 5
# Define global.json path
GLOBAL_JSON_PATH="/global.json"
# Show existing global.json config if it exists
echo
if [ -f "$GLOBAL_JSON_PATH" ]; then
CURRENT_VERSION=$(jq -r '.sdk.version' "$GLOBAL_JSON_PATH" 2>/dev/null)
echo "Current global.json version: $CURRENT_VERSION"
else
echo "No existing global.json found."
CURRENT_VERSION="None"
fi
# Show the new version and ask for confirmation
echo "New version to set: $LATEST_VERSION"
read -p "Do you want to update global.json? (y/n): " CONFIRM_UPDATE
if [[ "$CONFIRM_UPDATE" == "y" ]]; then
if [ -f "$GLOBAL_JSON_PATH" ]; then
# Update only the version while keeping "rollForward"
sudo jq '.sdk.version = "'"$LATEST_VERSION"'"' "$GLOBAL_JSON_PATH" > temp.json && sudo mv temp.json "$GLOBAL_JSON_PATH"
else
# Create a new global.json with "rollForward" set
echo "{ \"sdk\": { \"rollForward\": \"latestMajor\", \"version\": \"$LATEST_VERSION\" } }" | sudo tee "$GLOBAL_JSON_PATH" > /dev/null
fi
echo
echo "✅ Updated /global.json to version: $LATEST_VERSION"
else
echo "Skipped updating global.json."
fi
echo "✅ Installation complete!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment