Skip to content

Instantly share code, notes, and snippets.

@JustArchi
Last active September 13, 2023 14:48
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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="STS"
# If you need additional runtimes, specify their versions here separated by space, such as "6.0 5.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 /usr/share/dotnet ]; then
mv /usr/share/dotnet /tmp/dotnet-old
fi
mv /tmp/dotnet-new /usr/share/dotnet
if [ ! -L /usr/bin/dotnet ]; then
ln -s /usr/share/dotnet/dotnet /usr/bin/dotnet
fi
echo "Cleaning up..."
rm -rf /tmp/dotnet-old
dotnet --info
echo "All done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment