Install the .NET SDK
Install the SDK
The SDK is the package you want: it holds the compiler, the
project system, the dotnet command and the runtime.
The runtime alone can start a program that was built elsewhere,
and it cannot build one. A machine that only runs your program needs the
runtime; a machine that compiles it needs the SDK.
The download page is
dotnet.microsoft.com/download,
and it lists the two packages for every system. On a distribution with the
Microsoft feed the package name carries the version:
sudo apt install -y dotnet-sdk-10.0
On Alpine the SDK is a distribution package, and on Windows the installer is an .exe file. The commands of this lesson are the same on all three systems.
Check what is installed
dotnet --version prints the version of the SDK that would build a project here. It is the number a bug report wants.
dotnet --version
10.0.303
A machine can hold several SDKs and several runtimes at once. --list-sdks prints each SDK and the folder it is installed in:
dotnet --list-sdks
10.0.303 [/usr/lib/dotnet/sdk]
--list-runtimes prints the runtimes, which are what a built program needs. This container has two: the console runtime and the ASP.NET Core one.
dotnet --list-runtimes
Microsoft.AspNetCore.App 10.0.15 [/usr/lib/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.NETCore.App 10.0.15 [/usr/lib/dotnet/shared/Microsoft.NETCore.App]
The SDK version and the runtime version are different numbers on purpose: the SDK is a build tool with its own releases, and the runtime is the thing the program needs at run time. A program built with the 10.0.303 SDK runs on the 10.0.15 runtime, and the note at the end of each lesson names the SDK the page was built with.
dotnet --info
dotnet --info puts all of it in one report, with the operating system, the RID of the machine and the environment variables that change the behaviour of the CLI. The first lines of the report of this container:
dotnet --info
.NET SDK:
Version: 10.0.303
Commit: e730f1db75
Workload version: 10.0.300-manifests.330ea142
MSBuild version: 18.6.14+e730f1db7
Runtime Environment:
OS Name: alpine
OS Version: 3.24
OS Platform: Linux
RID: alpine.3.24-x64
Base Path: /usr/lib/dotnet/sdk/10.0.303/
The RID is the runtime identifier: the operating system, its version and the architecture in one word. It is what a publish command names when it builds for another system, and the lesson on dotnet publish uses it.
The versions from inside a program
A program can read the same facts at run time, and that is the check worth keeping: it tells you what the running process sees, not what the shell found.
using System.Runtime.InteropServices;
Console.WriteLine(RuntimeInformation.OSDescription);
Console.WriteLine(RuntimeInformation.OSArchitecture);
Console.WriteLine(RuntimeInformation.RuntimeIdentifier);
Console.WriteLine(RuntimeInformation.FrameworkDescription);
dotnet run
Alpine Linux v3.24
X64
alpine.3.24-x64
.NET 10.0.15
RuntimeIdentifier is the same RID that dotnet --info reported, and FrameworkDescription is the runtime rather than the SDK. Neither line would be possible in a language that is compiled to a native binary with no runtime: there the program cannot ask which runtime started it, because none did.
Exercises
- Run dotnet --list-sdks and dotnet --list-runtimes on your machine and compare the two numbers with the ones above.
- Run dotnet --info and find the RID. Which part of it changes on a Windows machine, and which part on an ARM one?
- Add Environment.MachineName and Environment.ProcessorCount to the program. Which of the two depends on the container rather than on the host?
- Install the runtime only, on a machine without the SDK, and try dotnet build. Write down the message.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does the .NET SDK hold that the runtime does not?
Answer
The compiler, the project system and the dotnet command
The runtime runs a built program; the SDK adds the compiler, the project system and the CLI that builds it, and it carries a runtime too.
-
Which command prints the version of the SDK?
Answer
dotnet --version
dotnet --version prints the SDK version; --list-runtimes prints the runtimes, and the other two are not commands.
-
What does this program print?
using System.Runtime.InteropServices; Console.WriteLine(RuntimeInformation.OSArchitecture);Answer
X64
OSArchitecture is the architecture of the process, which is X64 here; the RID and the OS name are other properties.
-
What does this program print?
Console.WriteLine(Environment.ProcessorCount > 0); Console.WriteLine(Environment.Version.Major);Answer
True / 10
The process runs on at least one CPU, and the major version of the 10.0.15 runtime is 10; 15 is the minor part.
-
What is a RID such as
alpine.3.24-x64?Answer
A runtime identifier: the system, its version and the architecture
A runtime identifier names the platform a build is for, which a publish command uses to pick the runtime pack.
-
A machine has the runtime but not the SDK. Which command fails?
Answer
dotnet build
dotnet build is part of the SDK, so a runtime-only install cannot build; the three others are the host commands and work.
-
What does this program print on a 64-bit machine?
Console.WriteLine(sizeof(long) * 8); Console.WriteLine(sizeof(int) * 8);Answer
64 / 32
sizeof of the built-in numeric types is allowed outside unsafe code, and long is 8 bytes while int is 4.
-
Which two commands report the version of a runtime rather than of the SDK?
Answer
dotnet --list-runtimes and RuntimeInformation.FrameworkDescription in a program
The first reports the installed runtimes from outside, the second from inside a running process; the SDK version is a different number.
-
What does this program print?
Console.WriteLine($"sdk: {Environment.Version}");Answer
sdk: 10.0.15
Environment.Version is the runtime version, 10.0.15 here; 10.0.303 is the SDK, which a running program does not know.
-
Which line of dotnet --info tells you which runtime pack a publish for this machine would use?
Answer
RID: alpine.3.24-x64
The RID names the platform, and a publish with that RID takes the runtime pack of that platform.
The code on this page was built with the .NET SDK 10.0.303 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev