Skip to content

Instantly share code, notes, and snippets.

@JustArchi
Last active November 19, 2024 09:40
Show Gist options
  • Save JustArchi/b5ec81de41a32fc77df8816716691918 to your computer and use it in GitHub Desktop.
Save JustArchi/b5ec81de41a32fc77df8816716691918 to your computer and use it in GitHub Desktop.
Script intended for environments without dotnet installer available, to easily and quickly install/update .NET SDK
#!/usr/bin/env sh
set -eu
# Main version for SDK and runtime
MAIN_VERSION="9.0"
# Installation directory
INSTALL_DIR="/usr/share/dotnet"
# If you need additional runtimes, specify their versions here separated by space, such as "9.0 8.0"
ADDITIONAL_VERSIONS=""
# If you require extra installer args you can specify them here
EXTRA_INSTALL_ARGS=""
if [ "$(id -u)" != 0 ]; then
echo "ERROR: You need to execute this script as root!"
exit 1
fi
if command -v dotnet > /dev/null; then
echo "Checking old install..."
dotnet --info
fi
case "$(uname -m)" in
"aarch64")
if [ "$(getconf LONG_BIT)" -lt 64 ]; then
# This is 32-bit OS running on 64-bit CPU
EXTRA_INSTALL_ARGS="${EXTRA_INSTALL_ARGS} --architecture arm"
fi
;;
esac
echo "Downloading dotnet-install.sh..."
rm -f /tmp/dotnet-install.sh
if command -v curl > /dev/null; then
curl -sL 'https://dot.net/v1/dotnet-install.sh' -o /tmp/dotnet-install.sh
elif command -v wget > /dev/null; then
wget -q 'https://dot.net/v1/dotnet-install.sh' -O /tmp/dotnet-install.sh
else
echo "ERROR: Install either curl or wget to download installer"
exit 1
fi
chmod +x /tmp/dotnet-install.sh
echo "Installing dotnet..."
rm -rf /tmp/dotnet-new
/tmp/dotnet-install.sh -c "$MAIN_VERSION" -i /tmp/dotnet-new --no-path $EXTRA_INSTALL_ARGS
if [ -n "$ADDITIONAL_VERSIONS" ]; then
for ADDITIONAL_VERSION in $ADDITIONAL_VERSIONS; do
/tmp/dotnet-install.sh -c "$ADDITIONAL_VERSION" -i /tmp/dotnet-new --runtime aspnetcore --no-path $EXTRA_INSTALL_ARGS
done
fi
rm -f /tmp/dotnet-install.sh
rm -rf /tmp/dotnet-old
if [ -d "$INSTALL_DIR" ]; then
mv "$INSTALL_DIR" /tmp/dotnet-old
fi
mv /tmp/dotnet-new "$INSTALL_DIR"
if [ ! -L /usr/bin/dotnet ]; then
ln -s "${INSTALL_DIR}/dotnet" /usr/bin/dotnet
fi
echo "Cleaning up..."
rm -rf /tmp/dotnet-old
"${INSTALL_DIR}/dotnet" --info
"${INSTALL_DIR}/dotnet" workload update
echo "All done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment