已安装.NET 8 SDK,新建Hello World控制台应用却无法运行
.NET 8控制台应用运行报错:提示未安装.NET,但环境已配置正常
环境与操作步骤
- 系统:Ubuntu 22.04.3 LTS
- 已安装
dotnet-sdk-8.0 - 创建并构建控制台应用的命令:
mkdir console dotnet new console dotnet build
问题现象
直接运行生成的二进制文件时,报错提示未安装.NET 8:
> ./bin/Debug/net8.0/console You must install .NET to run this application. App: /root/console/bin/Debug/net8.0/console Architecture: x64 App host version: 8.0.0 .NET location: Not found Learn more: https://aka.ms/dotnet/app-launch-failed Download the .NET runtime: https://aka.ms/dotnet-core-applaunch?missing_runtime=true&arch=x64&rid=linux-x64&os=ubuntu.22.04&apphost_version=8.0.0
但执行dotnet --info显示环境已正确配置:
> dotnet --info .NET SDK: Version: 8.0.100 Commit: 57efcf1350 Workload version: 8.0.100-manifests.6c33ef20 Runtime Environment: OS Name: ubuntu OS Version: 22.04 OS Platform: Linux RID: linux-x64 Base Path: /usr/share/dotnet/sdk/8.0.100/ .NET workloads installed: Workload version: 8.0.100-manifests.6c33ef20 There are no installed workloads to display. Host: Version: 8.0.0 Architecture: x64 Commit: 5535e31a71 .NET SDKs installed: 8.0.100 [/usr/share/dotnet/sdk] .NET runtimes installed: Microsoft.AspNetCore.App 8.0.0 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App] Microsoft.NETCore.App 8.0.0 [/usr/share/dotnet/shared/Microsoft.NETCore.App] Other architectures found: None Environment variables: Not set global.json file: Not found Learn more: https://aka.ms/dotnet/info Download .NET: https://aka.ms/dotnet/download
系统版本验证:
> cat /etc/os-release PRETTY_NAME="Ubuntu 22.04.3 LTS" NAME="Ubuntu" VERSION_ID="22.04" VERSION="22.04.3 LTS (Jammy Jellyfish)" VERSION_CODENAME=jammy ID=ubuntu ID_LIKE=debian HOME_URL="https://www.ubuntu.com/" SUPPORT_URL="https://help.ubuntu.com/" BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/" PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy" UBUNTU_CODENAME=jammy
解决方法
方法1:通过dotnet run运行应用
不要直接执行二进制文件,使用SDK自带命令运行:
cd console dotnet run
方法2:配置.NET环境变量
设置环境变量让应用能找到.NET运行时路径:
export DOTNET_ROOT=/usr/share/dotnet export PATH=$PATH:$DOTNET_ROOT:$DOTNET_ROOT/tools
若要永久生效,可将上述两行添加到~/.bashrc或~/.profile文件中,重启终端或执行source ~/.bashrc生效。
方法3:发布为自包含应用(独立运行)
如果需要直接运行二进制文件,可发布时打包运行时:
cd console dotnet publish -c Release -r linux-x64 --self-contained true
运行发布后的文件:
./bin/Release/net8.0/linux-x64/publish/console
内容的提问来源于stack exchange,提问作者Rytis I
相关产品推荐
相关产品推荐

