Tuesday 9 April 2024

Resolving the “.NET Core SDK Not Found” Error on Ubuntu 22.0

 



If you’re developing .NET applications on Ubuntu, you might encounter a frustrating error: “A fatal error occurred. The folder [/usr/share/dotnet/host/fxr] does not exist.” This issue typically arises after updating .NET or the operating system itself, leading to a broken .NET environment. But fear not, this blog post will guide you through resolving this issue with new code examples, steering clear of previously suggested fixes.

Understanding the Root Cause

This error often stems from a mix-up between .NET packages installed from the official Ubuntu repositories and those from Microsoft’s APT repository. Initially, .NET Core was not available in Ubuntu’s repo, prompting users to add Microsoft’s repo. Now that .NET is included in Ubuntu’s official repositories, having packages from both sources can lead to conflicts.

Solution 1: Stick with Microsoft Packages

To resolve these conflicts and ensure your .NET environment is up-to-date, follow these steps:

  1. Remove Existing .NET Packages:

    First, clear out any existing .NET installations to prevent conflicts.

    sudo apt remove 'dotnet*' 'aspnetcore*' 'netstandard*'
    
  2. Configure APT to Prefer Microsoft Packages:

    Create a preference file for APT to prioritize Microsoft packages. This example names the file 99microsoft-dotnet.pref to follow Ubuntu’s naming conventions.

    echo 'Package: *
    Pin: origin "packages.microsoft.com"
    Pin-Priority: 1001' | sudo tee /etc/apt/preferences.d/99microsoft-dotnet.pref
    
  3. Install .NET SDK from Microsoft Repository:

    Now, update your package lists and install the desired .NET SDK version. Replace 8.0 with your preferred version if necessary.

    sudo apt update && sudo apt install -y dotnet-sdk-8.0
    

Solution 2: Opt for Ubuntu’s Official Packages

If you prefer using the official Ubuntu packages for .NET:

  1. Remove Microsoft’s APT Repository and Existing Packages:

    Clean your system of any Microsoft .NET packages and repositories.

    sudo apt remove 'dotnet*' 'aspnetcore*' 'netstandard*'
    sudo rm /etc/apt/sources.list.d/microsoft-prod.list*  # Removes Microsoft's repository list
    sudo apt update
    
  2. Install .NET SDK from Ubuntu’s Repositories:

    Install the .NET SDK using Ubuntu’s package manager. This example installs .NET 6; adjust the version number as needed.

    sudo apt install dotnet-sdk-6.0
    

Additional Tips

  • Verify Installation: After following either solution, verify your installation by checking the .NET version.

    dotnet --info
    
  • Side-by-Side Installation: .NET SDKs support side-by-side installation, allowing you to have multiple versions installed simultaneously.

The dreaded “/usr/share/dotnet/host/fxr does not exist” error can be a stumbling block for .NET developers on Ubuntu. By carefully managing your package sources and installations, you can overcome this issue. Whether you choose to stick with Microsoft’s packages for the latest features or opt for the stability of Ubuntu’s official packages, the key is consistency to avoid package conflicts. Happy coding!

Labels:

0 Comments:

Post a Comment

Note: only a member of this blog may post a comment.

<< Home