Skip to content

Instantly share code, notes, and snippets.

@StrangeRanger
Last active October 27, 2024 16:49
Show Gist options
  • Save StrangeRanger/b848d8eeef31dae1cebc9a68ac10df35 to your computer and use it in GitHub Desktop.
Save StrangeRanger/b848d8eeef31dae1cebc9a68ac10df35 to your computer and use it in GitHub Desktop.
Fixes problems cause by micosoft making .NET natively available on Ubuntu 22.04. For more information, refer to https://github.com/dotnet/core/issues/7699.
#!/bin/bash
#
# Fixes problems caused by micosoft making .NET natively available on Ubuntu 22.04. For
# more information, refer to https://github.com/dotnet/core/issues/7699.
#
# NOTE:
# This script specifically install dotnet-sdk-6.0 on Ubuntu 22.04 and Linux Mint 21.
# If you are using a different version of Linux and/or dotnet-sdk, you may need to
# manually modify the script to suit your needs.
#
# Version: v1.0.0
# License: MIT License
# Copyright (c) 2022-2024 Hunter T. (StrangeRanger)
#
########################################################################################
####[ Global Variables ]################################################################
## Modify output text color.
C_YELLOW="$(printf '\033[1;33m')"
C_GREEN="$(printf '\033[0;32m')"
C_RED="$(printf '\033[1;31m')"
C_NC="$(printf '\033[0m')"
readonly C_YELLOW C_GREEN C_RED C_NC
## Identify distro and distro version.
if [[ -f /etc/os-release ]]; then
# shellcheck disable=SC1091
. /etc/os-release
readonly C_DISTRO="$ID"
readonly C_VER="$VERSION_ID" # Version: x.x.x...
readonly C_SVER=${C_VER//.*/} # Version: x
else
C_DISTRO=$(uname -s)
C_VER=$(uname -r)
readonly C_DISTRO C_VER
fi
## Identify bit and architecture type.
case $(uname -m) in
x86_64) readonly C_BITS="64" ;;
i*86) readonly C_BITS="32" ;;
armv*) readonly C_BITS="32" ;;
*) readonly C_BITS="?" ;;
esac
####[ Functions ]#######################################################################
####
# Ensure that .NET is set up to work with 'packages.microsoft.com'.
custom_dotnet() {
if (hash dotnet &>/dev/null && [[ $(dotnet --version) ]]) &>/dev/null; then
echo "${C_YELLOW}.NET runtime and .NET SDK seem to both be installed${C_NC}"
read -rp "Would you like to continue anyways? [y/N] " choice
choice="${choice,,}"
case "$choice" in
y*) ;;
*) echo "Exiting..."; exit 0 ;;
esac
elif (hash dotnet &>/dev/null && [[ ! $(dotnet --version) ]]) &>/dev/null; then
echo "${C_YELLOW}While the .NET runtime is installed, the .NET SDK is not${C_NC}"
else
echo "${C_YELLOW}Neither .NET runtime or .NET SDK are installed. There is" \
"nothihg to fix.${C_NC}"
echo "Exiting..."
exit 0
fi
echo "Uninstalling existing .NET Core 6.0 installation..."
sudo apt remove dotnet-sdk-6.0 -y
sudo apt autoremove -y
if [[ ! -f /etc/apt/preferences.d/custom-dotnet.pref ]]; then
echo "Upating prefered .NET Core install method..."
{
echo -e "Explanation: https://github.com/dotnet/core/issues/7699" \
"\nPackage: *" \
"\nPin: origin \"packages.microsoft.com\"" \
"\nPin-Priority: 1001" | sudo tee /etc/apt/preferences.d/custom-dotnet.pref
} || {
echo "${C_RED}Failed to create" \
"'/etc/apt/preferences.d/custom-dotnet.pref'${C_NC}" >&2
exit 1
}
fi
echo "Reinstalling .NET Core..."
# shellcheck disable=SC2015
sudo apt-get update \
&& sudo apt install dotnet-sdk-6.0 -y \
|| {
echo "${C_RED}Failed to reinstall .NET Core${C_NC}"
exit 1
}
echo "${C_GREEN}Done${C_NC}"
}
####[ Main ]############################################################################
echo -n "We will now fix 'No .NET SDKs were found'. "
read -rp "Press [Enter] to continue."
if [[ $C_BITS = 64 ]]; then
if [[ $C_DISTRO = "ubuntu" ]]; then
case "$C_VER" in
22.04) custom_dotnet ;;
esac
elif [[ $C_DISTRO = "linuxmint" ]]; then
case "$C_SVER" in
21) custom_dotnet ;;
esac
else
echo "${C_YELLOW}Your operaring systme version doesn't seem to need this fix"
echo "WARNING: By continuing, you accept that unexpected behaviors may occur${C_NC}"
read -rp "Would you like to continue anyways? [y/N] " choice
choice="${choice,,}"
case "$choice" in
y*) custom_dotnet ;;
*) echo "Exiting..."; exit 0 ;;
esac
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment