我正在尝试安装 .NET SDK 以与 VS Code 一起使用。
当我输入dotnet new console
以下内容时,返回:
The command could not be loaded, possibly because:
* You intended to execute a .NET application:
The application 'new' does not exist.
* You intended to execute a .NET SDK command:
No .NET SDKs were found.
Download a .NET SDK:
https://aka.ms/dotnet/download
Learn about SDK resolution:
https://aka.ms/dotnet/sdk-not-found
打字dotnet --version
也会返回上述错误。
另一方面dotnet --version
没有列出任何内容。
我按照各种网站尝试安装 sdk如何在 Ubuntu 22.04 上安装 .NET Core(dotnet)和在 Ubuntu 中使用 VSCode 设置 C#(Dotnet)没有成功。
更新:我关注了在 Ubuntu 23.04 上安装 .NET SDK 或 .NET 运行时。下面是此站点上的命令的输出:
hadmin@hp-linux:~$ dotnet --list-sdks
hadmin@hp-linux:~$ dotnet --list-runtimes
Microsoft.AspNetCore.App 6.0.16 [/usr/lib/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 7.0.5 [/usr/lib/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.NETCore.App 6.0.16 [/usr/lib/dotnet/shared/Microsoft.NETCore.App]
Microsoft.NETCore.App 7.0.5 [/usr/lib/dotnet/shared/Microsoft.NETCore.App]
hadmin@hp-linux:~$ wget https://dot.net/v1/dotnet-install.sh -O dotnet-install.sh
--2023-05-25 17:55:54-- https://dot.net/v1/dotnet-install.sh
Resolving dot.net (dot.net)... 20.81.111.85, 20.53.203.50, 20.112.52.29, ...
Connecting to dot.net (dot.net)|20.81.111.85|:443... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: https://dotnet.microsoft.com/download/dotnet/scripts/v1/dotnet-install.sh [following]
--2023-05-25 17:55:54-- https://dotnet.microsoft.com/download/dotnet/scripts/v1/dotnet-install.sh
Resolving dotnet.microsoft.com (dotnet.microsoft.com)... 13.107.238.64, 13.107.237.64, 2620:1ec:4f:1::64, ...
Connecting to dotnet.microsoft.com (dotnet.microsoft.com)|13.107.238.64|:443... connected.
HTTP request sent, awaiting response... 200 OK
Cookie coming from dotnet.microsoft.com attempted to set domain to dotnetwebsite.azurewebsites.net
Cookie coming from dotnet.microsoft.com attempted to set domain to dotnetwebsite.azurewebsites.net
Length: 58824 (57K) [application/x-sh]
Saving to: ‘dotnet-install.sh’
dotnet-install.sh 100%[=========================>] 57.45K --.-KB/s in 0.01s
2023-05-25 17:55:54 (5.56 MB/s) - ‘dotnet-install.sh’ saved [58824/58824]
hadmin@hp-linux:~$ sudo chmod +x ./dotnet-install.sh
hadmin@hp-linux:~$ ./dotnet-install.sh --version latest
dotnet-install: .NET Core SDK with version '6.0.408' is already installed.
hadmin@hp-linux:~$ ./dotnet-install.sh --version latest --runtime aspnetcore
dotnet-install: ASP.NET Core Runtime with version '6.0.16' is already installed.
hadmin@hp-linux:~$ ./dotnet-install.sh --channel 7.0
dotnet-install: Attempting to download using aka.ms link https://dotnetcli.azureedge.net/dotnet/Sdk/7.0.302/dotnet-sdk-7.0.302-linux-x64.tar.gz
dotnet-install: Extracting zip from https://dotnetcli.azureedge.net/dotnet/Sdk/7.0.302/dotnet-sdk-7.0.302-linux-x64.tar.gz
dotnet-install: Installed version is 7.0.302
dotnet-install: Adding to current process PATH: `/home/hadmin/.dotnet`. Note: This change will be visible only when sourcing script.
dotnet-install: Note that the script does not resolve dependencies during installation.
dotnet-install: To check the list of dependencies, go to https://learn.microsoft.com/dotnet/core/install, select your operating system and check the "Dependencies" section.
dotnet-install: Installation finished successfully.
hadmin@hp-linux:~$ dotnet --list-sdks
hadmin@hp-linux:~$ dotnet --list-runtimes
Microsoft.AspNetCore.App 6.0.16 [/usr/lib/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 7.0.5 [/usr/lib/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.NETCore.App 6.0.16 [/usr/lib/dotnet/shared/Microsoft.NETCore.App]
Microsoft.NETCore.App 7.0.5 [/usr/lib/dotnet/shared/Microsoft.NETCore.App]
hadmin@hp-linux:~$
根据@Khalid 的评论进行更新
hadmin@hp-linux:~$ env | grep DOTNET_ROOT
DOTNET_ROOT=/usr/lib/dotnet
hadmin@hp-linux:~$ ls /usr/lib/dotnet/
dotnet host LICENSE.txt packs shared ThirdPartyNotices.txt
hadmin@hp-linux:~$ ls $HOME/.dotnet/
corefx host packs sdk-manifests templates
dotnet LICENSE.txt sdk shared ThirdPartyNotices.txt
环境变量指向错误的目录。
我想清理安装(包括多个安装)并设置环境变量以指向正确的文件夹。
请问我该怎么做呢(哪个文件保存.NET 环境变量)?
答案1
该问题的解决方案解释如下:https://github.com/dotnet/sdk/issues/32804简单总结一下,Canonical 最近发布了官方的 dotnet 包,删除 MS source.list(或注释掉deb [arch=amd64,arm64,armhf] https://packages.microsoft.com/ubuntu/23.04/prod lunar main
)并安装dotnet*
即可解决问题:
$ sudo apt-get purge dotnet-sdk-7.0
$ sudo rm -f /etc/apt/sources.list.d/microsoft-prod.list && sudo apt update
$ sudo apt install -y dotnet7
$ dotnet --info
.NET SDKs installed:
7.0.110 [/usr/lib/dotnet/sdk]
如果您仍然想使用dotnet-sdk-7.0
来自 的来源packages.microsoft.com
,则需要提高包的优先级(我自己没有尝试过这个解决方案,但它很有意义):
echo -e "Package: *\nPin: origin packages.microsoft.com\nPin-Priority: 1001" > /etc/apt/preferences.d/dotnet
答案2
您没有安装 SDK。运行时没有得到任何结果dotnet --list-sdks
,这暗示您只安装了运行时。
install.sh
我建议使用以下文件安装 SDKhttps://dot.net然后确保安装目录位于您的路径中。您似乎正在这样做。
您可能需要重启终端并重试。有时.bashrc
不会重新加载。
答案3
install.sh
我只能通过使用脚本来解决这个问题https://dot.net,它将 dotnet 安装为用户范围。
默认的 Canonical 不是最新版本,并且 Microsoft 没有安装 SDK(至少没有注册 SDK 路径)