Installing the dotnet SDK on Ubuntu under WSL

Jul 10, 2023

Introduction

Over the past few months I found myself struggling to get a working installation of the dotnet SDK on Ubuntu running off Windows Subsystems for Linux (WSL). This is partly due to the fact new installation methods have been added over the years, others have been deprecated, and it’s really easy to inadvertently update from one installation method to another, messing up everything by the same occasion.

The documentation overview on the topic currently suggests the following methods:

  • Package feed (Canonical)
  • Package feed (Microsoft)
  • Script

And fails to mention snap (unavailable on WSL anyway). The Canonical package feed option has the major drawback of not having all the patch versions, and the script option doesn’t update the SDK along with other packages. It seems natural to me that anyone would want the Microsoft package feed, but the scripts that are provided are only partial and often lead to a corrupted state.

Solution

To properly install the SDK from the Microsoft feed, here are the steps I’m following.

# source https://learn.microsoft.com/en-us/dotnet/core/install/linux-package-mixup?pivots=os-linux-ubuntu#i-need-a-version-of-net-that-isnt-provided-by-my-linux-distribution
# remove pre-existing packages
sudo apt remove 'dotnet*' 'aspnet*' 'netstandard*'
# set the default ubuntu package feed as low priority for dotnet packages so they don't conflict
echo -e "Package: dotnet* aspnet* netstandard*\nPin: origin \"archive.ubuntu.com\"\nPin-Priority: -10" | sudo dd of=/etc/apt/preferences oflag=append conv=notrunc

# source https://learn.microsoft.com/en-us/dotnet/core/install/linux-ubuntu#register-the-microsoft-package-repository
# Get Ubuntu version
declare repo_version=$(if command -v lsb_release &> /dev/null; then lsb_release -r -s; else grep -oP '(?<=^VERSION_ID=).+' /etc/os-release | tr -d '"'; fi)
# Download Microsoft signing key and repository
wget https://packages.microsoft.com/config/ubuntu/$repo_version/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
# Install Microsoft signing key and repository
sudo dpkg -i packages-microsoft-prod.deb
# Clean up
rm packages-microsoft-prod.deb
# Update packages
sudo apt update

# install dotnet sdk (you can change the version to what you'd like)
sudo apt-get install dotnet-sdk-7.0

Last edited Apr 15, 2024 by Vincent Biret


Tags: