95

I can't access dotnet after an update. Reinstalling dotnet and vscode didn't help.

On Ubuntu 22.04, running dotnet --info produces the output:

A fatal error occurred. The folder [/usr/share/dotnet/host/fxr] does not exist

Slate
  • 221
  • 5
  • 14
Vladislav Volynets
  • 1,061
  • 1
  • 7
  • 6
  • Did you install dotnet using `apt`? Is it possible you have multiple instances of dotnet in your path? – PMF Sep 17 '22 at 15:12
  • 1
    Heppened for me multiple times on Ubuntu 22.04 with dotnet 6. Dotnet disapears while I was working on my projects. – profimedica Oct 11 '22 at 06:02

7 Answers7

263

When .NET (Core) was first released for Linux, it was not yet available in the official Ubuntu repo. So instead, many of us added the Microsoft APT repo in order to install it.

Now, the packages are part of the Ubuntu repo, and they are conflicting with the Microsoft packages. This error is a result of mixed packages.

So you need to pick which one you're going to use, and ensure they don't mix. Personally, I decided to stick with the Microsoft packages because I figured they'll be better kept up-to-date.

First, remove all existing packages to get to a clean state:

sudo apt remove dotnet*
sudo apt remove aspnetcore*
sudo apt remove netstandard*

Then, create a file in /etc/apt/preferences.d (I named mine 99microsoft-dotnet.pref, following the convention that files in such *.d directories are typically prefixed with a 2-digit number so that they sort and load in a predictable order) with the following contents:

Package: *
Pin: origin "packages.microsoft.com"
Pin-Priority: 1001

Then, the regular update & install:

sudo apt update
sudo apt install dotnet-sdk-6.0

If you would rather use the official Ubuntu packages, remove all the existing packages as above, but instead of creating the /etc/apt/preferences.d entry, just delete the Microsoft repo:

sudo rm /etc/apt/sources.list.d/microsoft-prod.list
sudo apt update
sudo apt install dotnet-sdk-6.0

However, note that the Microsoft repo contains other packages such as PowerShell, SQL Server Command-Line Tools, etc., so removing it may not be desirable.

I'm sure it's possible to make the APT config more specific to just these packages, but this is working for me for now. Hopefully Microsoft and Ubuntu work together to fix this soon.

More info on the issue and various solutions is available here:

