Use .NET 7 with Fedora 36 and Distrobox

A few days ago .NET 7 Preview 7 was officially announced by Microsoft. The current preview version of .NET will be the last preview for .NET 7 and the next version will be the first release candidate (RC). So it’s about time to download the latest binaries from Microsoft and try it out on my local developer machine.

Since this is still a preview (beta) version of .NET, I don’t want to mess up my current installation. As we all know, there are various options available for trying out new software versions or products like Docker or Virtual Machines (Gnome Boxes / VirtualBox) and so on.

In this post I want to show how to install the latest .NET Preview version on Fedora 36 Workstation without affecting the existing .NET installation. As an example I use Fedora 36 Workstation, but the steps mentioned below should also work fine under all Debian based distributions.

Setup

First, head over to the official .NET download page https://dotnet.microsoft.com/en-us/download/dotnet and select the latest preview version, which is .NET 7.0 in my case. Under SDK, select Linux and download the x64 binaries, or click this link

.NET 7.0 Preview Download

If you are already running .NET on your local linux system, then all the necessary dependencies should already be installed on your system. Otherwise visit https://docs.microsoft.com/en-us/dotnet/core/install/linux for further information.

Create a dedicated direcory for the preview dotnet installation and extract the downloaded package into it

mkdir -p $HOME/dotnet-preview
tar zxf dotnet-sdk-7.0.100-preview.7.22377.5-linux-x64.tar.gz -C $HOME/dotnet-preview

Define the necessary environment variables and that’s it

export DOTNET_ROOT=$HOME/dotnet-preview
export PATH=$HOME/dotnet-preview:$PATH

Check if everything is working properly with dotnet –info

dotnet --info

.NET SDK:
 Version:   7.0.100-preview.7.22377.5
 Commit:    ba310d9309

Runtime Environment:
 OS Name:     fedora
 OS Version:  36
 OS Platform: Linux
 RID:         fedora.36-x64
 Base Path:   /home/stef/dotnet-preview/sdk/7.0.100-preview.7.22377.5/

Note: We set the environment variables for the terminal session in which it was run, so when you restart the computer or even the terminal, the .NET SDK commands would no longer be found, or the “old” version would be invoked.

Distrobox

It is of course possible to store the corresponding variables in the shell profile (~/.bashrc), but this may affect the existing .NET installation. And exactly for this problem Distrobox is the perfect solution. From the official documentation:

Use any Linux distribution inside your terminal. Enable both backward and forward compatibility with software and freedom to use whatever distribution you’re more comfortable with. Distrobox uses podman or docker to create containers using the Linux distribution of your choice. The created container will be tightly integrated with the host, allowing sharing of the HOME directory of the user, external storage, external USB devices and graphical apps (X11/Wayland), and audio.

Checkout the official distrobox documentation to explore all the possibilities or watch the introduction video on Youtube by Jorge Castro.

Distrobox is included in Fedora and can be easily installed with dnf

sudo dnf install distrobox

Let’s create a new container based on Fedora 36 with a custom home directory under ~/.distrobox/dotnet-preview and named dotnet-preview

mkdir -p ~/.distrobox/dotnet-preview
distrobox create --image fedora:36 --name dotnet-preview --home ~/.distrobox/dotnet-preview

Enter into your new container with the distrobox enter command

distrobox enter dotnet-preview

After the distrobox has been started, the following commands are executed in the container you’ve just created (dotnet-preview). First, we install all updates and additional packages that are mandatory for dotnet

sudo dnf upgrade -y && dnf install nano libicu -y

The next steps are almost identical as before, with the difference that this time the dotnet package is extracted under /opt

sudo mkdir -p /opt/dotnet-preview
sudo tar zxf dotnet-sdk-7.0.100-preview.7.22377.5-linux-x64.tar.gz -C /opt/dotnet-preview

And this time, we can persist the environment variables without touching the configuration of our main system

echo 'export DOTNET_ROOT=/opt/dotnet-preview' >> ~/.bashrc
echo 'export PATH=$PATH:$DOTNET_ROOT:$DOTNET_ROOT/tools' >> ~/.bashrc

Now check your installation again with dotnet –info

dotnet --info

.NET SDK:
 Version:   7.0.100-preview.7.22377.5
 Commit:    ba310d9309

Runtime Environment:
 OS Name:     fedora
 OS Version:  36
 OS Platform: Linux
 RID:         fedora.36-x64
 Base Path:   /opt/dotnet-preview/sdk/7.0.100-preview.7.22377.5/

Now every time i want to check one of my projects for .NET 7 compatibility, i simply start my distrobox, navigate to the project and run a dotnet build or dotnet run.