Install gfortran on Linux, macOS and Windows

A Fortran program needs one thing before it runs: a compiler. This course uses gfortran, the Fortran front end of GCC, because it is free, it is in the package manager of every system below, and it takes the modern standard with one flag. This lesson says which package provides it, links the page that walks through each system, and builds a first program with the exact command line the rest of the course uses.

The package per system

The compiler is one package, and the package name follows the package manager rather than the language. The commands below are the whole install step on each family; every one of them needs administrator rights, which is what sudo asks for.

The install guides

Five of those systems have a page of their own in this course, with the update step, the install step, the version check and the PATH problem each one tends to have. The commands above are the summary; the pages are the walkthrough.

A reader who does not want to install anything can use the Docker image from the last link: the compiler is already inside it, and the run command is on that page.

The compiler this course uses

Ask the compiler what it is. The answer has two version numbers to read: the GCC release, which is the compiler, and the release of the package the distribution built it from, which can differ by a suffix.

gfortran --version

GNU Fortran (Alpine 15.2.0) 15.2.0
Copyright (C) 2025 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Every page of this course was built with that compiler, GNU Fortran 15.2.0, on Alpine Linux 3.24.3. Any release from 9 onward takes everything the course uses, and the pages that need a newer one say so. A program can print the same string itself, which is the shortest way to prove that a machine can build the rest of the course.

A first compile

Save this program as check.f90. It uses one module from the standard library, iso_fortran_env, twice: once for the string that names the compiler, once for the kind of a double precision real. A compiler that cannot build it is not ready for the course.

program check use iso_fortran_env, only: compiler_version, compiler_options, real64 implicit none real(real64) :: pi pi = 4.0_real64 * atan(1.0_real64) print *, "compiler:", compiler_version() print *, "real64 pi:", pi end program check

gfortran -Wall -Wextra -std=f2018 check.f90 -o check
./check

compiler:GCC version 15.2.0
real64 pi: 3.1415926535897931

Two things in that output are worth a second look. The compiler reports itself as GCC version 15.2.0 and not as GNU Fortran: the Fortran front end is one part of GCC, and the string comes from the driver. And atan(1.0) times four gives pi to sixteen digits, which is what real64 means on this machine.

iso_fortran_env is imported with only so the file states what it uses. The list is checked by the compiler: a name that the module does not export is an error, not a name that silently comes from somewhere else.

The three flags

Every build command in this course carries the same three flags, and the reason is worth stating once:

Two more flags appear later in the course and are worth knowing now. -O2 lets the optimiser rewrite loops and arrays, and it is what a release build uses. -g keeps the source line numbers in the binary so a debugger and a runtime error can name the line; the next lesson uses it.

One habit from the command line saves a class of bugs. Name the output with -o every time, and use the same name as the source without the extension: a build that fails leaves the previous binary in place, and a reader who runs ./check without looking at the build sees the old answer instead of an error.

Exercises

  1. Install gfortran on your system, then run the version command. Which GCC release do you have?
  2. Build the check program without -std=f2018. Does anything change in the output?
  3. Remove only from the use line and print int32 as well.
  4. Build with -O2 and compare the output with the unoptimised build. Which line could change, and why?

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. Which command installs gfortran on Ubuntu?

    Answer

    sudo apt install gfortran

    Debian and Ubuntu name the package after the compiler: gfortran.

  2. Which package provides gfortran on Rocky Linux?

    Answer

    gcc-gfortran

    The Red Hat family names it after the GCC source package, gcc-gfortran.

  3. What does -std=f2018 do?

    Answer

    It makes the compiler accept Fortran 2018 and reject other standards

    The flag fixes the language standard the compiler checks the source against.

  4. Why does every build command in the course pass -Wall -Wextra?

    Answer

    Because the warnings they add are almost always a real mistake

    The two flags turn on the diagnostics that catch unused and uninitialised names, among others.

  5. What does this program print? program p use iso_fortran_env, only: int32 implicit none print *, int32 end program p

    Answer

    4

    int32 is the kind value of a 4-byte integer on this compiler.

  6. What does gfortran --version print first?

    Answer

    GNU Fortran (Alpine 15.2.0) 15.2.0

    The first line names the front end and its version, with the distribution in brackets.

  7. What is the difference between the two numbers in GNU Fortran (Alpine 15.2.0) 15.2.0?

    Answer

    The bracketed one is the distribution build, the last one the compiler version

    A distribution can rebuild the same compiler release under its own suffix.

  8. What does -g add to the build?

    Answer

    Source line numbers in the binary, for a debugger and a runtime error

    The debug information is what lets gdb and the Fortran runtime name a line.

  9. Why name the binary with -o on every build?

    Answer

    A failed build leaves the old binary, and the old output looks like success

    The write of the binary is the last step, so a failed compile leaves the previous one in place.

  10. Which flag keeps the source line numbers for the debugger?

    Answer

    -g

    -g writes the debug information; -O2 is the optimiser.

The programs on this page were built with GNU Fortran (Alpine 15.2.0) 15.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container. The package commands were not run: each one needs its own system and administrator rights.

Article author: Arthur Isaev