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.
- Debian and Ubuntu: sudo apt update, then sudo apt install gfortran.
- Rocky Linux, Fedora and the rest of the Red Hat family: sudo dnf install gcc-gfortran. The package is named after the GCC source package, so it carries the C compiler with it.
- Alpine and other apk systems: sudo apk add gcc-gfortran.
- Arch and its derivatives: sudo pacman -S gcc-fortran.
- FreeBSD: sudo pkg install lang/gcc. The Ports tree builds the same compiler from source when a binary package is not enough.
- macOS: brew install gcc. Homebrew ships gfortran inside its gcc formula, so one install gives the C and the Fortran compilers.
- Windows: MSYS2 with the UCRT64 environment, then pacman -S mingw-w64-x86_64-gcc-fortran. A Linux virtual machine or Docker is the shorter route for a reader who already has either.
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.
- Install gfortran on Ubuntu
- Install gfortran on Rocky Linux
- Install gfortran on Windows 11
- Install gfortran on macOS
- Install gfortran on FreeBSD
- Fortran in Docker, which is how the code on these pages was built.
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:
- -Wall turns on the warnings that are almost always a mistake: an unused variable, a name used before it is set, a comparison that cannot be true.
- -Wextra adds the rest of them, including the ones that a program can be right about but rarely is.
- -std=f2018 fixes the standard. Without it gfortran accepts a mixture of standards and GNU extensions, and a file that compiles on one machine then fails on another. With it, the compiler rejects anything that is not Fortran 2018, which is the version this course teaches.
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
- Install gfortran on your system, then run the version command. Which GCC release do you have?
- Build the check program without -std=f2018. Does anything change in the output?
- Remove only from the use line and print int32 as well.
- 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.
-
Which command installs gfortran on Ubuntu?
Answer
sudo apt install gfortran
Debian and Ubuntu name the package after the compiler: gfortran.
-
Which package provides gfortran on Rocky Linux?
Answer
gcc-gfortran
The Red Hat family names it after the GCC source package, gcc-gfortran.
-
What does
-std=f2018do?Answer
It makes the compiler accept Fortran 2018 and reject other standards
The flag fixes the language standard the compiler checks the source against.
-
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.
-
What does this program print?
program p use iso_fortran_env, only: int32 implicit none print *, int32 end program pAnswer
4
int32 is the kind value of a 4-byte integer on this compiler.
-
What does
gfortran --versionprint 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.
-
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.
-
What does
-gadd 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.
-
Why name the binary with
-oon 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.
-
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