LinuxCommandLibrary

dotnet-build

Build .NET projects and dependencies

TLDR

Compile the project or solution in the current directory

$ dotnet build
copy

Compile a .NET project or solution in debug mode
$ dotnet build [path/to/project_or_solution]
copy

Compile in release mode
$ dotnet build [[-c|--configuration]] [Release]
copy

Compile without restoring dependencies
$ dotnet build --no-restore
copy

Compile with a specific verbosity level
$ dotnet build [[-v|--verbosity]] [quiet|minimal|normal|detailed|diagnostic]
copy

Compile for a specific runtime
$ dotnet build [[-r|--runtime]] [runtime_identifier]
copy

Specify the output directory
$ dotnet build [[-o|--output]] [path/to/directory]
copy

SYNOPSIS

dotnet build [<PROJECT | SOLUTION>] [options]

<PROJECT | SOLUTION>: Optional. The path to a project file, a solution file, or a directory containing either. If omitted, the command searches the current directory for a project or solution file.

PARAMETERS

-c | --configuration <CONFIGURATION>
    Specifies the build configuration, such as Debug (default) or Release.

-f | --framework <FRAMEWORK>
    Compiles for a specific target framework defined in the project file, e.g., net8.0 or net7.0.

-o | --output <OUTPUT_DIRECTORY>
    Directory where the build artifacts are placed. Defaults to bin/Debug or bin/Release within the project directory.

--no-restore
    Does not perform an implicit NuGet restore during build. This option is useful when dependencies are already restored.

--no-dependencies
    Does not build project-to-project references. Only builds the specified project.

-r | --runtime <RUNTIME_IDENTIFIER>
    Builds a self-contained application for a specific runtime (RID), e.g., linux-x64, win-x86.

-p | --property <PROPERTY=VALUE>
    Sets one or more MSBuild properties. For example, -p:Version=1.0.0.

-v | --verbosity <LEVEL>
    Sets the MSBuild verbosity level. Options include q (quiet), m (minimal), n (normal), d (detailed), and diag (diagnostic).

DESCRIPTION

The dotnet build command is a fundamental tool within the .NET Command-Line Interface (CLI), used to compile C#, F#, or VB.NET projects and solutions. It's the primary way to transform your source code into executable files (.exe) or libraries (.dll). When invoked, dotnet build implicitly performs a dependency restore (similar to dotnet restore) unless explicitly told not to, ensuring all necessary NuGet packages are available. It then orchestrates the compilation process, resolving references, compiling code, and packaging the output into the project's designated build output directory (typically bin/Debug or bin/Release). This command is crucial for validating code, preparing applications for testing, or as a step before publishing for deployment. It supports building for various target frameworks and runtimes, making it highly versatile for cross-platform development.

CAVEATS

  • Requires the .NET SDK to be installed on the Linux system.
  • Build success is contingent upon valid project files (.csproj, .fsproj, .vbproj) and accessibility of all declared dependencies.
  • While executed on Linux, dotnet build can compile code targeting other operating systems or architectures when combined with the --runtime option.

IMPLICIT RESTORE

Before compiling, dotnet build automatically executes a dotnet restore operation to download and install all project dependencies. This behavior can be bypassed using the --no-restore option if dependencies are already guaranteed to be in place.

BUILD ARTIFACTS LOCATION

Compiled outputs (assemblies, executables, debug symbols) are typically placed in a subdirectory of the project's root, usually under bin/<Configuration>/<TargetFramework>/, for example, bin/Debug/net8.0/. This organized structure helps manage different build outputs.

HISTORY

The dotnet build command emerged as a cornerstone of the .NET Core CLI, introduced around 2016. Its development was central to Microsoft's strategy to make .NET cross-platform and open-source, moving away from Windows-exclusive development environments like Visual Studio. It provided a unified, command-line driven interface for building .NET applications on Linux, macOS, and Windows, abstracting away the complexities of underlying build systems like MSBuild while still leveraging them. With each subsequent release of .NET (e.g., .NET 5, 6, 7, 8), dotnet build has continued to evolve, supporting new language features, target frameworks, and build optimizations, solidifying its role as the standard build command for modern .NET development.

SEE ALSO

dotnet(1), dotnet restore(1), dotnet run(1), dotnet publish(1), dotnet test(1)

Copied to clipboard