Skip to content
Tutorials Center
My learning

Topics

Lesson 2 of 39 ยท Getting Started

Installing a Fortran Compiler

Install gfortran on Windows, macOS or Linux, compile and run a Fortran program from the command line, and learn the most useful compiler flags for beginners.

Fortran is a compiled language: you write source code in a text file, a compiler translates it into a program your computer can run, and then you run that program. You need two things: a text editor (VS Code, Vim, anything) and a compiler. The most popular free compiler is gfortran, part of the GNU Compiler Collection, and it is what this course assumes.

Other good compilers exist: Intel's ifx, NVIDIA's nvfortran, LLVM's flang, and the interactive LFortran. Programs that follow the standard compile with any of them.

Installing gfortran

Linux (Debian/Ubuntu):

Terminal
sudo apt install gfortran

On Fedora use sudo dnf install gcc-gfortran; on Arch use sudo pacman -S gcc-fortran.

macOS: with Homebrew, brew install gcc installs gfortran as part of GCC.

Windows: the easiest routes are

  • WSL (Windows Subsystem for Linux): install Ubuntu, then follow the Linux steps.
  • MSYS2: install it, open the UCRT64 shell and run pacman -S mingw-w64-ucrt-x86_64-gcc-fortran.
  • Conda or Pixi: conda install -c conda-forge gfortran works on every platform.

Check the installation:

Terminal
gfortran --version

If you see a version number (anything from 11 upward is fine), you are ready.

Not ready to install anything yet? Online Fortran compilers exist, and you can paste any listing from this course into one. Installing locally is still worth doing, because you will want multiple files and libraries soon.

Compile and run

Save this program as hello.f90:

FORTRAN
program hello
  implicit none
  print *, "Hello, Fortran!"
end program hello

Compile it and run it:

Terminal
gfortran hello.f90 -o hello
./hello

On Windows (MSYS2 or Command Prompt) the program is hello.exe, and you run it with hello or .\hello. The output is:

TEXT
 Hello, Fortran!

There is a leading space: Fortran's default list-directed output starts each line with one blank. You will learn how to control it in the lesson on reading and writing.

The pipeline looks like this:

flowchart LR
  A["hello.f90<br/>source"] -->|gfortran| B["hello<br/>executable"]
  B -->|run| C["Output on screen"]

Compilation happens once; you can run the executable many times.

File extensions matter

The extension tells the compiler how to read the file:

Extension Meaning
.f90, .f95, .f03, .f08 free-form source (use .f90 for all modern code)
.F90 free-form, run through the C preprocessor first
.f, .for old fixed-form source (columns matter)

Modern code is almost always free-form .f90, even when it uses Fortran 2018 features. The digits describe the source form, not the standard version.

Useful compiler flags

Flags go on the command line. Start with this set while learning:

Terminal
gfortran -std=f2018 -Wall -Wextra -fcheck=all -g hello.f90 -o hello
Flag What it does
-std=f2018 warn about anything that is not standard Fortran 2018
-Wall -Wextra turn on helpful warnings (unused variables, etc.)
-fcheck=all check array bounds and other mistakes at run time
-g include debug information for debuggers
-O2 optimize for speed (use once the program is correct)
-o name name of the output program
-c compile only, produce an object file .o
-fimplicit-none behave as if implicit none were everywhere

A good habit: develop with the checking flags; switch to -O2 for production runs. A run-time check can turn a silent wrong answer into a clear error message such as an array index outside its bounds, which is a great learning aid.

Compiling several files

Large programs are split into files. You can compile them in one command (the module file first, because the main program uses it):

Terminal
gfortran geometry.f90 main.f90 -o app

or step by step, which is what build tools do:

Terminal
gfortran -c geometry.f90      # creates geometry.o and geometry.mod
gfortran -c main.f90
gfortran geometry.o main.o -o app

Modules also produce .mod files. They are how the compiler learns what a module contains, so keep them next to your sources while building. Later chapters add make, and the Fortran Package Manager (fpm), to automate this.

Common compile errors

  • Error: Symbol 'x' at (1) has no IMPLICIT type: you used a variable that was never declared (with implicit none active). Declare it.
  • Error: Unclassifiable statement at (1): a typo or a missing character, such as a missing ::.
  • Error: Type mismatch: you passed the wrong kind of value, for example a string where a number is expected.
  • undefined reference to ... at link time: a procedure or module file is missing from the command.

The (1) in the message points at the exact place in the source line, shown with a caret 1 under it. Read the first error first; later errors are often consequences of it.

A runnable stand-in: what a compile step catches

Python has no separate compile step, but we can imitate the idea of "declare before use" with a tiny checker. This is only a teaching toy, not how a compiler works.

Python Stand-in (Python): a toy declaration checker

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 input()), it gets the next line from this box.

Python Stand-in (Python): fix it by declaring

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 input()), it gets the next line from this box.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which command compiles hello.f90 into a program called hello?

    gfortran compiles the source file and -o sets the name of the executable.
  2. 2. Which file extension is best for modern free-form Fortran?

    .f90 means free-form source and is used for all modern code, even Fortran 2018 programs.
  3. 3. What does the flag -fcheck=all do?

    It adds run-time checks, so an out-of-range array index produces an error message instead of a silent wrong result.
  4. 4. What are .mod files?

    When you compile a module, the compiler writes a .mod file that other files read to learn what the module provides.

Summary

  • Fortran programs are compiled; gfortran is a free, popular compiler for Windows, macOS and Linux.
  • gfortran file.f90 -o name compiles; run the result as ./name.
  • Use .f90 for modern free-form source.
  • While learning, compile with -std=f2018 -Wall -Wextra -fcheck=all -g.
  • Modules create .mod files and must be compiled before the files that use them.

Next: Your First Fortran Program