LinuxCommandLibrary

dotnet-publish

Package .NET application for deployment

TLDR

Compile a .NET project in release mode

$ dotnet publish [[-c|--configuration]] Release [path/to/project_file]
copy

Publish the .NET Core runtime with your application for the specified runtime
$ dotnet publish [[-sc|--self-contained]] true [[-r|--runtime]] [runtime_identifier] [path/to/project_file]
copy

Package the application into a platform-specific single-file executable
$ dotnet publish [[-r|--runtime]] [runtime_identifier] -p:PublishSingleFile=true [path/to/project_file]
copy

Trim unused libraries to reduce the deployment size of an application
$ dotnet publish [[-sc|--self-contained]] true [[-r|--runtime]] [runtime_identifier] -p:PublishTrimmed=true [path/to/project_file]
copy

Compile a .NET project without restoring dependencies
$ dotnet publish --no-restore [path/to/project_file]
copy

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

SYNOPSIS

dotnet publish [project] [options]

PARAMETERS

--configuration, -c
    Configuration: Debug or Release (default: Debug)

--framework, -f
    Target framework moniker (TFM), e.g., net8.0

--output, -o
    Output directory for published files

--runtime, -r
    Target runtime ID, e.g., linux-x64, ubuntu.22.04-x64

--self-contained [true|false]
    Self-contained deployment (includes runtime); default false

--no-self-contained
    Framework-dependent deployment (excludes runtime)

--no-build
    Skip building; use existing outputs

--no-restore
    Skip NuGet restore

--no-dependencies
    Exclude dependency projects

--force
    Force rebuild even if no changes

/p:=
    MSBuild property, e.g., /p:PublishSingleFile=true

--verbosity
    Output verbosity: quiet|minimal|normal|detailed|diagnostic

DESCRIPTION

The dotnet publish command publishes a .NET application or library for deployment to a specific runtime environment. It compiles the project, resolves dependencies, and packages all necessary files into an output directory. This includes executables, libraries, and assets optimized for production use.

Key features include support for framework-dependent deployments (requiring .NET runtime on target) or self-contained deployments (including runtime). It handles multiple target frameworks, runtime identifiers (RIDs) like linux-x64, trimming for size reduction, and single-file publishing.

On Linux, specify RIDs such as linux-x64, linux-arm64 for native executables. Use with --self-contained for standalone apps or omit for smaller framework-dependent bundles. Integrates with MSBuild for custom properties via /p: options.

Ideal for CI/CD pipelines, containerization (Docker), and cloud deployment. Output is ready for scp, archiving, or direct execution.

CAVEATS

Self-contained deploys significantly increase output size (~100MB+). Incorrect RID causes runtime failures on Linux. Requires .NET SDK installed. Not for NuGet packages (use dotnet pack).

DEPLOYMENT MODES

Self-contained: dotnet publish -r linux-x64 --self-contained for standalone app.
Framework-dependent: dotnet publish -c Release requires target .NET runtime.

LINUX EXAMPLE

dotnet publish -c Release -r linux-x64 -o ./publish --self-contained
Produces executable in ./publish runnable via ./publish/MyApp.

HISTORY

Introduced in .NET Core 1.0 SDK (2016) as part of cross-platform shift from .NET Framework. Evolved in .NET 5+ with AOT compilation, single-file publish, and ready-to-run (R2R). Widely used in DevOps since .NET Core 2.0.

SEE ALSO

dotnet(1), dotnet-build(1), dotnet-pack(1)

Copied to clipboard