權威的資料是微軟官方的文檔:在Ubuntu上安裝.NET SDK或.NET運行時。
1、准備工作
(1) 添加Microsoft包簽名密鑰
將Microsoft包簽名密鑰添加到受信任密鑰列表,並添加包存儲庫。
> wget https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb > sudo dpkg -i packages-microsoft-prod.deb
(2) unable to resolve host address錯誤
執行上面命令,會出現unable to resolve host address錯誤。通過Xshell進入Ubuntu系統,使用root權限修改resolv.conf,添加Google DNS配置。
> su > password > cd /etc > vi resolv.conf
在編輯頁面添加下面配置:
nameserver 8.8.8.8 #google域名服務器 nameserver 8.8.4.4 #google域名服務器
然后退出編輯並保存:
> Esc
> :x
2、在Ubuntu中安裝.NetCore環境
(1) 安裝.netcore SDK
> sudo apt-get update; \ sudo apt-get install -y apt-transport-https && \ sudo apt-get update && \ sudo apt-get install -y dotnet-sdk-3.1
(2) 出現錯誤
執行上面命令會出現如下錯誤:
Couldn't create temporary file /tmp/apt.conf.sqqT47 for passing config to apt-key
這是因為找不到文件夾/tmp,解決方法是使用root創建/tmp文件夾,並對其賦權:
> cd / > mkdir tmp > chmod 777 /tmp
(3) 安裝.netcore運行時
> sudo apt-get update; \ sudo apt-get install -y apt-transport-https && \ sudo apt-get update && \ sudo apt-get install -y aspnetcore-runtime-3.1
3、運行.netcore app
使用下面命令運行.netcore app:
dotnet TestConsole.dll
運行.netcore app時可能會出現“Failed to create CoreCLR, HRESULT: 0x80004005”錯誤,重新創建並賦權/tmp文件夾即可。