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

I've created a worker(cli: dotnet new worker) project in ubuntu 20.04 which run on a raspberry pi, but get error when start the service via systemd. the error message is: A fatal error occurred...

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and
privacy statement. We’ll occasionally send you account related emails.

Already on GitHub?
Sign in
to your account

Comments

@huangchinlin

I’ve created a worker(cli: dotnet new worker) project in ubuntu 20.04 which run on a raspberry pi, but get error when start the service via systemd.

the error message is: A fatal error occurred. The required library libhostfxr.so could not be found.

I have installed the dotnet sdk and runtime, but the situation is the same.


dotnet —info

.NET SDK (reflecting any global.json):
 Version:   5.0.101
 Commit:    d05174dc5a

Runtime Environment:
 OS Name:     ubuntu
 OS Version:  20.04
 OS Platform: Linux
 RID:         ubuntu.20.04-arm64
 Base Path:   /usr/local/share/dotnet/sdk/5.0.101/

Host (useful for support):
  Version: 5.0.1
  Commit:  b02e13abab

.NET SDKs installed:
  5.0.101 [/usr/local/share/dotnet/sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 5.0.1 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 5.0.1 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]

ENV:

DOTNET_ROOT=/usr/local/share/dotnet
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin:/usr/local/share/dotnet

Program.cs

    public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .UseSystemd()
                .ConfigureServices((hostContext, services) =>
                {
                    services.AddHostedService<Worker>();
                });
    }

Please advise the solution for this. Thanks.

@dotnet-issue-labeler

I couldn’t figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

@wli3
wli3

transferred this issue from dotnet/sdk

Feb 9, 2021

@dotnet-issue-labeler

I couldn’t figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

@wli3
wli3

removed their assignment

Feb 9, 2021

@msftbot

Tagging subscribers to this area: @vitek-karas, @agocke
See info in area-owners.md if you want to be subscribed.

Issue Details


I’ve created a worker(cli: dotnet new worker) project in ubuntu 20.04 which run on a raspberry pi, but get error when start the service via systemd.

the error message is: A fatal error occurred. The required library libhostfxr.so could not be found.

I have installed the dotnet sdk and runtime, but the situation is the same.


dotnet —info

.NET SDK (reflecting any global.json):
 Version:   5.0.101
 Commit:    d05174dc5a

Runtime Environment:
 OS Name:     ubuntu
 OS Version:  20.04
 OS Platform: Linux
 RID:         ubuntu.20.04-arm64
 Base Path:   /usr/local/share/dotnet/sdk/5.0.101/

Host (useful for support):
  Version: 5.0.1
  Commit:  b02e13abab

.NET SDKs installed:
  5.0.101 [/usr/local/share/dotnet/sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 5.0.1 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 5.0.1 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]

ENV:

DOTNET_ROOT=/usr/local/share/dotnet
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin:/usr/local/share/dotnet

Program.cs

    public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .UseSystemd()
                .ConfigureServices((hostContext, services) =>
                {
                    services.AddHostedService<Worker>();
                });
    }

Please advise the solution for this. Thanks.

Author: huangchinlin
Assignees:
Labels:

area-Host, untriaged

Milestone:

@vitek-karas

I assume it works if you run it from shell, right?

Would it be possible to collect a host trace? Before starting the process set env. variables:

COREHOST_TRACE=1
COREHOST_TRACEFILE=/tmp/host_trace.txt

It should produce the /tmp/host_trace.txt, which should include more information about the failure. Note that this file contains some env. variable values, and directory/file names from the machine, so only share it if you’re OK with sharing that information.

At the very least it should contain the path where it was looking for hostfxr.

I would also expect to see more in the error message. It should either say where it was look (in the error message itself), or there should be more lines to the error message with things like If this is a self-contained application, that library should exist in and so on.

Is this a framework dependent or self-contained application?

@huangchinlin

I assume it works if you run it from shell, right?

Would it be possible to collect a host trace? Before starting the process set env. variables:

COREHOST_TRACE=1
COREHOST_TRACEFILE=/tmp/host_trace.txt

It should produce the /tmp/host_trace.txt, which should include more information about the failure. Note that this file contains some env. variable values, and directory/file names from the machine, so only share it if you’re OK with sharing that information.

At the very least it should contain the path where it was looking for hostfxr.

I would also expect to see more in the error message. It should either say where it was look (in the error message itself), or there should be more lines to the error message with things like If this is a self-contained application, that library should exist in and so on.

