Fortran Setup

Choosing a Compiler

All compilers below implement the current Fortran 2023 standard to a high degree and — critically for learning — ship excellent diagnostic messages. Pick the one your platform makes easiest; you can always add another later, since each installs side by side.

CompilerVendorBest forNotes
gfortranGNU (GCC)Everything, every OSThe default teaching compiler; -fcoarray=lib, -fopenmp, -fopenacc built in.
flang-newLLVMOptimization studyThe LLVM front-end; compare -O3 codegen against gfortran.
ifx / ifortIntelHPC clusters, XeonFree for students and open source; best tuned for Intel CPUs and MKL.
lfortranLFortran communityBrowser playgroundInteractive notebook-style REPL; also powers the official online playground.

The rest of this track uses gfortran in examples, because it is the install-everywhere compiler with the most complete free diagnostics. Everything shown compiles identically on the others.

Installing the toolchain

GNU Fortran ships with every major package manager; there is no install wizard to hunt down.

Verify the installation — the version numbers below simply confirm the compiler talks to you; anything ≥ 12 is fine for this track:

$ gfortran --version
GNU Fortran (GCC) 13.2.0
$ which gfortran      # a real path, not "command not found"

If the second command prints command not found, the terminal session was started before the install finished (Windows/MSYS2) or the package manager needs relogin — restart the shell before debugging further.

Your first program

Create a file named hello.f90 with any editor, and type the program below exactly. The .f90 extension tells every compiler to use free form (the modern layout); a plain .f means legacy fixed form, which the syntax lesson explains. Note the three lines every modern Fortran program should have: program naming the unit, implicit none banning silent typo-variables, and an end program that optionally repeats the name — gfortran will verify the name matches the opening statement.

program hello
  ! The first program in every language. Nothing clever, everything mandatory.
  implicit none                  ! never let a typo invent a variable
  character(len=12) :: name      ! a fixed-length string (learned in Strings)
  write (*, '(a)', advance='no') 'What is your name? '
  read (*, '(a)') name
  print '(a,a,a)', 'Hello, ', trim(name), '!'
end program hello

Compile and run

Compilation happens in two invisible stages — translation to object code, then linking — but one command does both. The -o hello flag names the output executable; without it gfortran writes a file called a.exe on Windows or a.out elsewhere.

$ gfortran -Wall -Wextra -std=f2018 -o hello hello.f90
$ ./hello
What is your name? Ada
Hello, Ada!

Two flags deserve early respect. -Wall -Wextra turns on warnings that catch real bugs — unused variables, suspicious integer conversions, deleted-feature usage — before a human does. -std=f2018 (or -std=f2023 on newer compilers) makes the compiler reject obsolete constructs so your code stays portable forward. During HPC work you add -O3; during debugging you add -g -fcheck=bounds -fbacktrace. The performance lesson details when each set buys you something real.

Reading compiler diagnostics

Compilers are the best teachers when you let them talk. Deliberately break the program above — remove the implicit none line and rename read to real — and watch gfortran answer with a file name, a line, a column caret, and an explanation:

$ gfortran -std=f2018 hello.f90
hello.f90:5:13:
    5 |  real (*, '(a)') name
      |             1
Error: Symbol 'real' at (1) has no IMPLICIT type

This is the loop of Fortran learning: write small, compile often, and let the diagnostic point at the exact character. The errors lesson later teaches how to read the nastier ones (linker errors, array-shape mismatches) defensively.

Editor and tools

Fortran is well served in VS Code: install the Modern Fortran extension, which bundles the language server fortls and provides hover documentation, go-to-definition across module boundaries, and live diagnostics. The base package also gives syntax highlighting (the same grammar this site's code viewer uses). Whatever you use, the workflow is the same: file → save → compile in the terminal.

Scaffold a project with fpm

fpm (Fortran Package Manager) is the community build tool — the part of the ecosystem that finally made dependency management normal. It fills the same role as cargo for Rust or mix for Elixir: one manifest declares sources, dependencies, and executables, and everything is compiled in the right order. Install it from fpm.fortran-lang.org (Windows builds are provided as release ZIPs), then try the full cycle:

$ fpm new helloworld     # creates src/, test/, fpm.toml and a stub app
$ cd helloworld
$ fpm run                # builds and executes the default app target
 Hello, world!
$ fpm test               # builds and runs the unit-test target

The ecosystem lesson returns to fpm in depth: adding dependencies, profiles, and publishing. For now it matters only that you can create a real multi-file project without a makefile.

Done? Run gfortran --version one more time and compile hello.f90 from this page. When both succeed, the environment is ready and the next lesson — Fortran Syntax — starts the language itself.