安装 .NET Core SDKInstall the .NET Core SDK

本文介绍如何安装 .NET Core SDK。In this article, you'll learn how to install the .NET Core SDK. .NET Core SDK 用于创建 .NET Core 应用和库。The .NET Core SDK is used to create .NET Core apps and libraries. .NET Core 运行时始终随 SDK 一起安装。The .NET Core runtime is always installed with the SDK.

使用安装程序安装Install with an installer

Windows 具有独立的安装程序,可用于安装 .NET Core 3.1 SDK:Windows has standalone installers that can be used to install the .NET Core 3.1 SDK:

使用安装程序安装Install with an installer

macOS 具有独立的安装程序,可用于安装 .NET Core 3.1 SDK:macOS has standalone installers that can be used to install the .NET Core 3.1 SDK:

使用包管理器安装Install with a package manager

可使用许多常见的 Linux 包管理器安装 .NET Core SDK。You can install the .NET Core SDK with many of the common Linux package managers. 有关详细信息,请参阅 Linux 包管理器 - 安装 .NET CoreFor more information, see Linux Package Manager - Install .NET Core.

仅在 x64 体系结构上支持使用包管理器安装。Installing with a package manager is only supported on the x64 architecture. 如果要使用其他体系结构(如 ARM)安装 .NET Core SDK,请遵循下面的下载并手动安装说明。If you're installing the .NET Core SDK with a different architecture, such as ARM, follow the Download and manually install instructions below. 有关支持的体系结构的详细信息,请参阅 .NET Core 依赖项和要求For more information about what architectures are supported, see .NET Core dependencies and requirements.

下载并手动安装Download and manually install

若要提取 SDK 并使 .NET Core CLI 命令可用于终端,请先下载 .NET Core 二进制版本。To extract the SDK and make the .NET Core CLI commands available at the terminal, first download a .NET Core binary release. 然后,打开终端并运行以下命令。Then, open a terminal and run the following commands.

mkdir -p $HOME/dotnet && tar zxf dotnet-sdk-3.1.100-linux-x64.tar.gz -C $HOME/dotnet
export DOTNET_ROOT=$HOME/dotnet
export PATH=$PATH:$HOME/dotnet

提示

前面的 export 命令只会使 .NET Core CLI 命令对运行它的终端会话可用。The preceding export commands only make the .NET Core CLI commands available for the terminal session in which it was run.

你可以编辑 shell 配置文件,永久地添加这些命令。You can edit your shell profile to permanently add the commands. Linux 提供了许多不同的 shell,每个都有不同的配置文件。There are a number of different shells available for Linux and each has a different profile. 例如:For example:

  • Bash Shell:~/.bash_profile、~/.bashrcBash Shell: ~/.bash_profile, ~/.bashrc
  • Korn Shell:~/.kshrc 或 .profileKorn Shell: ~/.kshrc or .profile
  • Z Shell:~/.zshrc 或 .zprofileZ Shell: ~/.zshrc or .zprofile

为 shell 编辑相应的源文件,并将 :$HOME/dotnet 添加到现有 PATH 语句的末尾。Edit the appropriate source file for your shell and add :$HOME/dotnet to the end of the existing PATH statement. 如果不包含 PATH 语句,则使用 export PATH=$PATH:$HOME/dotnet 添加新行。If no PATH statement is included, add a new line with export PATH=$PATH:$HOME/dotnet.

另外,将 export DOTNET_ROOT=$HOME/dotnet 添加至文件的末尾。Also, add export DOTNET_ROOT=$HOME/dotnet to the end of the file.

使用 Visual Studio 安装Install with Visual Studio

如果你要使用 Visual Studio 开发 .NET Core 应用,请参阅下表,了解不同目标 .NET Core SDK 版本所需的 Visual Studio 最低版本。If you're using Visual Studio to develop .NET Core apps, the following table describes the minimum required version of Visual Studio based on the target .NET Core SDK version.

.NET Core SDK 版本.NET Core SDK version Visual Studio 版本Visual Studio version
3.13.1 Visual Studio 2019 版本 16.4 或更高版本。Visual Studio 2019 version 16.4 or higher.
3.03.0 Visual Studio 2019 版本 16.3 或更高版本。Visual Studio 2019 version 16.3 or higher.
2.22.2 Visual Studio 2017 版本 15.9 或更高版本。Visual Studio 2017 version 15.9 or higher.
2.12.1 Visual Studio 2017 版本 15.7 或更高版本。Visual Studio 2017 version 15.7 or higher.

如果你已安装 Visual Studio,则可以使用以下步骤检查你的版本。If you already have Visual Studio installed, you can check your version with the following steps.

  1. 打开 Visual Studio。Open Visual Studio.
  2. 选择“帮助” > “Microsoft Visual Studio”。Select Help > About Microsoft Visual Studio.
  3. 从“关于”对话框中读取版本号。Read the version number from the About dialog.

Visual Studio 可安装最新的 .NET Core SDK 和运行时。Visual Studio can install the latest .NET Core SDK and runtime.

选择工作负载Select a workload