Is this a framework dependent or self-contained application?

Thanks for reply, Vitek-karas.
My app is framework dependent, and can run it well from the shell.

I have built again the environment, and also install SDK which version is 5.0.103 thru dotnet-install.sh.
The sample worker app can run very well from both shell and systemd.

The difference to last environment is install the dotnet sdk by apt or dotnet-install.sh, in last environment, I used apt to install sdk. I think it is root cause to this situation because raspberry pi is arm64 architecture, the correct way to install sdk and runtime is thru dotnet-install.sh.

@msftbot
msftbot
bot

locked as resolved and limited conversation to collaborators

Mar 31, 2021

 ❯ dotnet ef   
A fatal error occurred. The required library libhostfxr.so could not be found.
If this is a self-contained application, that library should exist in [/home/ru/.dotnet/tools/.store/dotnet-ef/6.0.2/dotnet-ef/6.0.2/tools/netcoreapp3.1/any/].
If this is a framework-dependent application, install the runtime in the global location [/usr/share/dotnet] or use the DOTNET_ROOT environment variable to specify the runtime location or register the runtime location in [/etc/dotnet].

The .NET runtime can be found at:
  - https://aka.ms/dotnet-core-applaunch?missing_runtime=true&arch=x64&rid=arch-x64&apphost_version=6.0.2

~ 

dotnet ef not working
After running the command is not working so what can I do?

asked Feb 24, 2022 at 12:29

Raul Penate's user avatar

Solved by adding this in whatever part of shell profile (it can be at the end):

# Netcore
export PATH=$PATH:$HOME/.dotnet/tools
export DOTNET_ROOT=$HOME/.dotnet
export PATH=$PATH:$DOTNET_ROOT

Your shell profile can be in one of these files:

  • Bash Shell: ~/.bash_profile, ~/.bashrc
  • Korn Shell: ~/.kshrc or .profile
  • Z Shell: ~/.zshrc or .zprofile

Don’t forget to close and open your terminal, so env variables can load.

More here

To check if everything is ok run dotnet ef, you should get something like this:

❯ dotnet ef

                     _/__       
               ---==/    \      
         ___  ___   |.    |    
        | __|| __|  |  )   \   
        | _| | _|   _/ |  //|\ 
        |___||_|       /   \/\

Entity Framework Core .NET Command-line Tools 6.0.2

Usage: dotnet ef [options] [command]

Options:
  --version        Show version information
  -h|--help        Show help information
  -v|--verbose     Show verbose output.
  --no-color       Don't colorize output.
  --prefix-output  Prefix output with level.

Commands:
  database    Commands to manage the database.
  dbcontext   Commands to manage DbContext types.
  migrations  Commands to manage migrations.

Use "dotnet ef [command] --help" for more information about a command.

answered Feb 24, 2022 at 12:29

Raul Penate's user avatar

Raul PenateRaul Penate

1702 silver badges15 bronze badges

1

I got this error (and many others) while using the Snap version.
These problems all went away when I switched to the package manager version.

answered Apr 27, 2022 at 4:25

CrazyPyro's user avatar

CrazyPyroCrazyPyro

3,1172 gold badges30 silver badges39 bronze badges

.NET (previously named .NET Core) is a FOSS software framework from Microsoft for C#, Visual Basic, and F#. It is designed to be cross-platform, modular and apt for modern applications, as opposed to its predecessor, the .NET Framework.

Installation

If you only want to run .NET managed applications, install the dotnet-runtime package.

To build apps with .NET, install dotnet-sdk as well.

Lastly, to build dynamic web sites, applications, and services using ASP.NET Core, install aspnet-runtime.

Microsoft recommends using Visual Studio Code , their Electron-based IDE, to build & debug .NET apps.

Tip: Add ~/.dotnet/tools to PATH, otherwise dotnet tools will not work from shell.

Install multiple versions manually

You can install multiple versions of the .NET SDK or runtime side by side by using the dotnet-install.sh script provided by the .NET Foundation. You can find the documentation of the script here.

For instance, this command would install the latest version found in the «STS»(Standard Term Support) channel in /usr/share/dotnet:

# ./dotnet-install.sh --install-dir /usr/share/dotnet -channel STS -version latest

You may want to simulate the installation first by using the -Dryrun flag.

Once installed, you can verify the SDKs available:

$ dotnet --list-sdks
2.2.108 [/usr/share/dotnet/sdk]
3.0.103 [/usr/share/dotnet/sdk]
$ dotnet --version
3.0.103

