Skip to content
Tutorials Center
My learning

Topics

Lesson 2 of 37 ยท Getting Started

Installing .NET and Using F# Interactive (dotnet fsi)

Install the .NET SDK, run F# in the interactive shell (dotnet fsi), execute .fsx scripts, create a console project, and pick an editor such as VS Code, Rider or Visual Studio.

To write F# you need the .NET SDK. It contains the F# compiler, the dotnet command-line tool and F# Interactive (FSI), a shell where you type code and see results immediately. This lesson gets you from nothing to a working setup, and shows the three ways you will run F# code: the interactive shell, script files, and full projects.

F# examples on this site are read-only because the site's runners cannot execute F#. Everything here runs on your own machine once the SDK is installed. Runnable C# stand-ins are labelled.

Install the SDK

Download the installer for your system from the official .NET website (search for "download .NET SDK") and run it. Pick the current LTS or latest version; any modern SDK includes F#. Linux users can install through their package manager, and macOS users can use the installer or Homebrew.

Check the installation in a terminal:

Terminal
dotnet --version

If a version number prints, you are ready. dotnet --list-sdks shows every installed SDK.

F# Interactive: your scratch pad

Start the shell with:

Terminal
dotnet fsi

You get a prompt. Type an expression, end it with ;; (two semicolons) and press Enter. FSI evaluates it and prints the result with its type:

FSHARP
> 2 + 3 * 4;;
val it: int = 14

> let greeting = "Hello, " + "F#";;
val greeting: string = "Hello, F#"

> String.length greeting;;
val it: int = 9

Notice the pieces of the answer. val means a value, it is the automatic name FSI gives to an unnamed result, then a colon, the type, and the value. Type #quit;; (or press Ctrl+D) to leave.

The ;; is only an FSI convention telling the shell "I'm done typing, run it". It is not part of F# programs. FSI also understands a few directives that start with #:

Directive Meaning
#help;; Show help
#load "file.fsx";; Run another script first
#r "nuget: Package";; Reference a NuGet package (see below)
#time "on";; Print how long each command takes
#quit;; Exit

Script files (.fsx)

A file ending in .fsx is an F# script. Create hello.fsx:

FSHARP
let name = "world"
printfn "Hello, %s!" name

Run it:

Terminal
dotnet fsi hello.fsx

Output:

TEXT
Hello, world!

Scripts need no project file and start quickly, so they are perfect for experiments, data exploration and small automation. A script can also pull in packages directly:

FSHARP
#r "nuget: Newtonsoft.Json, 13.0.3"

open Newtonsoft.Json

let json = JsonConvert.SerializeObject {| name = "Ada"; year = 1815 |}
printfn "%s" json

The first run downloads the package; later runs use the cache. Arguments passed on the command line are available in the array fsi.CommandLineArgs.

Projects

Real applications use a project (.fsproj). Create one with the template tool:

Terminal
dotnet new console -lang "F#" -o HelloApp
cd HelloApp
dotnet run

The generated folder contains two files you should recognise:

  • HelloApp.fsproj: the project definition (target framework, packages, and the ordered list of source files).
  • Program.fs: the entry point.

A minimal Program.fs is just this:

FSHARP
printfn "Hello from F#"

Other useful commands:

Command What it does
dotnet build Compile the project
dotnet run Build and run
dotnet test Run tests
dotnet add package Name Add a NuGet package
dotnet new list Show available templates

F# compiles files in the order listed in the .fsproj. A file can only use code from files above it. If you add a file in the wrong position you will see "not defined" errors for things that clearly exist. Visual Studio and Rider have "move up/down" commands; with VS Code and Ionide you can edit the <Compile Include=...> lines by hand.

flowchart TD
    A["Quick experiment"] --> B["dotnet fsi (interactive)"]
    C["Small automation or data work"] --> D["hello.fsx script"]
    E["Application or library"] --> F[".fsproj project"]

Pick the lightest tool that fits the job.

Choosing an editor

  • Visual Studio Code with the Ionide extension: lightweight, free, excellent for F#. Select a line and press Alt+Enter to send it to FSI.
  • JetBrains Rider: a full IDE with strong refactoring and debugging.
  • Visual Studio (Windows): the classic full IDE, includes F# tooling.

Whichever you choose, enable format-on-save with the Fantomas formatter. F# is whitespace sensitive, so a consistent layout matters, and Fantomas removes all debate about it.

A runnable stand-in: top-level code

Modern F# and C# both let you write a program as a series of top-level lines. This C# stand-in is the shape of the "hello" program above, so you can see that no class or Main method is needed in either language.

C# C# stand-in: top-level program

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Common setup problems

  • dotnet is not recognised: open a new terminal after installing so the PATH refreshes.
  • FSI waits forever: you forgot ;; at the end of an interactive line.
  • Strange indentation errors in scripts: mixing tabs and spaces. F# forbids tab characters; configure your editor to insert spaces.
  • Package restore fails: check your internet connection and the package version number.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which command starts F# Interactive?

    dotnet fsi starts the interactive shell, and dotnet fsi file.fsx runs a script.
  2. 2. What is the ;; for in FSI?

    The double semicolon is only an interactive-shell convention; it is not used in normal source files.
  3. 3. Why can F# project files break when you add a new file in the wrong place?

    Order matters in an .fsproj. Dependencies must come before the files that use them.
  4. 4. Which line references a NuGet package from a script?

    The #r nuget directive downloads and references a package inside a script or FSI session.

Summary

  • Install the .NET SDK; it includes the F# compiler and F# Interactive.
  • dotnet fsi is an interactive shell where ;; ends an input.
  • .fsx files are scripts run with dotnet fsi file.fsx; they can reference NuGet packages.
  • dotnet new console -lang "F#" creates a project; file order in the .fsproj matters.
  • VS Code with Ionide, Rider and Visual Studio all support F#; use Fantomas to format.

Next: Your first F# program