The required library hostfxr.dll could not be found

Problem

You are trying to run a .NET executable and you get the following error:

A fatal error occurred. The required library hostfxr.dll could not be found.
If this is a self-contained application, that library should exist in [C:\MyApp].
If this is a framework-dependent application, install the runtime in the global location [C:\Program Files\dotnet] or use the DOTNET_ROOT environment variable to specify the runtime location or register the runtime location in [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x64\InstallLocation].

The .NET runtime can be found here: <hopefully a helpful URL to a specific .NET version>

This means you need to install .NET.

Another symptom of this is when you try to start the app directly (instead of starting it from the command line), it closes immediately. I suggest starting the app from the command line to confirm the error.

Solution

You have two choices for installing .NET:

  • Install the specific .NET runtime your app needs (console, desktop, or ASP.NET Core).

-or-

  • Install the .NET SDK. This contains all of the runtimes (+ tools for development).

In most cases, I’d suggest installing the .NET SDK. Here are the .NET SDK download pages for a few versions:

Which .NET version?

Be sure to pick the right .NET version that your app needs. Otherwise you’ll get another error message like: It was not possible to find any compatible framework version.

These error messages usually have a URL at the bottom with the right .NET version you need. However, you can find this information yourself by looking in the .runtimeconfig file. Let’s say your app is called MyApp.exe. Look in MyApp.runtimeconfig for the framework version. For example, here’s an ASP.NET Core app running in .NET 5:

{
  "runtimeOptions": {
    "tfm": "net5.0",
    "framework": {
      "name": "Microsoft.AspNetCore.App",
      "version": "5.0.0"
    },
    "configProperties": {
      "System.GC.Server": true,
      "System.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization": false
    }
  }
}
Code language: JSON / JSON with Comments (json)