Uninstall manually installed version

You may want to remove outdated versions installed with dotnet-install.sh. The automated .NET Uninstall Tool does still not support Linux, so the desinstallation has to be done manually.

$ dotnet --list-sdks
5.0.100 [/usr/share/dotnet/sdk]
5.0.102 [/usr/share/dotnet/sdk]

Desinstallation of the sdk:

$ SDK_VERSION="5.0.100"
$ DOTNET_UNINSTALL_PATH="/usr/share/dotnet"
# rm -rf $DOTNET_UNINSTALL_PATH/sdk/$SDK_VERSION

Using dotnet-install.sh also dotnet host and shared packages are getting installed, which may have to get removed additionally depending on the release.

Complete deinstallation of the .NET Version (sdk, host, shared):

$ SDK_VERSION="5.0.100"
$ DOTNET_VERSION="5.0.0"
$ DOTNET_UNINSTALL_PATH="/usr/share/dotnet"
# rm -rf $DOTNET_UNINSTALL_PATH/sdk/$SDK_VERSION
# rm -rf $DOTNET_UNINSTALL_PATH/shared/Microsoft.NETCore.App/$DOTNET_VERSION
# rm -rf $DOTNET_UNINSTALL_PATH/shared/Microsoft.AspNetCore.All/$DOTNET_VERSION
# rm -rf $DOTNET_UNINSTALL_PATH/shared/Microsoft.AspNetCore.App/$DOTNET_VERSION
# rm -rf $DOTNET_UNINSTALL_PATH/host/fxr/$DOTNET_VERSION

Install multiple versions via AUR

Some of the AUR dotnet packages are made to be installed alongside each other. Only one host package (dotnet-host-binAUR or dotnet-host) is needed containing the command-line tool and you can install any of the available SDKs and Runtimes (latest packages of all major versions) next to it. List of compatible packages:

  • dotnet-host-binAUR, dotnet-runtime-binAUR, aspnet-runtime-binAUR, dotnet-sdk-binAUR
  • dotnet-host-preview-binAUR, dotnet-runtime-preview-binAUR, aspnet-runtime-preview-binAUR, dotnet-sdk-preview-binAUR
  • dotnet-runtime-3.0AUR, aspnet-runtime-3.0AUR, dotnet-sdk-3.0AUR,
  • dotnet-runtime-2.2AUR, aspnet-runtime-2.2AUR, dotnet-sdk-2.2AUR, dotnet-sdk-2.2-vs2017AUR
  • dotnet-runtime-2.1AUR, aspnet-runtime-2.1AUR, dotnet-sdk-2.1AUR

Install PowerShell Core

You can install PowerShell Core as a «global» tool also [1] [2]

# dotnet tool install --global PowerShell

to update to the current version

# dotnet tool update --global PowerShell

Telemetry

Telemetry is enabled by default but can be disabled by setting environment variable DOTNET_CLI_TELEMETRY_OPTOUT=1.

Tab-Completion

All dotnet programs that use System.CommandLine.Parser to parse their arguments have auto-complete support. Enabling it just requires adding a few lines to your .bashrc / .zshrc file, as described in their documentation. Instructions for standalone binaries are here.

Troubleshooting

It was not possible to find any compatible framework version

If you get the following error when you try to run a newly created project, you no longer need to set a DOTNET_ROOT variable as described in the solutions of various GitHub issues. Arch’s dotnet package (as of 3.1) installs it to the Microsoft recommended location of /usr/share/dotnet.

$ dotnet run
It was not possible to find any compatible framework version
The framework 'Microsoft.AspNetCore.App', version '3.1.0' was not found.
  - No frameworks were found.

You can resolve the problem by installing the specified framework and/or SDK.

The specified framework can be found at:
  - https://aka.ms/dotnet-core-applaunch?framework=Microsoft.AspNetCore.App&framework_version=3.1.0&arch=x64&rid=arch-x64

This is caused because the runtime is shipped as a separate package in Arch. You just need to make sure you have the aspnet-runtime package installed as well.

«the required library libhostfxr.so could not be found» error

Some of the dotnet SDK tools (for example libman, dotnet-watch etc.) may expect you to have the environment variable DOTNET_ROOT pre-configured. If it is not, an error like this one could be observed: [3]

