Hello World (first programs) · Beginner · 15 min
Hello World in F#: dotnet, printfn and F# scripts
By Padding · Updated 18 Nov 2026
Ubuntu 26.04 · .NET 10 LTS SDK (runtime 10.0.15, F# 10) · mcr.microsoft.com/dotnet/sdk:10.0
You will create and run an F# 10 console app with the .NET 10 SDK, run the same idea as an F# script with dotnet fsi, and extend it with a greet function, the pipeline operator and pattern matching on the command-line arguments.
On this page
F# is the functional-first language of .NET. It runs on the same runtime as C#, compiles to the same intermediate language (IL), and can use any .NET library, from the standard System classes to the thousands of packages on NuGet. What changes is how you write code. Functions are values you pass around, data is immutable unless you say otherwise, the compiler works out almost every type for you, and indentation is part of the syntax, as in Python.
F# 10 ships inside the .NET 10 SDK, the current long-term support (LTS) release of .NET. There is no separate F# install: if dotnet works, F# works. In this tutorial you build a console project, run a script without a project, and then write a small program that shows three things F# code leans on every day: functions, the |> pipeline and pattern matching.
You work in a throwaway folder, ~/fsharp-hello-demo, and delete it at the end.
Step 1 · Install the .NET 10 SDK
The SDK (software development kit) holds the .NET runtime, the C# and F# compilers, F# Interactive and the dotnet command. On Ubuntu 26.04 it is in Ubuntu's own archive:
sudo apt update
sudo apt install -y dotnet-sdk-10.0On Windows and macOS, and for the details of Ubuntu's and Microsoft's packages, follow the first step of Hello World in C#; it installs the same SDK. The SDK is a large download, so on prepaid mobile data it is worth doing once on Wi-Fi. Then check what you have:
dotnet --version
dotnet --list-sdksCheck it worked
dotnet --version prints a version that starts with 10.0., such as 10.0.114 from Ubuntu's package or 10.0.404 from Microsoft. --list-sdks shows it with its install path. If you see No .NET SDKs were found, you have only the runtime; install dotnet-sdk-10.0.
Step 2 · Create an F# console project
dotnet new console makes a C# project unless you ask for another language. The -lang "F#" option picks the F# template. Quote F#, because in many shells # starts a comment and the rest of the line would be ignored:
mkdir -p ~/fsharp-hello-demo
cd ~/fsharp-hello-demo
dotnet new console -lang "F#" -o HelloFs
cd HelloFs
ls
cat Program.fs-o HelloFs creates a HelloFs folder and names the project after it. Inside are HelloFs.fsproj, Program.fs and an obj folder the SDK uses for its working files. Program.fs has two lines:
// For more information see https://aka.ms/fsharp-console-apps
printfn "Hello from F#"//starts a comment. The compiler ignores the rest of the line.printfnis F#'s print function. Thenat the end means it adds a newline after the text."Hello from F#"is a string, and it is the argument toprintfn. F# calls a function by writing the argument after it with a space, with no brackets and no commas.- There is no semicolon, no class and no
mainfunction. Code at the top level of the last file in a project runs from top to bottom when the program starts.
Check it worked
ls lists HelloFs.fsproj, Program.fs and obj, and cat shows the comment and the printfn line.
Step 3 · Read the project file and run it
Look at the project file:
cd ~/fsharp-hello-demo/HelloFs
cat HelloFs.fsprojMost of it matches a C# project: <OutputType>Exe</OutputType> says the result is a runnable program, and <TargetFramework>net10.0</TargetFramework> says it targets .NET 10. One part is specific to F#:
<ItemGroup>
<Compile Include="Program.fs" />
</ItemGroup>An F# project lists its source files, and the order of that list is the order they are compiled in. A file can only use what is defined above it in the same file, or in files listed before it. C# finds code anywhere in the project; F# deliberately does not, so you can read a project from top to bottom and never meet a name before its definition. When you add a second .fs file, add a <Compile> line for it above Program.fs.
Build and run the program:
cd ~/fsharp-hello-demo/HelloFs
dotnet rundotnet run compiles the project into bin/Debug/net10.0/HelloFs.dll and starts it on the .NET runtime. The first build takes a few seconds; later ones only rebuild what changed.
Check it worked
The terminal prints Hello from F#, and a bin folder appears next to obj. If you see Couldn't find a project to run, cd into the HelloFs folder first.
Step 4 · Run an F# script with dotnet fsi
F# also runs as a scripting language. F# Interactive, started with dotnet fsi, compiles and runs code on the spot. A file with the .fsx extension is a script: no project, no bin folder. Write one in its own folder:
mkdir -p ~/fsharp-hello-demo/scripts
cd ~/fsharp-hello-demo/scripts
cat > hello.fsx <<'FSX'
let args = fsi.CommandLineArgs |> Array.skip 1
let who = if args.Length > 0 then args[0] else "World"
printfn "Hello, %s, from an F# script." who
printfn "Running on .NET %s" (string System.Environment.Version)
FSX
dotnet fsi hello.fsx
dotnet fsi hello.fsx AdaLine by line:
letgives a name to a value.argsandwhocannot be changed afterwards; F# values are immutable by default.fsi.CommandLineArgsis an array of the words on the command line, and its first item is the script's own name.Array.skip 1drops that item, leaving only the words you typed after it.if ... then ... elseis an expression that produces a value, so it can sit on the right oflet. Both branches must give the same type, herestring.args[0]is the first item of the array.- In
printfn "Hello, %s, ..." who,%sis a placeholder for a string, andwhofills it. The compiler checks placeholders against the values you pass. string System.Environment.Versionturns the runtime version into text. The brackets around it make it a single argument toprintfn.System.Environmentis an ordinary .NET class, used from F# with no wrapper.
You can also type code straight into F# Interactive. Run dotnet fsi on its own, type a line ending with two semicolons, and press Enter:
let greet name = sprintf "Hello, %s!" name;;F# Interactive replies with the type it worked out, something like val greet: name: string -> string. You never wrote string: the %s placeholder told the compiler that name is a string and that sprintf returns one. Type #quit;; to leave. In a script, a line such as #r "nuget: <package>" pulls a NuGet package into the script; the F# Interactive documentation covers it.
Check it worked
The first run prints Hello, World, from an F# script. and the second prints Hello, Ada, from an F# script. Both end with Running on .NET 10.0. and a patch number. The scripts folder still holds only hello.fsx.
Step 5 · See type inference catch a mistake
Type inference does not mean types are loose. Every value has one fixed type, decided when the code compiles, and F# does not convert between them for you. Write a script with a deliberate mistake:
cd ~/fsharp-hello-demo/scripts
cat > oops.fsx <<'FSX'
let price = 1500
let label = "Price: " + price
printfn "%s" label
FSX
dotnet fsi oops.fsx || echo "F# refused to run oops.fsx"price is an int and "Price: " is a string, and + will not join the two. Nothing runs, not even the lines before the mistake: the whole script is checked first. Fix it by converting the number yourself, either with "Price: " + string price or with a placeholder, sprintf "Price: %d" price, where %d stands for an integer.
Check it worked
The output contains error FS0001 with something like The type 'string' does not match the type 'int', the file name and a line and column number pointing at line 2, then F# refused to run oops.fsx.
Step 6 · Add a greet function, the pipeline and pattern matching
Now go one step further in the console project. Replace Program.fs:
cd ~/fsharp-hello-demo/HelloFs
cat > Program.fs <<'FS'
let greet name = sprintf "Hello, %s!" name
let shout (text: string) = text.ToUpperInvariant()
[<EntryPoint>]
let main argv =
match List.ofArray argv with
| [] ->
printfn "%s" (greet "World")
0
| [ name ] ->
name |> greet |> printfn "%s"
0
| [ name; "--loud" ] ->
name |> greet |> shout |> printfn "%s"
0
| _ ->
eprintfn "Usage: dotnet run -- [name] [--loud]"
1
FS
dotnet run
dotnet run -- Ada
dotnet run -- Chiamaka --loud
dotnet run -- too many words || echo "Exit code: $?"What each part does:
let greet name = ...defines a function. A function is aletwith parameters. Its body is one expression, and its value is what the function returns; F# has noreturnkeyword.let shout (text: string) = ...writes the type out,: string. Here inference needs the hint, because.ToUpperInvariant()is a .NET method and the compiler must know which type's method to call.ToUpperInvariantuppercases text the same way on every machine, whatever the language settings, and it handles letters such as Yorùbáéandọ.[<EntryPoint>]marksmainas the place the program starts.argvis an array of the command-line words. The entry point must be the last thing in the last file, which fits the compile-order rule, and it returns an integer exit code:0for success, anything else for failure.List.ofArray argvturns the array into an F# list, which is easier to match on.match ... withcompares the list against each pattern below it, from top to bottom, and runs the first one that fits.[]is an empty list.[ name ]is a list of exactly one item, and it binds that item to the namename.[ name; "--loud" ]is two items where the second is the exact text--loud._matches anything left.name |> greet |> printfn "%s"is the pipeline.x |> fmeansf x: the value on the left goes in as the last argument of the function on the right. Read it left to right: take the name, greet it, print it. It does the same asprintfn "%s" (greet name), without the nesting.eprintfnprints to standard error instead of standard output, which is where error messages belong.- The indentation under each
|is the body of that branch. Lines at the same depth belong together, and moving one left ends the block.
If you delete the | _ -> branch and build again, the compiler warns Incomplete pattern matches on this expression (warning FS0025) and suggests a value you did not cover. That check is one of the main reasons people choose F#.
Check it worked
The four runs print Hello, World!, Hello, Ada!, HELLO, CHIAMAKA!, and then the usage line followed by Exit code: 1.
Step 7 · Avoid common beginner mistakes and clean up
Mistakes that trip up most first F# programs:
- Tabs for indentation. F# rejects tab characters in indentation by default. Set your editor to insert spaces; four is the usual choice.
- Wrong indentation. A line indented less than the block it belongs to ends that block early, which shows up as errors such as
FS0010: Unexpected ... in bindingorpossible incorrect indentation. Line up lines that belong together. - Calling with brackets and commas.
greet("Ada")happens to work, butadd(1, 2)passes one pair of values, not two arguments. Writeadd 1 2for a function defined aslet add a b = a + b. - Unquoted
F#in the shell.dotnet new console -lang F#can arrive as-lang For with the rest of the line cut off. Keep the quotes. - Using a name before it is defined. A function lower in the file, or in a file later in the
.fsproj, is not visible yet. Move it up. - A placeholder that does not match.
printfn "%d" "Ada"is a compile error, because%dexpects an integer. Use%sfor strings,%dfor integers,%ffor decimals, or%Ato print almost any value in a readable form.
Where F# is used: banks, trading firms and insurers use it for pricing, risk and other finance code, where its types catch mistakes before they reach money. It is popular for data and analytics work, from scripts run in F# Interactive to notebooks. It also powers .NET web back ends, with ASP.NET Core directly or with F# libraries such as Giraffe, and it can call any C# library, so a team can add F# to an existing .NET system one project at a time.
When you are done, remove the demo folder:
rm -rf ~/fsharp-hello-demoTo remove .NET itself on Ubuntu, run sudo apt remove dotnet-sdk-10.0 and then sudo apt autoremove. Skip that if you also use C# or another .NET tool.
Check it worked
ls ~/fsharp-hello-demo reports No such file or directory.
Next steps
- The same SDK, with the object-oriented .NET language: Hello World in C#.
- F# grew out of the ML family, and OCaml is its closest relative: Hello World in OCaml.
- For a language that is purely functional rather than functional-first: Hello World in Haskell.
Related tutorials
- Hello World in C#: dotnet new, top-level statements and file-based appsYou will install the .NET 10 SDK, create and run a C# console app with dotnet new and dotnet run, run a single .cs file with no project, read a name safely from the keyboard and publish a self-contained executable.
- Hello World in OCaml: opam, utop and a dune projectYou will install OCaml 5.5 with opam, run Hello World as a script, compile it to bytecode and native code, and build a dune project with a greet function whose type OCaml works out for you.
- Hello World in Haskell: GHCup, ghci and IOYou will install GHC 9.14 with GHCup, try expressions in ghci, run and compile Hello World, and write a program that keeps a pure greet function apart from the IO that reads your name.
Need this done for you?
Padding does this work for growing companies, from the first diagnosis to a system that keeps working.
Get new tutorials