How to install Fortran on Ubuntu

Endrit Qerreti

Endrit Qerreti

Fortran is the oldest programming language, and is still one of the most used programming languages.

Fortran is mainly used for scientific purposes.

Fortran is available as package on Ubuntu repositories, which allows you to install Fortran on your machine directly from your terminal, without having to add an external repository.

In this tutorial, you will learn how to install, uninstall and how to create a simple app in Fortran in Ubuntu.

Install Fortran on Ubuntu

To install Fortran on Ubuntu, simply run the command below

sudo apt install gfortran

Verify Fortran installation

To verify if you can run Fortran on your terminal, you can check the version of Fortran by running the command below

gfortran --version

Use Fortran

You can now use Fortran to compile Fortran projects on your machine. To compile a Fortran project, you can use the syntax below

gfortran fortran-project.f90

Where gfortran is the compiler, and fortran-project.f90 is the name of the file that contains Fortran code.

Build a Fortran app

Creating a Fortran app is very easy. First you write the code into a file, then you compile this file, and then you run it. Follow the steps below to learn how to do this.

1) Create the fortran file

sudo nano fortran-project.f90

2) Write the fortran code

In this example we writing a simple Hello world app in fortran. So when you run it, it will simply print Hello World.

Simply copy and paste the code below into the fortran file that you created on the first step, and then make sure to save it.

program hello
  print *, 'Hello, World!'
end program hello

3) Compile the app

To compile a fortran file, run the following command in your terminal

gfortran -o helloworld fortran-project.f90

The command above will compile your app into a file called helloworld So if you take a look at files via the ls command, you will see a new file helloworld on your machine, this is your compiled fortran app.

4) Run Fortran app

Once the Fortran file has been compiled, you can proceed to run it

./helloworld

And you will see the output of the app on your terminal

Output

Hello, World!

Uninstall Fortran on Ubuntu

To uninstall Fortran on Ubuntu, you can use the default package manager with the remove and purge commands.

sudo apt remove gfortran --purge

Conclusion

In this tutorial, you learned how to Install, Uninstall Fortran language on Ubuntu, and how to create a simple Fortran app, and also how to run it.

Now that you have the fortran compiler on your machine, you should be able to run fortran code on your machine via the terminal.