Ubuntu .Net 7 Install Can't Find SDK

4.7k Views Asked by At

I am running Ubuntu 22.04 and trying to install .NET 7.0.5

I have tried multiple different methods of installing .NET 7, none of them seem to allow me to use the dotnet build or dotnet publish command.

It appears to be some kind of issue with Ubuntu not recognizing that my SDKs are installed.

dotnet publish -c Release -r linux-x64 --self-contained false

You must install or update .NET to run this application.

App: /usr/share/dotnet/sdk/7.0.302/dotnet.dll
Architecture: x64
Framework: 'Microsoft.NETCore.App', version '7.0.5' (x64)
.NET location: /usr/share/dotnet/

No frameworks were found.

I tried to show the dotnet version:

root@localhost:~# dotnet --version
    The command could not be loaded, possibly because:
      * You intended to execute a .NET application:
          The application '--version' does not exist.
      * You intended to execute a .NET SDK command:
          No .NET SDKs were found.

Dotnet information:

root@localhost:~# dotnet --info

    Host:
      Version:      7.0.5
      Architecture: x64
      Commit:       8042d61b17

    .NET SDKs installed:
      No SDKs were found.

    .NET runtimes installed:
      Microsoft.AspNetCore.App 7.0.5 [/usr/lib/dotnet/shared/Microsoft.AspNetCore.App]
      Microsoft.NETCore.App 7.0.5 [/usr/lib/dotnet/shared/Microsoft.NETCore.App]

    Other architectures found:
      None

    Environment variables:
      Not set

    global.json file:
      Not found

Where is dotnet according to my system?

root@localhost:~# whereis dotnet
        dotnet: /usr/bin/dotnet /usr/lib/dotnet /etc/dotnet /usr/share/dotnet 
                /usr/share/man/man1/dotnet.1.gz

Could my PATH not be correctly set or something? How would I fix this issue?

Thanks!

3

There are 3 best solutions below

5
On BEST ANSWER

Look at the paths carefully in the error messages:

App: /usr/share/dotnet/sdk/7.0.302/dotnet.dll
Architecture: x64
Framework: 'Microsoft.NETCore.App', version '7.0.5' (x64)
.NET location: /usr/share/dotnet/

This says the SDK is supposed to be at /usr/share/dotnet.

    .NET SDKs installed:
      No SDKs were found.

    .NET runtimes installed:
      Microsoft.AspNetCore.App 7.0.5 [/usr/lib/dotnet/shared/Microsoft.AspNetCore.App]
      Microsoft.NETCore.App 7.0.5 [/usr/lib/dotnet/shared/Microsoft.NETCore.App]

This says the runtimes are at /usr/lib/dotnet/, and the SDKs are not found.

You have somehow installed some parts of .NET in /usr/share/dotnet and others in /usr/lib/dotnet. That's not going to work, because .NET expects everything to be in a single location - either /usr/lib/dotnet/ or /usr/share/dotnet. Because parts are installed in different locations, .NET gets completely confused and gives you these misleading errors.

I suspect this is because you installed the .NET packages from two different providers - the runtime is from the Ubuntu repository (and installs at /usr/lib/dotnet) while the SDK is from the Microsoft repository (and installs at /usr/share/dotnet).

To fix this, I would:

  • Uninstall all .NET packages: sudo apt remove 'dotnet*' 'aspnet*' 'netstandard*'
  • Remove the Microsoft repository: sudo rm -f /etc/apt/sources.list.d/microsoft-prod.list
  • Install .NET (which should pull down the SDK and the runtime from the Ubuntu repository): sudo apt install dotnet-sdk-7.0

For more details, and some other solutions to this problem, see https://learn.microsoft.com/en-us/dotnet/core/install/linux-package-mixup?pivots=os-linux-ubuntu#solutions

1
On

If you want to use a version from the Microsoft registry, they provide the instructions.

This is similar to the other answer where we first clear our installed dotnet packages that are from different sources, but then we tell apt to prefer the dotnet related packages by lowering the preference for the official registry.

Steps are simplified here, reproduced from the link due to community guidelines:

  1. Remove the existing packages: sudo apt remove 'dotnet*' 'aspnet*' 'netstandard*'

  2. If you run dotnet --info and it still has values for SDKs, Runtimes, etc., purge them. Example: sudo apt remove --purge 'dotnet*' 'aspnet*' 'netstandard*'

  3. Create the preferences file, if it doesn't exist: touch /etc/apt/preferences

  4. Run this command to see what the current apt package sources are, you'll need these in the next step: apt-cache policy '~ndotnet.*' | grep -v microsoft | grep '/ubuntu' | cut -d"/" -f3 | sort -u

  5. Open /etc/apt/preferences and add the following block to it, once per source returned from the previous step. Replace <your-package-source> with the source from the previous step. Mine were archive.ubuntu.com and security.ubuntu.com, but these can apparently vary by location.

    Package: dotnet* aspnet* netstandard*
    Pin: origin "<your-package-source>"
    Pin-Priority: -10
    
  6. Save and exit the preferences file.

  7. Reinstall your dotnet package(s).

I ran

sudo apt install dotnet-sdk-7.0=7.0.306-1

because I wanted the latest version. Ubuntu was on 7.0.109. Run the following to see what versions are available:

apt list -a dotnet-sdk-7.0

The output shows a non-ubuntu package installed, and the lower-versioned ubuntu package midway down the list. enter image description here

Update 2023-08-29: DNS issues?

From another WSL2 instance on a different system, I tried a restart between removing, and then after installing .NET with no luck. Note that I also tried a purge (not called out above, but it's in the list, now) with no luck.

My specific issue this time was listed under the OUTPUT area, normally sharing space with the DEBUG CONSOLE and TERMINAL, when ".NET Runtime" was selected in the drop down list. I believe this is used by one of the VS Code C# extensions. Without this working, I couldn't generate build tasks nor launch configs by using the debug play button. (I assume manual dotnet build commands may have worked, but I didn't test that. I like my F5 key for build/run!) I don't believe Intellisense worked, either.

Before my fix below, the output window would show "Downloading .NET version(s) ........", and it would never complete. After fixing, it resolves with a "Done!" message, as shown below. output window for .NET Runtime, post-fix

This system did not yet have my WSL2 DNS fixes, included below. I'm not sure if this is exactly what fixed the problem, but it fixed it for me: gist for update to wsl.conf

You can get to the original thread, which spans from Windows 10 to current versions of 11, via a comment in the last line of my gist. Or this helpful comment on the original gist seems helpful if you want to configure it from the Windows side. I haven't tested that one.

The ultimate update is to add the following command to /etc/wsl.conf, which is supported in WSL2 on Windows 11 (maybe newer versions of 10?):

[boot]
    command="printf \"nameserver 1.1.1.1\n$(cat /etc/resolv.conf)\nnameserver 8.8.8.8\nnameserver 1.0.0.1\n\" > /etc/resolv.conf"

This injects public DNS nameservers around your local machine's bridge IP (bridge IP comes from /etc/resolv.conf and is generated when the WSL service starts up or the distro is booted (not sure exactly when, feel free to update the specifics).

The Powershell version does the same, but it takes the configured nameservers from the Windows OS and modifies the resolv.conf, which may be a better option for security with work machines (VPNs and whatnot).

4
On

The SDK is installed at /usr/share/dotnet/sdk, but the command dotnet only recognizes the /usr/lib/dotnet/ path.

So after moving all the contents to it correct place, everything work for me:

sudo cp -r /usr/share/dotnet/* /usr/lib/dotnet/

dotnet --list-sdks

7.0.302 [/usr/lib/dotnet/sdk]