F# Tutorial - Hello World in 5 minutes
Intro
Purpose
Install .NET and create your first application written in the F# programming language.
Prerequisites
None.
macOS 12.0 or later versions.
Time to Complete
5 minutes
Scenario
A simple application written in F# that prints Hello, World!
to the console.
Download and install
To start building .NET apps, download and install the .NET SDK.
Download .NET 9 SDK x64 (Intel)
Download .NET 9 SDK Arm64 (Apple Silicon)
If you're on a Mac with an Apple M1 or M2 chip, you need to install the Arm64 version of the SDK.
Check everything installed correctly
Once you've installed, open a new command prompt and run the following command:
Once you've installed, open a new terminal and run the following command:
dotnet --version
If the installation succeeded, you should see version 9.0.100 or higher outputted:
9.0.100
If everything looks good, select the Continue button below to go to the next step.
Got an error?
If you receive a 'dotnet' is not recognized as an internal or external command error, make sure you opened a new command prompt. If quickly restarting your machine doesn't resolve the issue, use the I ran into an issue button to get help fixing the problem.
Create your app
In your command prompt, run the following command to create your app:
In your terminal, run the following command to create your app:
dotnet new console -lang F# -o MyFSharpApp
Note: some terminals may require you to add quotes around F#
like this: "F#"
.
Then, navigate to the new directory created by the previous command:
cd MyFSharpApp
The dotnet
command creates a new application of type console for you. The -lang
parameter specifies the F# programming language and -o
creates a directory named MyFSharpApp
where your app is stored and populates it with the required files. The cd MyFSharpApp
command puts you into the newly created app directory.
The main file in the MyFSharpApp
folder is Program.fs
. By default, it already contains the necessary code to write "Hello World from F#!" to the Console.
// For more information see https://aka.ms/fsharp-console-apps
printfn "Hello from F#"
Select the Continue button below to go to the next step.
Got an error?
If you receive a message similar to Template "Console Application" could not be created. Access to the path 'C:\Windows\System32\MyApp' is denied., change your current directory to one where you have permissions to create a new folder and try to run the command again.
If Windows can't find the SDK when you try to create the project and you are sure you have installed the SDK, your machine might have an issue with the PATH environment variable. See this Stack Overflow post for instructions on how to diagnose and fix this issue.
If you can't resolve the issue you're having, select the I ran into an issue button below to get help fixing the problem.
Run your app
In your command prompt, run the following command:
In your terminal, run the following command:
dotnet run
If your app ran successfully, you should see the following output:
Hello from F#
Congratulations, you've built and run your first F# app! Select the Continue button below to go to the next step.
Edit your code
Open Program.fs
in any text editor and replace all of the code with the following. If you want to, you can replace the name Ana
with your name.
// Define a new function to print a name.
let printGreeting name =
printfn $"Hello {name} from F#!"
// Call your new function!
printGreeting "Ana"
Save the Program.fs
file and run your code again.
dotnet run
If your app ran successfully, you should see an output similar to the following:
Hello Ana from F#!
Next steps
Now that you've got the basics, let's dig deeper into the language, with a self-guided learning of F#.
Microsoft Learn: Take your first steps with F#
F# for Beginners
Let Luis walk you through the basics of the F# language and learn how to code in F#:
You might also be interested in...