5

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!

Scottish Smile
  • 455
  • 7
  • 22
  • You're trying to use .NET SDK when only the .NET runtime is installed on your system. You need to install the .NET 7 SDK as well. Have you installed the SDK? How? – Panagiotis Kanavos May 26 '23 at 13:40
  • Ubuntu 22.04 [added .NET 7 recently](https://learn.microsoft.com/en-us/dotnet/core/install/linux-ubuntu-2204) which means you can run `sudo apt-get update && sudo apt-get install -y dotnet-sdk-7.0` to install the .NET 7 SDK – Panagiotis Kanavos May 26 '23 at 13:43
  • I've tried 4 or so methods. Using the MS repository, using the MS scripted install, using the ununtu repository as mentioned above, installing 6.0 first. Same result. No SDKs were found. – Scottish Smile May 27 '23 at 00:46
  • Maybe docker is the way to go here? I think this is an ubuntu bug it is not installing .net in the right locations. I am not the only one. – Scottish Smile May 27 '23 at 00:52
  • This seems to be the same issue? https://github.com/dotnet/sdk/issues/27082 – Scottish Smile May 27 '23 at 00:56
  • Thanks for the help guys, I had to reboot the ubuntu VM after doing the remove commands to get it to work. "reboot now" – Scottish Smile May 27 '23 at 01:06

3 Answers3

13

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

omajid
  • 14,165
  • 4
  • 47
  • 64
  • Unfortunately, the result is still the same after running your commands. I have looked at that MS guide before (and several others) and none work. Thanks for the help though. Maybe using docker is the way to go? – Scottish Smile May 27 '23 at 00:44
  • 1
    Sorry Omajid, when I rebooted my ubuntu VM after doing your remove commands it worked. (you missed the ending apostrophy ' in sudo apt remove 'dotnet*' 'aspnet*' 'netstandard*' if you can fix it?) Thanks for the help! It was driving me crazy! – Scottish Smile May 27 '23 at 01:05
  • 1
    Thank you omajid, this was exactly what I needed after my .NET 7 apps spontaneously couldn't find the framework any longer, coincident with 7 being added to the official repository, presumably moving install locations around. This gave me a blank slate and everything started working again. – Adrian Hand Jun 16 '23 at 11:16
  • 1
    Can confirm the above steps work. I did not have to restart the server, my app started working again as soon as dotnet was re-installed from the Ubuntu repository. I had both Microsoft and Ubuntu repositories and didn't have Microsoft set as the priority to use for updates. – Bill Kindle Jun 25 '23 at 14:33
  • I would recommend removing "you" from the line "You have somehow installed some parts of .NET" and other such parts of this answer. I came across this problem not by anything I did but what Linux Mint and .NET did. I didn't do anything but run `apt-get update`, `apt-get upgrade`, and Linux Mint's Update Manager. In fact, after removing everything and then re-running `sudo apt-get install dotnet-sdk-7.0`, I note that it is putting things into the separate directories. – bmitc Aug 28 '23 at 08:56
3

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]
Tien La
  • 43
  • 1
  • 7
  • 1
    Thanks, but this is very confusing as these paths can change depending on the linux OS used and the version of ubuntu used. It's much easier to just wipe the VM and reinstall using the microsoft repo directly " sudo apt install dotnet-sdk-7.0 " now that we have that option. – Scottish Smile Jun 06 '23 at 23:41
  • @ScottishSmile I actually ran into the original problem using Ubuntu 22.04 with WSL2 and the microsoft feed. I basically did the same as this answer, but copied only the sdk directory. If you want versions that are newer or different from supported versions on the default registry, you're supposed to use the MS registry. – ps2goat Jul 27 '23 at 18:50
  • I did find Microsoft's official steps if you want to continue to use the Microsoft package repo, and added them as a separate answer. – ps2goat Jul 27 '23 at 19:15
  • This is the only thing that has worked for me. I have no idea what caused this. I simply ran `apt-get update`, `apt-get upgrade`, and then Linux Mint's Update Manager, and then dotnet got into this weird state. Even removing everything, the official install puts everything back into these separate directories. It is unclear what made this happen and what can be done to permanently fix it, but the copy got the dotnet command working again. Thanks! – bmitc Aug 28 '23 at 08:58
0

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).

ps2goat
  • 8,067
  • 1
  • 35
  • 68