[C#][ubuntu]Ubuntu18.04安装.NET 5.0 SDK

Posted FL1623863129

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[C#][ubuntu]Ubuntu18.04安装.NET 5.0 SDK相关的知识,希望对你有一定的参考价值。

安装命令:

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

sudo apt-get install apt-transport-https
sudo apt-get update

sudo apt-get install dotnet-sdk-5.0

测试安装:

dotnet new console --output sample1

如果运行报错:可以在前面加sudo

运行结果:

Welcome to .NET 5.0!
---------------------
SDK Version: 5.0.100

Telemetry
---------
The .NET tools collect usage data in order to help us improve your experience. It is collected by Microsoft and shared with the community. You can opt-out of telemetry by setting the DOTNET_CLI_TELEMETRY_OPTOUT environment variable to '1' or 'true' using your favorite shell.

Read more about .NET CLI Tools telemetry: https://aka.ms/dotnet-cli-telemetry

----------------
Installed an ASP.NET Core HTTPS development certificate.
To trust the certificate run 'dotnet dev-certs https --trust' (Windows and macOS only).
Learn about HTTPS: https://aka.ms/dotnet-https
----------------
Write your first app: https://aka.ms/dotnet-hello-world
Find out what's new: https://aka.ms/dotnet-whats-new
Explore documentation: https://aka.ms/dotnet-docs
Report issues and find source on GitHub: https://github.com/dotnet/core
Use 'dotnet --help' to see available commands or visit: https://aka.ms/dotnet-cli
--------------------------------------------------------------------------------------
Getting ready...
The template "Console Application" was created successfully.

Processing post-creation actions...
Running 'dotnet restore' on sample1/sample1.csproj...
  Determining projects to restore...
  Restored /root/.net5/sample1/sample1.csproj (in 70 ms).
Restore succeeded.

查看项目结构
使用 tree sample1 查看目录结构,内容如下所示:

root@server00:~/.net5# tree sample1/
sample1/
├── obj
│   ├── project.assets.json
│   ├── project.nuget.cache
│   ├── sample1.csproj.nuget.dgspec.json
│   ├── sample1.csproj.nuget.g.props
│   └── sample1.csproj.nuget.g.targets
├── Program.cs
└── sample1.csproj

1 directory, 7 files

查看一下主程序 Progrom.cs 的内容如下所示:

root@server00:~/.net5# cat sample1/Program.cs
using System;

namespace sample1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}
编译并运行
现在使用命令 dotnet run --project sample1 进行编译并运行,可以看到以下内容:

root@server00:~/.net5# dotnet run --project sample1
Hello World!

运行成功。

然后我们再看一下项目的结构,可以看到出很多新的内容:

root@server00:~/.net5# tree sample1/
sample1/
├── bin
│   └── Debug
│       └── net5.0
│           ├── ref
│           │   └── sample1.dll
│           ├── sample1
│           ├── sample1.deps.json
│           ├── sample1.dll
│           ├── sample1.pdb
│           ├── sample1.runtimeconfig.dev.json
│           └── sample1.runtimeconfig.json
├── obj
│   ├── Debug
│   │   └── net5.0
│   │       ├── apphost
│   │       ├── ref
│   │       │   └── sample1.dll
│   │       ├── sample1.AssemblyInfo.cs
│   │       ├── sample1.AssemblyInfoInputs.cache
│   │       ├── sample1.assets.cache
│   │       ├── sample1.csprojAssemblyReference.cache
│   │       ├── sample1.csproj.CoreCompileInputs.cache
│   │       ├── sample1.csproj.FileListAbsolute.txt
│   │       ├── sample1.dll
│   │       ├── sample1.GeneratedMSBuildEditorConfig.editorconfig
│   │       ├── sample1.genruntimeconfig.cache
│   │       └── sample1.pdb
│   ├── project.assets.json
│   ├── project.nuget.cache
│   ├── sample1.csproj.nuget.dgspec.json
│   ├── sample1.csproj.nuget.g.props
│   └── sample1.csproj.nuget.g.targets
├── Program.cs
└── sample1.csproj

8 directories, 26 files
关于运行
其中,目录 /sample1/bin/Debug/net5.0/sample1 就是可执行程序,可以直接运行。比如:

root@server00:~/.net5# ./sample1/bin/Debug/net5.0/sample1
Hello World!

不过他还依赖于 /sample1/bin/Debug/net5.0/ref/sample1.dll 才可以运行。需要将其放置在指定的路径中,然后配置环境变量即可。
经测试,发现程序一共需要以下三个程序(放在同一目录中)才可运行:sample1、sample1.dll 和 sample1.runtimeconfig.json。其中最后一个为配置文件,内容如下:

root@server00:~/test1# cat sample1.runtimeconfig.json
{
  "runtimeOptions": {
    "tfm": "net5.0",
    "framework": {
      "name": "Microsoft.NETCore.App",
      "version": "5.0.0"
    }
  }
}

由此可见,此文件主要用于指定程序的类型和运行环境。

小结
通过本示例,可以看到安装和运行.NET5.0的程序还是很方便的,只需要几条命令即可。测试过程中也很顺利,几条命令运行完成,都得到了预期的效果。这样我们就可以使用.NET5.0来建立程序,可以看到
原文链接:https://blog.csdn.net/weixin_43145361/article/details/110526948

以上是关于[C#][ubuntu]Ubuntu18.04安装.NET 5.0 SDK的主要内容,如果未能解决你的问题,请参考以下文章

[C#][ubuntu][转载]monodevelop安装

Ubuntu18.04安装CUDA

Ubuntu 18.04 系统U盘安装与初步设置

ubuntu18.04安装教程

Ubuntu 18.04下OpenFOAM 2.3.1安装及配置

ubuntu18.04最小化安装为啥带桌面