With the recently release .NET 9, Microsoft has changed how they're publishing packages for Linux distributions. Previously, there was a Microsoft managed package repository available from which you could easily install different .NET versions, making it especially easy for CI agents to keep multiple .NET versions installed and updated.
However, starting with .NET 9, Microsoft no longer provides a feed for their distributions. So if you need the SDK or just the runtime, you need to use a feed managed by Canonical (the company behind Ubuntu). The problem is, there's no feed on which you can get multiple versions, e.g. .NET 8 and .NET 9 combined. Also, you can't just install from two different repositories, since the packages will have mutually exclusive dependencies, prohibiting you from installing .NET 8 and 9 at the same time. Also, the Ubuntu repositories seem to be a bit behind, so you could have to wait up to one week to get the latest updates for your SDKs.
I didn't properly dive into the reasons behind this change, I think it's mostly Microsoft wanting to integrate their .NET distribution into more official channels, but from a developer perspective, that doesn't seem very ideal for me.
So, yesterday evening, I tried to figure out how to get .NET 9 running while keeping all the other versions installed. Even though they're out of support, sometimes you just need to update an on-off CLI tool with a bugfix or new feature without having to update the whole project and the CI pipeline. Also, since both .NET 8 and .NET 9 are still in active support, we plan to also run tests for our products on both - so having them all on our CI agents was required.
There was no way that I found how to work with feeds, but luckily, you can just use the dotnet-install.sh script for manual SDK installations. I found it pretty easy to use, but there's a gotcha: If you've already got some .NET versions installed, they're probably by default in a different directory than what the installation script uses. In that case, though, before installation, you can simply run dotnet --list-sdks to get an output like this:
dotnet --list-sdks
6.0.428 [/usr/share/dotnet/sdk]
7.0.410 [/usr/share/dotnet/sdk]
8.0.404 [/usr/share/dotnet/sdk]
Here, we see that .NET is installed to /usr/share/dotnet, which we can then pass to the installation script as directory like this:
./dotnet-install.sh --install-dir /usr/share/dotnet --channel 9.0
Running dotnet --list-sdks again should now list .NET 9 as well, and you're good to go. There's no need to set any more environment variables. The only thing to keep in mind is that you now have to manually update .NET via the install script, since apt-get won't do those for the .NET 9 sdk now.
Happy developing!