Installing and Running Fortran Program on Linux

Linux TLDR
Last Updated:
Reading time: 2 minutes

Fortran, which stands for “Formula Translation“, was initially created by IBM in the 1950s to serve as a high-level programming language for scientific and engineering calculations.

It is commonly used in areas where performance and numerical accuracy are critical, such as physics, astronomy, geophysics, computational chemistry, and fluid dynamics.

While Fortran remains strong in numerical and scientific computing, its popularity has waned compared to modern programming languages like Python, C/C++, and Julia.

Yet, it still maintains a dedicated user base in niche domains, and we’ll show you how to install it on your preferred Linux distribution and run your first program.

Tutorial Details

DescriptionFortran
Difficulty LevelLow
Root or Sudo PrivilegesYes (for installation)
OS CompatibilityUbuntu, Manjaro, Fedora, etc.
Prerequisites
Internet RequiredYes (for installation)

How to Install Fortran on Linux

To write and execute your first Fortran program on your Linux system, you need to install a GNU Fortran (gfortran) compiler.

As one of the oldest and most mature programming languages, it’s available in most Linux distribution repositories and easily installable via their default package manager.

Here are instructions for a few popular distributions:

$ sudo apt install gfortran                                                                        #For Ubuntu, Debian, Linux Mint
$ sudo dnf install gcc-gfortran                                                                #For RHEL, Fedora, AlmaLinux
$ sudo pacman -S gcc-fortran                                                                 #For Arch, Manjaro, EndeavourOS
$ sudo zypper install gcc-fortran                                                            #For OpenSUSE

After successfully completing the installation, run the following command to confirm its accessibility from your terminal:

$ gfortran --version

Output:

Confirming the gfortran is accessible from terminal by checking its version

If you get the same output as shown above, then congratulations, you’ve successfully installed it. You can now proceed to the next section to write and execute your first Fortran program on Linux.

Write and Execute a Basic Fortran Program on Linux

Let’s check out the basic process of writing and running a simple Fortran program with a practical example. In this example, we’ll create a program that calculates the sum of two numbers.

Step 1: Writing the Fortran Program

Create a text file with the “.f90” extension (e.g., “addition.f90“) and paste the following Fortran code into the file using your choice of text editor.

program Addition
  implicit none
  real :: num1, num2, result

  ! Prompt the user for input
  write(*,*) 'Enter the first number:'
  read(*,*) num1

  write(*,*) 'Enter the second number:'
  read(*,*) num2

  ! Calculate the sum
  result = num1 + num2

  ! Display the result
  write(*,*) 'The sum of', num1, 'and', num2, 'is', result

end program Addition

Save, and then close your file when you have finished!

Step 2: Compiling the Fortran Code

Open your terminal and navigate to the directory where you saved “addition.f90“. To compile the code using the “gfortran” compiler, run the following command:

$ gfortran -o addition addition.f90

When the above command finishes executing, it will create an executable file in the same directory with name “addition“, as shown.

Compiling the Fortran file

Step 3: Running the Fortran Program

Once the code is successfully compiled, you can execute the program by running the following command:

$ ./addition

The program will run, requesting input for two numbers. Once you provide the numbers, it will compute and show the sum.

Running Fortran program on Linux

Final Word

That’s all! You’ve successfully installed, written, compiled, and executed your first Fortran program on Linux. To enhance and extend the functionality of this basic program, a solid understanding of the Fortran programming language is required.

Since it’s the oldest, there are only a few high-quality resources available. For a beginner’s start, I suggest exploring Tutorials Point’s chapter that covers the basics of Fortran.

Apart from that, if you have any questions or queries related to the topic, then feel free to ask them in the comment section.

Till then, peace!

Join The Conversation

Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.