Tobias J
  • 19,813
  • 8
  • 81
  • 66
  • 13
    This solved the issue for me on Ubuntu 22.04.1. I also decided to go with Microsoft packages. – Robert Hardy Oct 04 '22 at 11:12
  • Well I followed the above steps on 22.04.01 and I am still facing the same issue. Any help ? – codingfreak Oct 10 '22 at 18:17
  • @codingfreak sorry I don't really have further knowledge other than what I gained from the links I included at the end. I'd suggest asking on the GitHub issue with your specific error & see if they have feedback. – Tobias J Oct 11 '22 at 16:19
  • 4
    Thanks for this. Some guide linked on MS pages specified the required file location incorrectly. This indeed works. – Mikhail Orlov Oct 12 '22 at 09:50
  • This solves the problem on WSL with ubuntu 22. Thanks a lot! Would you mind explain a little bit more about how the `99microsoft-dotnet` file solves this issue? Thanks! – LeOn - Han Li Oct 25 '22 at 15:37
  • 1
    @LeOn-HanLi I just copied it from the linked github issue; it specifies that the priority of all packages coming from the microsoft repo is higher than all packages coming from the Ubuntu repo. This should be fine since the only conflicts are the dotnet ones. You could also specify that only the packages I mentioned (`dotnet`, `aspnetcore`, `netstandard`) should have a higher priority. More info on how to configure `apt` priority is here: https://wiki.debian.org/AptConfiguration – Tobias J Oct 27 '22 at 17:40
  • 1
    It seems that the typical extension for files in `/etc/apt/preferences.d` is `*.pref` according to [this askubuntu answer](https://askubuntu.com/a/1346577/445548). Hence, I'd rename `99microsoft-dotnet` to `99microsoft-dotnet.pref`. – Kevin Nov 18 '22 at 21:29
  • 1
    @Kevin thanks, my directory was empty so wasn't sure what to use. I updated the answer as suggested & also included info on why the "99" prefix is used. – Tobias J Nov 18 '22 at 22:41
  • 1
    Thank you so much! I had no idea about the mismatched packages, this solved my current problem as well as cleaned up some other weirdness (random crashes) I'd encountered using VSCode on Linux Mint 21. Thanks again!! – Klom Dark Jan 09 '23 at 02:31
  • 1
    Although this works I keep getting the same problem after few days – GorillaApe Jun 26 '23 at 07:17
  • @GorillaApe yeah I've had it occur again a couple times too. I'm guessing there's something about the settings file that isn't quite right but it's also just barely not annoying enough for me to want to dive into it further haha. Now that .NET/Core isn't moving as quickly as it was in the 2/3/5 days I might just switch back to the Ubuntu packages. – Tobias J Jun 26 '23 at 14:54
  • I think the recurring problem happens when the package version in the ubuntu repository is higher than the one in the microsoft repo. If I run `apt upgrade` with `--allow-downgrades` that seems to keep me on the correct version. – Mike Frank Jun 29 '23 at 13:49
  • @MikeFrank thanks, I figured it might have to do with their version numbers but didn't know about `--allow-downgrades`, I'll try that if it happens again! – Tobias J Jun 29 '23 at 14:37
28

I had a same error and I did install .Net with microsoft packages. I think the problem is if you have had older .Net or mixing scenarios regarding Ubuntu package and .Net packages(f.x via Jammy or PMC). BTW, I solved my problem to stick with Ubuntu packages and did run this bash script :

# First, try to remove/uninstall older .Net, if any, then install .Net 6
echo "$(tput setaf 3)Installing .Net 6$(tput sgr0)"
sudo apt remove 'dotnet*'
sudo apt remove 'aspnetcore*'
sudo apt remove 'netstandard*'
sudo rm /etc/apt/sources.list.d/microsoft-prod.list
sudo rm /etc/apt/sources.list.d/microsoft-prod.list.save
sudo apt update
sudo apt install dotnet6
sa.as
  • 381
  • 2
  • 4
1

I've got this error message after:

sudo apt install dotnet-host

This solved it for me:

sudo apt install dotnet-sdk-6.0
rundekugel
  • 1,118
  • 1
  • 10
  • 23
  • 1
    Did not work for me – nivs1978 Mar 31 '23 at 12:25
  • 1
    On a clean installation of Ubuntu 23.04, per Ubuntu's auto recommendation when attempting to execute `dotnet` but it wasn't installed, I also ran `apt install dotnet-host` and ran into this problem. I can confirm that using the suggested workaround (in my case, `pkg remove dotnet-host; pkg install dotnet-sdk-7.0`) worked. – Mahmoud Al-Qudsi May 19 '23 at 17:10
1
  1. First of all, you need to run using super user
sudo su

insert you password and go to seccond point:

  1. If you only want to run a .net code
install dotnet-host-7.0

2.1) If you want to develope systems using .net

apt install dotnet-sdk-7.0

If you want to remove olders versions:

sudo apt remove 'dotnet*'
sudo apt remove 'aspnetcore*'
sudo apt remove 'netstandard*'
Felipe Augusto
  • 1,341
  • 1
  • 16
  • 18
1

In case anyone encounters the same error right now (2023 July), I've tried a lot of the comments and blog posts, but this worked for me perfectly:

There is a part:

dotnet --version
A fatal error occurred. The folder [/usr/share/dotnet/host/fxr] does not exist
Great.

Since there, just follow along and it works :)

Not my solution, all respect to the author of this post:

https://alexanderzeitler.com/articles/fixing-my-bricked-dotnet-sdk-6-installation-on-xubuntu-ubuntu-after-dotnet-7-sdk-installation/

Tyler2P
  • 2,324
  • 26
  • 22
  • 31
himalczyk
  • 13
  • 3
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Tyler2P Jul 14 '23 at 13:53
1

In my case I had to remove /etc/apt/sources.list.d/mssql-release.list which had been installed as part of some other work where I needed ODBC drivers. It says mssql, but that's the same source as the MS dotnet packages, causing the interference.

To stick with just the Ubuntu packages:

sudo rm -f /etc/apt/sources.list.d/mssql-release.list
sudo apt purge dotnet* aspnetcore* netstandard*
sudo apt update
sudo apt install dotnet-sdk-6.0
Mendhak
  • 8,194
  • 5
  • 47
  • 64
0

I had this error when I created a new VS Code Dev Container using Ubuntu Jammy and selecting the "dotnet CLI" feature. I ended up switching my Dev Container to use Focal and the problem went away.

Alex Dresko
  • 5,179
  • 3
  • 37
  • 57