安装或修改 Visual Studio 时,根据要生成的应用程序的类型,选择以下一个或多个工作负载:When installing or modifying Visual Studio, select one or more of the following workloads, depending on the kind of application you're building:

  • “其他工具集”部分中的“.NET Core 跨平台开发”工作负荷。The .NET Core cross-platform development workload in the Other Toolsets section.
  • “Web 和云”部分中的“ASP.NET 和 Web 开发”工作负荷。The ASP.NET and web development workload in the Web & Cloud section.
  • “Web 和云”部分中的“Azure 开发”工作负载。The Azure development workload in the Web & Cloud section.
  • “桌面和移动”部分中的“NET 桌面开发”工作负载。The .NET desktop development workload in the Desktop & Mobile section.

具有 .NET Core 工作负载的 Windows Visual Studio 2019Windows Visual Studio 2019 with .NET Core workload

使用 Visual Studio for Mac 安装Install with Visual Studio for Mac

在选定“.NET Core”工作负载时,使用 Visual Studio for Mac 安装 .NET Core SDK。Visual Studio for Mac installs the .NET Core SDK when the .NET Core workload is selected. 若要开始在 macOS 上进行 .NET Core 开发,请参阅安装 Visual Studio 2019 for MacTo get started with .NET Core development on macOS, see Install Visual Studio 2019 for Mac. 对于最新的版本 .NET Core 3.1,则必须使用 Visual Studio for Mac 8.4 预览版。For the latest release, .NET Core 3.1, you must use the Visual Studio for Mac 8.4 Preview.

具有 .NET Core 工作负载功能的 macOS Visual Studio 2019 for MacmacOS Visual Studio 2019 for Mac with .NET Core workload feature

随 Visual Studio Code 一起安装Install alongside Visual Studio Code

Visual Studio Code 是一个功能强大的轻量级源代码编辑器,可在桌面上运行。Visual Studio Code is a powerful and lightweight source code editor that runs on your desktop. Visual Studio Code 适用于 Windows、macOS 和 Linux。Visual Studio Code is available for Windows, macOS, and Linux.

虽然 Visual Studio Code 不像 Visual Studio 一样附带自动的 .NET Core 安装程序,但添加 .NET Core 支持非常简单。While Visual Studio Code doesn't come with an automated .NET Core installer like Visual Studio does, adding .NET Core support is simple.

  1. 下载并安装 Visual Studio CodeDownload and install Visual Studio Code.
  2. 下载并安装 .NET Core SDKDownload and install the .NET Core SDK.
  3. 从 Visual Studio Code 市场安装 C# 扩展Install the C# extension from the Visual Studio Code marketplace.

使用 PowerShell 自动化安装Install with PowerShell automation

dotnet-install 脚本用于 SDK 的自动化和非管理员安装。The dotnet-install scripts are used for automation and non-admin installs of the SDK. 可从 dotnet-install 脚本引用页下载该脚本。You can download the script from the dotnet-install script reference page.

此脚本默认安装最新的长期支持 (LTS) 版本,即 .NET Core 3.1。The script defaults to installing the latest long term support (LTS) version, which is .NET Core 3.1. 若要安装最新版本的 .NET Core,请使用以下开关运行脚本。To install the current release of .NET Core, run the script with the following switch.

dotnet-install.ps1 -Channel Current

使用 Bash 自动化安装Install with bash automation

dotnet-install 脚本用于 SDK 的自动化和非管理员安装。The dotnet-install scripts are used for automation and non-admin installs of the SDK. 可从 dotnet-install 脚本引用页下载该脚本。You can download the script from the dotnet-install script reference page.

此脚本默认安装最新的长期支持 (LTS) 版本,即 .NET Core 3.1。The script defaults to installing the latest long term support (LTS) version, which is .NET Core 3.1. 若要安装最新版本的 .NET Core,请使用以下开关运行脚本。To install the current release of .NET Core, run the script with the following switch.

./dotnet-install.sh -c Current

所有 .NET Core 下载项All .NET Core downloads

可直接通过以下链接之一下载和安装 .NET Core:You can download and install .NET Core directly with one of the following links:

DockerDocker

容器提供了一种将应用程序与主机系统的其余部分隔离的轻量级方法。Containers provide a lightweight way to isolate your application from the rest of the host system. 同一计算机上的容器只共享内核,并使用为应用程序提供的资源。Containers on the same machine share just the kernel and use resources given to your application.

.NET Core 可在 Docker 容器中运行。.NET Core can run in a Docker container. 官方 .NET Core Docker 映像发布到 Microsoft 容器注册表 (MCR),用户可以在 Microsoft.NET Core Docker 中心存储库中找到这些映像。Official .NET Core Docker images are published to the Microsoft Container Registry (MCR) and are discoverable at the Microsoft .NET Core Docker Hub repository. 每个存储库包含 .NET(SDK 或运行时)和可以使用的操作系统的不同组合的映像。Each repository contains images for different combinations of the .NET (SDK or Runtime) and OS that you can use.

Microsoft 提供适合特定场景的映像。Microsoft provides images that are tailored for specific scenarios. 例如,ASP.NET Core 存储库提供针对在生产环境中运行 ASP.NET Core 应用生成的映像。For example, the ASP.NET Core repository provides images that are built for running ASP.NET Core apps in production.

有关在 Docker 容器中使用 .NET Core 的详细信息,请参阅 .NET 和 Docker 简介示例For more information about using .NET Core in a Docker container, see Introduction to .NET and Docker and Samples.

后续步骤Next steps