Skip to content

Beginner · Getting started with C# and .NET · topic 2 of 43 · 25 min

Installing .NET and using the dotnet CLI

Explanation

Install the .NET SDK from dotnet.microsoft.com, then use the dotnet command in a terminal. dotnet new console -o Hello makes a project, dotnet run builds and runs it, and dotnet build only builds it. The .csproj file describes the project: its name, its .NET version and the packages it uses.

Why a command line?

A CLI (Command-Line Interface) is a program you control by typing commands in a terminal instead of clicking buttons. The .NET CLI is one program named dotnet. It creates projects, builds them, runs them, runs tests and installs packages. Big editors like Visual Studio call the same dotnet tool behind the scenes, so learning it helps you everywhere: on your laptop, on a Linux server and in automated build pipelines.

Step 1: install the SDK

  1. Go to dotnet.microsoft.com and open the Download page.
  2. Download the SDK (not only the runtime) for the latest LTS version, for your operating system.
  3. Run the installer. On Linux you can also use your package manager; the download page shows the commands.
  4. Close and reopen your terminal so it finds the new dotnet command.
bash
dotnet --version
dotnet --list-sdks
Check the install. --version prints the SDK version in use, for example 10.0.100.

Step 2: pick an editor

  • Visual Studio Code with the C# Dev Kit extension: free, works on all systems, light on old laptops.
  • Visual Studio Community: a full IDE (Integrated Development Environment) for Windows, free for students and individuals.
  • JetBrains Rider: a full IDE for all systems, free for non-commercial use.

Step 3: create and run a project

bash
dotnet new console -o Hello
cd Hello
dotnet run
new console uses the console-app template; -o Hello puts it in a new folder named Hello. dotnet run builds the code and runs it.
output
Hello, World!
What the new program prints.

A template is a ready-made starting project. dotnet new list shows all installed templates: console, classlib (a library), webapi, xunit (tests) and more.

What is inside the folder?

File or folderWhat it is
Program.csYour C# code.
Hello.csprojThe project file: settings in XML.
obj/Temporary files the build uses. Created automatically.
bin/The build output: Hello.dll and friends, inside bin/Debug/net10.0/ (the last part matches your version).
xml
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net10.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

</Project>
A console .csproj. TargetFramework says which .NET version to build for. Nullable turns on null-safety warnings (module 3). ImplicitUsings adds common using lines for you.

The everyday commands

CommandWhat it does
dotnet new console -o NameCreate a new console project in a folder
dotnet buildCompile only; shows errors and warnings
dotnet runBuild if needed, then run
dotnet watchRun, and rebuild automatically when you save a file
dotnet cleanDelete the build output
dotnet add package NameAdd a NuGet package (module 9)
dotnet publish -c ReleaseMake a release build ready to copy to a server

Common mistakes and fixes

  • "dotnet is not recognized as a command": the terminal was open before the install. Close it and open a new one. If it still fails, restart the computer.
  • "Couldn't find a project to run": you are in the wrong folder. Use cd to go into the folder that has the .csproj file.
  • Committing bin/ and obj/ to Git: they are rebuilt every time and are large. Run dotnet new gitignore once in your project to create a .gitignore that skips them.
  • Installing only the runtime: you can run apps but not build them. Install the SDK.
Tip: Since .NET 10 you can also run a single file without a project: dotnet run hello.cs. It is handy for quick experiments, but real apps still use a .csproj.

Need help?

Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.

Try:

Watch the lesson

Installing .NET and using the dotnet CLI · 1/7

The dotnet CLI

In this lesson you will install the .NET SDK and use the dotnet command to create and run your first project.

Prefer a video by a teacher? Find free videos on “install .NET SDK dotnet new console dotnet run tutorial” on YouTube ↗

In real life: Setting up a college lab in one hour

A lab assistant at an engineering college in Nagpur sets up 40 computers for a C# class. She installs the .NET SDK and VS Code on each one, then checks every machine with dotnet --version. Students create their first app with dotnet new console, so nobody needs a paid licence.

bash
dotnet --version
dotnet new console -o MarksApp
cd MarksApp
dotnet run
The four commands every student types on day one.

Practice (0/3 done)

Task 1 · Question

Which command creates a new console project in a folder named Shop?

Task 2 · Question

You type dotnet run and see "Couldn't find a project to run". What is the most likely cause?

Task 3 · Practice

On your own computer: install the .NET SDK, create a console app named Hello, run it, then change the message to print your name and run it again.

Quick quiz

Question 1

1.What is the difference between dotnet build and dotnet run?

Question 2

2.Which file tells .NET the project's target version?

Question 3

3.Should you commit the bin and obj folders to Git?

Question 4

4.What do you need to install to BUILD C# programs?

Question 5

5.Which command reruns your app automatically when you save a file?

Read more (free)

Before this, learn

This topic builds on these. If something here feels new, open them first.

Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.

What is the difference between dotnet build and dotnet run?

Answer: build only compiles; run compiles if needed and then runs

dotnet build checks and compiles. dotnet run also starts the program.

Which file tells .NET the project's target version?

Answer: The .csproj file

The <TargetFramework> element in the .csproj, for example net10.0.

Should you commit the bin and obj folders to Git?

Answer: No, they are build output and are recreated by every build

They are generated files. Add them to .gitignore (dotnet new gitignore does it).

What do you need to install to BUILD C# programs?

Answer: The .NET SDK

The SDK has the compiler and the dotnet tools. The runtime alone can only run built apps.

Which command reruns your app automatically when you save a file?

Answer: dotnet watch

dotnet watch rebuilds and restarts when files change.

Where is a package dependency recorded?Related

Answer: As a PackageReference in the .csproj

The .csproj lists PackageReference items.

From NuGet packages, solutions and project structure

Which .NET versions are Long Term Support (LTS)?Related

Answer: Even-numbered ones like 8 and 10

Even-numbered releases are LTS with three years of support.

From What C# and .NET are

Since which version does a new ASP.NET Core app start from Program.cs without Startup.cs?Related

Answer: .NET 6

The minimal hosting model arrived in .NET 6.

From ASP.NET Core: your first Web API

What does dotnet test do?Related

Answer: Builds the projects and runs all the tests

It builds and runs tests, reporting passes and failures.

From Unit testing with xUnit

Pass the quiz (60% or more) or mark this topic as done when you are ready.

Your progress is saved on this device only.

Next steps

  1. 3 more topics in “Getting started with C# and .NET” before its exam: Your first program: output, input and the Main method, Types and variables, Operators, conversions and parsing input.
  2. Module 1 exam: Getting started with C# and .NET · 13 questions · 20 min, without help. 5 topics of this module still to finish first.
  3. Then the next module, Decisions, loops and methods. 43 topics left in the course before the final exam.

← Previous: What C# and .NET are