F# projects
last modified December 4, 2026
In this article we create and organize F# projects with the
dotnet command line tool.
Most examples in this series are scripts that run with
dotnet fsi. Applications and libraries are built as
projects instead. A project is a directory with a project file
(.fsproj) and source files (.fs). The project
file lists the source files in the order in which they are compiled, the
target framework, the packages the code depends on, and other settings.
The first project was created in
Introduction to F#; here we look at projects
in more detail.
Creating a project
The dotnet new command creates a project from a template.
The console template creates an application,
-lang "F#" selects F# (the default language is C#), and
-o gives the output directory, which is also the name of the
project.
λ dotnet new console -lang "F#" -o tally The template "Console App" was created successfully. λ ls tally obj Program.fs tally.fsproj
We show only the first line of the dotnet new output; the
rest is the log of the package restore. The project contains a source
file, a project file, and the obj directory with restore
data. The generated Program.fs has a comment and one line of
code.
// For more information see https://aka.ms/fsharp-console-apps printfn "Hello from F#"
dotnet run builds the project in the current directory and
runs it. The compiled files go to the bin directory.
λ dotnet run Hello from F#
The project file
The project file is an MSBuild XML file. The template creates the smallest file that works.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net11.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="Program.fs" />
</ItemGroup>
</Project>
The Sdk attribute brings in the default build rules for .NET
projects. Properties go into a PropertyGroup:
OutputType Exe makes an executable, and
TargetFramework selects the .NET version. Items go into an
ItemGroup; each Compile item adds a source file.
Unlike C# projects, which compile all .cs files in the
directory, F# projects compile only the files listed here.
<TargetFramework>net11.0</TargetFramework>
The program runs on the .NET 11 runtime. To build for several versions,
we use the TargetFrameworks property with a semicolon
separated list, such as net10.0;net11.0.
The dotnet build command only compiles the project. We show
the first three lines of its output; the last one gives the path of the
built assembly. The rest is a summary with the number of warnings and
errors and the elapsed time.
λ dotnet build Determining projects to restore... All projects are up-to-date for restore. tally -> tally/bin/Debug/net11.0/tally.dll
Several source files
We turn the template into a small statistics tool. The calculations go
into their own file, Stats.fs.
module Stats
let mean (xs: float list) = List.sum xs / float xs.Length
let median (xs: float list) =
let sorted = List.sort xs
let n = sorted.Length
if n % 2 = 1 then sorted[n / 2]
else (sorted[n / 2 - 1] + sorted[n / 2]) / 2.0
Every file of a project except the last one must start with a
module or namespace declaration. The file
defines the Stats module with two functions.
[<EntryPoint>]
let main argv =
let xs = argv |> Array.map float |> List.ofArray
printfn "count %d" xs.Length
printfn "mean %.2f" (Stats.mean xs)
printfn "median %.2f" (Stats.median xs)
0
Program.fs stays the last file. Its
[<EntryPoint>] function receives the command line
arguments, as shown in
F# reading input and arguments.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net11.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="Stats.fs" />
<Compile Include="Program.fs" />
</ItemGroup>
</Project>
The new file is listed before Program.fs. Arguments after
-- are passed to the program instead of to
dotnet run.
λ dotnet run -- 4 8 15 16 23 42 count 6 mean 18.00 median 15.50
File order
The F# compiler reads the files in the order of the Compile
items, and a file can use only what the files above it define. This
keeps dependencies one-way: there are no cycles between files. We add a
Report module that uses Stats, but list it in
the wrong place.
module Report
let summary (xs: float list) =
[ sprintf "count %d" xs.Length
sprintf "mean %.2f" (Stats.mean xs)
sprintf "median %.2f" (Stats.median xs) ]
summary returns the lines of the report as a list of
strings.
[<EntryPoint>]
let main argv =
let xs = argv |> Array.map float |> List.ofArray
Report.summary xs |> List.iter (printfn "%s")
0
The main function now only prints the report.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net11.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="Report.fs" />
<Compile Include="Stats.fs" />
<Compile Include="Program.fs" />
</ItemGroup>
</Project>
Report.fs comes first, so the Stats module does
not exist yet when it is compiled. The build fails with error FS0039 for
each use of Stats. We have shortened the paths in the
output. Swapping the two Compile lines fixes the build. If
Program.fs is not the last file, the build fails with error
FS0222, because only the last file may omit the module declaration.
λ dotnet run -- 4 8 15 16 23 42 tally/Report.fs(5,30): error FS0039: The value, namespace, type or module 'Stats' is not defined. [tally/tally.fsproj] tally/Report.fs(6,30): error FS0039: The value, namespace, type or module 'Stats' is not defined. [tally/tally.fsproj] The build failed. Fix the build errors and run again.
Warnings as errors
Project properties also control the compiler. Some warnings are off by
default; WarnOn turns them on, and
TreatWarningsAsErrors makes every warning stop the build.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net11.0</TargetFramework>
<WarnOn>FS1182</WarnOn>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>
<ItemGroup>
<Compile Include="Stats.fs" />
<Compile Include="Report.fs" />
<Compile Include="Program.fs" />
</ItemGroup>
</Project>
Warning FS1182 reports unused variables. The files are now in the right order.
module Report
let summary (xs: float list) =
let sorted = List.sort xs
[ sprintf "count %d" xs.Length
sprintf "mean %.2f" (Stats.mean xs)
sprintf "median %.2f" (Stats.median xs) ]
The sorted value is left over from an earlier version and
is never used. With the default settings this compiles silently; here
the unused value fails the build.
λ dotnet run -- 4 8 15 16 23 42 tally/Report.fs(4,9): error FS1182: The value 'sorted' is unused [tally/tally.fsproj] The build failed. Fix the build errors and run again.
Class libraries
Code that several applications share goes into a class library. The
classlib template creates one. We move the statistics
functions into a library called Stats, next to the
tally directory.
λ dotnet new classlib -lang "F#" -o Stats The template "Class Library" was created successfully.
The template creates Library.fs with an example module. We
delete it, together with tally/Stats.fs, and write the
library file.
module Stats
let mean (xs: float list) = List.sum xs / float xs.Length
let median (xs: float list) =
let sorted = List.sort xs
let n = sorted.Length
if n % 2 = 1 then sorted[n / 2]
else (sorted[n / 2 - 1] + sorted[n / 2]) / 2.0
let range (xs: float list) = List.max xs - List.min xs
The module is the same as before, with a new range
function.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net11.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="Stats.fs" />
</ItemGroup>
</Project>
A library has no OutputType; it builds a
.dll that other projects load. The
dotnet add reference command makes the application depend
on the library.
λ dotnet add reference ../Stats Reference `..\Stats\Stats.fsproj` added to the project.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net11.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="Report.fs" />
<Compile Include="Program.fs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Stats\Stats.fsproj" />
</ItemGroup>
</Project>
The command added the ProjectReference item. We removed
Stats.fs from the application and, to keep the example
short, the two warning properties. Report.fs uses the
library module as before and adds the range.
module Report
let summary (xs: float list) =
[ sprintf "count %d" xs.Length
sprintf "mean %.2f" (Stats.mean xs)
sprintf "median %.2f" (Stats.median xs)
sprintf "range %.2f" (Stats.range xs) ]
dotnet run builds the library first, then the
application.
λ dotnet run -- 4 8 15 16 23 42 count 6 mean 18.00 median 15.50 range 38.00
Solutions
A solution file groups projects so that we can build and test them with
one command. dotnet new sln creates it and
dotnet sln add adds projects. Since .NET 10 the template
creates the XML-based .slnx format, which simply lists the
project files.
Running dotnet build in the directory with the solution
builds all its projects, each library before the projects that reference
it. The elapsed time in the build summary varies from run to run.
λ dotnet new sln -n Tally
The template "Solution File" was created successfully.
λ dotnet sln add Stats tally
Project `Stats/Stats.fsproj` added to the solution.
Project `tally/tally.fsproj` added to the solution.
λ cat Tally.slnx
<Solution>
<Project Path="Stats/Stats.fsproj" />
<Project Path="tally/tally.fsproj" />
</Solution>
λ dotnet build -v q
Build succeeded.
0 Warning(s)
0 Error(s)
Time Elapsed 00:00:00.16
NuGet packages
Libraries from other authors come as NuGet packages from
nuget.org.
dotnet add package adds a package reference to the project
and downloads the package. We add Humanizer, a library that turns
numbers, dates, and identifiers into readable text.
λ dotnet add package Humanizer.Core --version 2.14.1
λ cat tally.fsproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net11.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="Report.fs" />
<Compile Include="Program.fs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Stats\Stats.fsproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Humanizer.Core" Version="2.14.1" />
</ItemGroup>
</Project>
The command prints a long restore log, which we do not show. It adds a
PackageReference item with the package name and version.
Without --version it takes the latest stable version.
open Humanizer
[<EntryPoint>]
let main argv =
let xs = argv |> Array.map float |> List.ofArray
printfn "%s" ("tally_report".Humanize())
printfn "%s values" (xs.Length.ToWords())
Report.summary xs |> List.iter (printfn "%s")
0
open Humanizer brings in the extension methods of the
package. Humanize turns an identifier into a sentence and
ToWords spells out a number.
λ dotnet run -- 4 8 15 16 23 42 tally report six values count 6 mean 18.00 median 15.50 range 38.00
Packages in scripts
Scripts cannot have a project file. They load packages with the
#r "nuget: ..." directive. F# Interactive downloads the
package on the first run and caches it.
#r "nuget: Humanizer.Core, 2.14.1"
open System
open Humanizer
let durations = [ TimeSpan.FromSeconds 45.0; TimeSpan.FromMinutes 90.0; TimeSpan.FromDays 3.0 ]
for d in durations do
printfn "%s" (d.Humanize(2))
printfn "%s" ("delivery".ToQuantity(3))
The version after the comma is optional; without it the script gets the latest version, which may change between runs. Pinning the version keeps the script reproducible.
printfn "%s" (d.Humanize(2))
Humanize(2) shows a TimeSpan with at most two
units. ToQuantity puts a number in front of a word and uses
the plural form.
λ dotnet fsi durations.fsx 45 seconds 1 hour, 30 minutes 3 days 3 deliveries
Publishing
dotnet run and dotnet build use the
Debug configuration. For deployment we publish the
application in the Release configuration, which turns on
optimizations. dotnet publish copies the program with all
its dependencies into one directory. With -v q it prints
nothing on success.
The output directory has the program, the library, the Humanizer and
FSharp.Core assemblies, and resource directories with FSharp.Core
messages in other languages. Setting the
SatelliteResourceLanguages property to en
leaves those directories out. The tally file is a native
launcher for tally.dll. The published program still needs
the .NET runtime on the target computer.
λ dotnet publish -c Release -o ../out -v q λ ls out cs de es fr FSharp.Core.dll Humanizer.dll it ja ko pl pt-BR ru Stats.dll Stats.pdb tally tally.deps.json tally.dll tally.pdb tally.runtimeconfig.json tr zh-Hans zh-Hant λ ./out/tally 4 8 15 16 23 42 tally report six values count 6 mean 18.00 median 15.50 range 38.00
Self-contained applications
A self-contained application includes the .NET runtime, so it runs on
computers without .NET. It is built for one platform, given with
-r as a runtime identifier such as linux-x64,
win-x64, or osx-arm64.
PublishSingleFile packs the program, the libraries, and the
runtime into a single executable. It is large because it carries the
whole runtime; the .pdb files contain debug symbols and are
not needed to run it.
λ dotnet publish -c Release -r linux-x64 --self-contained -p:PublishSingleFile=true -o ../dist -v q λ ls dist Stats.pdb tally tally.pdb λ du -h dist/tally 78M dist/tally λ ./dist/tally 4 8 15 16 23 42 tally report six values count 6 mean 18.00 median 15.50 range 38.00
Source
Get started with F# with the .NET CLI
.NET application publishing overview
In this article we have created F# projects with dotnet new,
looked at the project file, ordered source files, turned warnings into
errors, moved code into a class library, grouped projects in a solution,
used NuGet packages in projects and scripts, and published the
application. Modules in project files are covered in
F# modules and namespaces, and the next
article, F# unit testing, adds a test
project to a solution.
Author
List all F# tutorials.