How to manage the installed versions of .NET on macOS

TL;DR

  • List installed versions of .NET:
dotnet --list-sdks
  • Install new versions of the .NET SDK by using the official installers found here.
  • To remove one or many versions of the .NET SDK  from your dev machine I recommend using .NET uninstall tool:
dotnet-core-uninstall remove [version]|--all --sdk

Introduction

As a .NET developer under macOS using Visual Studio For Mac I sometimes find myself having problems when a lot of different versions of the .NET SDK are installed.

Especially when working on a dev machine for a couple of month and after having updated Visual Studio For Mac a couple times it might be the case that the system is quite cluttered with all kinds of different .NET versions installed.

Or you had the brilliant idea to try out some preview version of some kind of .NET version and you discover that installing it somehow seemed to have broken your development environment. Visual Studio does not seem to compile your apps anymore. All of a sudden you get all kinds of weird errors seem to appear.

So in this article I like to give some guidelines on how to manage the installed versions of the .NET SDK under macOS, specifically how to check the installed versions, install new ones and remove one or many versions of .NET from your system, all this with the help of the terminal.

List installed .NET versions

As a first step I recommend to check which versions of .NET are actually installed on your system right now. You can do this by entering the following command in your terminal:

dotnet --list-sdks

The output you get might look something like this:

3.1.420 [/usr/local/share/dotnet/x64/sdk]
5.0.408 [/usr/local/share/dotnet/x64/sdk]
6.0.402 [/usr/local/share/dotnet/x64/sdk]

So in this case there are three versions of the .NET SDK installed.

Install new versions of .NET

As stated in the official documentation, there are different ways to install new versions of .NET on your system.

Now, as a developer being comfortable using the terminal it might be tempting to do a manual installation using an install script. But, as stated in documentation, the option of installing new versions of .NET using the provided dotnet-install.sh  script is supposed to be used mostly in continuous integration scenarios on a build server.

So the way I recommend to install a new version .NET is also by using the provided installers. For .NET 6 the section on the website to download it looks like this:

After executing the installer the new version should appear in the output of dotnet --list-sdks.

Remove .NET versions

As stated in the introduction, especially after you are operating your dev machine for a couple of month you might end up with quite a lot of different versions of the .NET SDK being installed, e.g. since when Visual Studio For Mac itself offers you a newer version of the .NET SDK via it's updater, the new version does not update the former one but it is actually installed in addition to it.

As described above, installing a new version of the .NET SDK using the provided installer is quite straight forward. Yet, removing one is not that intuitive, since a .NET SDK does not come with any sort of uninstaller.

But with the help of the official .NET uninstall tool (https://learn.microsoft.com/en-us/dotnet/core/additional-tools/uninstall-tool) it is not to hard to uninstall a certain version of the .NET SDK (or even all of them!).

You can get the tool from the official git repository: https://github.com/dotnet/cli-lab/releases.

Don't be confused by the downloaded executable. Although it is called "dotnet-core-uninstall", with the tool you can uninstall all kinds of versions of .NET (not just .NET Core).

Delete specific versions of the .NET SDK

Before uninstalling a specific .NET SDK I first recommend to let the tool output a list of all the SDKs it recognized by executing the following command in the terminal:

dotnet-core-uninstall list

The output you get should look something like this:

.NET Core SDKs:
  6.0.402  (x64)  [Used by Visual Studio for Mac. Specify individually or use —-force to remove]
  6.0.106  (x64)
  6.0.100  (x64)
  5.0.408  (x64)
  3.1.420  (x64)

As you can see, the tool even marks the version Visual Studio is using at the moment.

So let's assume we want to uninstall version 6.0.100 of the SDK. You can do this using the remove  subcommand of the tool and additionally providing the exact version number:

dotnet-core-uninstall remove 6.0.100 --sdk

As you can see, you also have to set the --sdk  switch to instruct the tool to remove the SDK of the provided version (yes there is also a --runtime switch.

After executing this command the specified version of the SDK should be removed. I recommend to double check this by executing the dotnet-core-uninstall list one more time.

Delete many SDKs

Removing specific versions of the .NET SDK that are no longer needed, is a good way to keep your dev machine clean (and also to free up some space on your hard drive).

But, sometimes, when things are really seem to go sideways and your development environment throws weird errors you have not seen before, for a fresh start it might a good idea to remove all .NET SDKs from you dev machine.

But instead of iterating through all the installed versions and removing them one by one the .NET uninstall tool actually provides some very handy extra options, one of them being --all. So by executing the following command you should be able to remove all installed .NET SDKs from your system:

dotnet-core-uninstall remove --all --sdk
The --all option actually removes all .NET SDKs excluding the one currently used by Visual Studio For Mac. If you want to also uninstall this one, you have to provide the -f as well.

Conclusion

In this post I described how you can manage the installed .NET SDKs under macOS by explaining how to inspect the currently installed ones, install new ones and uninstall specific or many versions.

This should help you to keep your dev machine nice and tidy, at least regarding the installed .NET versions.

Cleaning up the installed .NET SDKs on your dev machine from time to time is something I can definitely recommend. It does not only save you precious space on your disc but can also sometimes help with weird build errors.

Thanks for reading this article! If you liked it and it helped you in any way getting a grasp on a new topic, the best way to support this blog is just by sharing it with others. However, if you would like to support me and my work more directly you can just buy me a cup of coffee (which as we know will eventually be converted into code):