A fatal error occurred, the required library libhostfxr.so could not be found.
If this is a self-contained application, that library should exist in [/home/my_user/.dotnet/tools/.store/microsoft.web.librarymanager.cli/1.0.172/microsoft.web.librarymanager.cli/1.0.172/tools/netcoreapp2.1/any/].
If this is a framework-dependent application, install the runtime in the default location [/usr/share/dotnet] or use the DOTNET_ROOT environment variable to specify the runtime location.

The workaround is to manually export DOTNET_ROOT in your shell:

~/.bashrc
export DOTNET_ROOT=/opt/dotnet

This happens after an update. The currently running shell / login session is storing environment variables for the dotnet SDK version different from one installed. Restarting the shell or logging in again should fix this.

SDK specified could not be found

This is believed to caused by a conflict between the Mono and MSBuild SDK libs and the dotnet ones. To fix this export the path manually in your shell (replacing the version number as necessary) e.g:

~/.bashrc
export MSBuildSDKsPath=$( echo /usr/share/dotnet/sdk/3.*/Sdks );

dotnet command still installed

Installed packages do not uninstall dotnet-host, so uninstall dotnet-host

See also

  • .NET homepage
  • The .NET Foundation
  • .NET API Browser

A fatal error occured. The required library hostfxr.dll could not be found

Today in this article, we will cover below aspects,

  • Issue Description
  • Resolution
  • Resolution 1 – Build Application as FDD(Framework dependent deployment)
  • hostfxr.dll location
  • Resolution 2 – Build Application as Self Content Deployment
  • hostfxr.dll location

Issue Description

The application throws the below error in the runtime,

A fatal error occurred. The required library hostfxr.dll could not be found

A fatal error occured The required library hostfxrdll could not be found 1

Resolution

The issue I found to be due to the .NET runtime not being available in the target machine.

There could be few basic reasons for the error. Generally, developers tend to deploy the application in a new environment assuming it will work as-is.

When installing the application to the new environment, if the target machine has the required .NET version already installed then the application should work without any issue.

If the required .NET core SDK/runtime is not installed then one can follow any of the below approaches discussed. An approach using Self-content deployment doesn’t require a .NET Core SDK/Runtime to be installed on the target machines.

Let’s see both the approaches in detail,

Resolution 1 – Build Application as FDD(Framework dependent deployment)

If you are building an application using an approach called Framework dependent deployment (FDC) approach, then it is expected that the target server or environment has the .NET Core SDK and runtime installed.

For more details, please refer Self Contained Vs Framework Dependent Deployment

Typically your application supported Runtime/SDK will be available in the below folder on the target machine,

C:Program Filesdotnet

hostfxr.dll location

hostfxr.dll can be found below shared location depending on the target .NET version used,

required library hostfxrdll could not be found
hostfxrdll location

Resolution: Install NET Core on the target machine/environment. Your application will work just fine as a developer machine.

If you want an alternative solution without installing the .NET Core version, please see below,

Resolution 2 – Build Application as Self Content Deployment

If you are building an application using an approach, then you make sure the application deployable is self-content and can run on its own.

For more details, please refer Self Contained Vs Framework Dependent Deployment

This can be achieved by building your application using the below build command,

For Linux

dotnet publish -r linux-x64 --self-contained true -c release

For Windows10

dotnet publish -r win10-x64 --self-contained true -c release

OR

For Windows Server

dotnet publish -r win-x64 --self-contained true -c release

With this approach, you achieve the below advantage,

  • App doesn’t required to download and install. NET.
  • Application is isolated from other .NET apps
  • Application doesnt use a locally installed shared runtime if available
  • You are able to control whcih target version you want to use.

The user of your app isn’t required to download and install. NET.

With the above-discussed approaches, you should be all set to run your application without any issues.

hostfxr.dll location

Missing hostfxr.dll can be found in the publish target folder along with the other binaries.

hostfxr

References:

  • Create Self Contained Single Executable (EXE) in .NET Core
  • Self Contained Vs Framework Dependent Deployment -Guidelines

Did I miss anything else in these resolution steps?

Did the above steps resolve your issue? Please sound off your comments below!

Happy Coding !!


Please bookmark this page and share it with your friends. Please Subscribe to the blog to get a notification on freshly published best practices and guidelines for software design and development.


Понравилась статья? Поделить с друзьями:
  • A fatal error occurred however mscoree dll could not be loaded to display the appropriate
  • A fatal error occurred failed to connect to espressif device no serial data received
  • A fatal error occurred failed to connect to esp8266 timed out waiting for packet header
  • A fatal error occurred failed to connect to esp32 no serial data received
  • A fatal error occurred failed to connect to esp32 invalid head of packet 0x65