Troubleshoot .NET errors related to missing files on Linux

When you try to use .NET on Linux, commands such as dotnet new and dotnet run may fail with a message related to a file not being found, such as fxr, libhostfxr.so, or FrameworkList.xml. Some of the error messages may be similar to the following items:

  • System.IO.FileNotFoundException

    System.IO.FileNotFoundException: Could not find file '/usr/share/dotnet/packs/Microsoft.NETCore.App.Ref/5.0.0/data/FrameworkList.xml'.

  • A fatal error occurred.

    A fatal error occurred. The required library libhostfxr.so could not be found.

    or

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

    or

    A fatal error occurred, the folder [/usr/share/dotnet/host/fxr] does not contain any version-numbered child folders.

  • Generic messages about dotnet not found

    A general message may appear that indicates the SDK isn't found, or that the package has already been installed.

One symptom of these problems is that both the /usr/lib64/dotnet and /usr/share/dotnet folders are on your system.

Tip

Use the dotnet --info command to list which SDKs and Runtimes are installed. For more information, see How to check that .NET is already installed.

What's going on

These errors usually occur when two Linux package repositories provide .NET packages. While Microsoft provides a Linux package repository to source .NET packages, some Linux distributions also provide .NET packages. These distributions include:

  • Alpine Linux
  • Arch
  • CentOS
  • CentOS Stream
  • Fedora
  • RHEL
  • Ubuntu 22.04+

If you mix .NET packages from two different sources, you'll likely run into problems. The packages might place things at different paths and might be compiled differently.

Solutions

The solution to these problems is to use .NET from one package repository. Which repository to pick, and how to do it, varies by use-case and the Linux distribution.

My Linux distribution provides .NET packages, and I want to use them

  • Do you use the Microsoft repository for other packages, such as PowerShell and MSSQL?

    • Yes

      Configure your package manager to ignore the .NET packages from the Microsoft repository. It's possible that you've installed .NET from both repositories, so you want to choose one or the other.

      1. Remove the existing .NET packages from your distribution. You want to start over and ensure that you don't install them from the wrong repository.

        sudo dnf remove 'dotnet*' 'aspnet*' 'netstandard*'
        
      2. Configure the Microsoft repository to ignore .NET packages.

        echo 'excludepkgs=dotnet*,aspnet*,netstandard*' | sudo tee -a /etc/yum.repos.d/microsoft-prod.repo
        
      3. Reinstall .NET from the distribution's package feed. For more information, see Install .NET on Linux.

    • No

      1. Remove the existing .NET packages from your distribution. You want to start over and ensure that you don't install them from the wrong repository.

        sudo dnf remove 'dotnet*' 'aspnet*' 'netstandard*'
        
      2. Delete the Microsoft repository feed from your distribution.

        sudo dnf remove packages-microsoft-prod
        
      3. Reinstall .NET from the distribution's package feed. For more information, see Install .NET on Linux.

  • Do you use the Microsoft repository for other packages, such as PowerShell and MSSQL?

    • Yes

      Configure your package manager to ignore the .NET packages from the Microsoft repository. It's possible that you've installed .NET from both repositories, so you want to choose one or the other.

      1. Remove the existing .NET packages from your distribution. You want to start over and ensure that you don't install them from the wrong repository.

        sudo apt remove 'dotnet*' 'aspnet*' 'netstandard*'
        
      2. Create /etc/apt/preferences, if it doesn't already exist.

        touch /etc/apt/preferences
        
      3. Open /etc/apt/preferences in an editor and add the following settings, which prevents packages that start with dotnet, aspnetcore, or netstandard from being sourced from the Microsoft repository:

        Package: dotnet* aspnet* netstandard*
        Pin: origin "packages.microsoft.com"
        Pin-Priority: -10
        
      4. Reinstall .NET from the distribution's package feed. For more information, see Install .NET on Linux.

    • No

      1. Remove the existing .NET packages from your distribution. You want to start over and ensure that you don't install them from the wrong repository.

        sudo apt remove 'dotnet*' 'aspnet*' 'netstandard*'
        
      2. Delete the Microsoft repository feed from your distribution.

        sudo rm -f /etc/apt/sources.list.d/microsoft-prod.list
        sudo apt update
        
      3. Reinstall .NET from the distribution's package feed. For more information, see Install .NET on Linux.

I need a version of .NET that isn't provided by my Linux distribution

Configure your package manager to ignore the .NET packages from the distribution's repository. It's possible that you've installed .NET from both repositories, so you want to choose one or the other.

  1. Remove the existing .NET packages from your distribution. You want to start over and ensure that you don't install them from the wrong repository.

    sudo dnf remove 'dotnet*' 'aspnet*' 'netstandard*'
    
  2. Configure the Linux repository to ignore .NET packages.

    echo 'excludepkgs=dotnet*,aspnet*,netstandard*' | sudo tee -a /etc/yum.repos.d/<your-package-source>.repo
    

    Make sure to replace <your-package-source> with your distribution's package source.

  3. Reinstall .NET from the distribution's package feed. For more information, see Install .NET on Linux.

Configure your package manager to ignore the .NET packages from the distribution's repository. It's possible that you've installed .NET from both repositories, so you want to choose one or the other.

  1. Remove the existing .NET packages from your distribution. You want to start over and ensure that you don't install them from the wrong repository.

    sudo apt remove 'dotnet*' 'aspnet*' 'netstandard*'
    
  2. Create /etc/apt/preferences, if it doesn't already exist.

    touch /etc/apt/preferences
    
  3. Open /etc/apt/preferences in an editor and add the following settings, which prevents packages that start with dotnet, aspnetcore, or netstandard from being sourced from the distribution's repository.

    Package: dotnet* aspnet* netstandard*
    Pin: origin "<your-package-source>"
    Pin-Priority: -10
    

    Make sure to replace <your-package-source> with your distribution's package source, for example, on Ubuntu you may use archive.ubuntu.com in the US.

    Tip

    Use the apt-cache policy command to find the source:

    apt-cache policy '~ndotnet.*' | grep -v microsoft | grep '/ubuntu' | grep updates | cut -d"/" -f3 | sort -u
    
  4. Reinstall .NET from the Microsoft package feed. For more information, see Install .NET on Linux. If using Ubuntu, see My Ubuntu distribution doesn't include the .NET version I want, or I need an out-of-support .NET version.

Online references

Many other users have reported these problems. The following is a list of those issues. You can read through them for insights on what may be happening:

See also