How to install Gfortran 9, 10 or 11 on Ubuntu 20.04 LTS

Let’s see the commands that we can use to install Gfortran 5/6/7/8/9/10/11 versions on Ubuntu 18.04/20.04/21.04, Linux Mint, Debian, and other similar systems. 

FORTRAN is the abbreviation of Formula Translation. This high-level language was designed for science, engineering problems, or those problems in enterprise management that can be expressed by mathematical formulas, and its numerical calculation function is strong.

It was proposed in 1954 and officially used in 1956. It has a history of sixty years until 2021, but it is still enduring. It has always been the main language used in the field of numerical computing. However, it has also been an object-oriented programming language since 2003, which is used in particular for numerical calculations in science, technology, and research.

The GNU version of Fortran is known as GFortran, a GNU Fortran compiler, which is part of the free GNU Compiler Collection (GCC) for Fortran 95/2003/2008/2018. Learn more about it on Wikipedia.

Here we will learn the steps to install GFortran’s latest version on Ubuntu 20.04 LTS Focal fossa to start learning this programming language.

Steps to install GFortran latest version on Ubuntu 20.04 Linux

Run system update

Make sure you have an updated system and for that run the below-given system update command that will also rebuild the repository cache.

sudo apt update

 

Install GFrotan 5/6/7/8/9/10/11 on Ubuntu 20.04

The default base repository of Ubuntu 20.04 LTS offers 5 to 10 versions of it, so if you want to install any of them, can use the appropriate command given below:

GFrotran 5

sudo apt install gfortran-5

For Version 6

sudo apt install gfortran-6

For Version 7

sudo apt install gfortran-7

For Version 8

sudo apt install gfortran-8

For Version 9

sudo apt install gfortran-9

For Version 10

sudo apt install gfortran-10

For Gfortran Version 11 and other latest one in development:

However, currently while writing the article Gfortran version 11 was not available in the default repository of Ubuntu 20.04. Hence, if you want that then can add a PPA.

sudo add-apt-repository ppa:ubuntu-toolchain-r/test

Add GFortran PPA repository on Ubuntu 20.04

Install Gfortran 11 on Ubuntu

sudo apt install gfortran-11

Install gfortran 11 on Ubuntu 20.04 Linux

To check the installed version:

gfortran --version

or

gfortran:11 --version

You can replace the 11 with the version in the above command you are using.

Code Example

Depending upon the Fortran code, save the code file with the appropriate extension. For example: If the source code is in  FORTRAN 77 the code file will be saved with  .f (or the less common .for) extension. In the same way for Fortran 90 .f90 the extension will be used and for Fortran 95.f95

Let’s take an example of  Fortran 77 code 

Using the below-given code we will ask a user to enter two values A and B after that system will automatically find the find greatest common divisor for both the number using the Euclidean algorithm.

nano test.f

Copy-paste the below-given code:

 PROGRAM EUCLID
        PRINT *, 'A?'
        READ *, NA
        IF (NA.LE.0) THEN
          PRINT *, 'A must be a positive integer.'
          STOP
        END IF
        PRINT *, 'B?'
        READ *, NB
        IF (NB.LE.0) THEN
          PRINT *, 'B must be a positive integer.'
          STOP
        END IF
        PRINT *, 'The GCD of', NA, ' and', NB, ' is', NGCD(NA, NB), '.'
        STOP
      END

      FUNCTION NGCD(NA, NB)
        IA = NA
        IB = NB
    1   IF (IB.NE.0) THEN
          ITEMP = IA
          IA = IB
          IB = MOD(ITEMP, IB)
          GOTO 1
        END IF
        NGCD = IA
        RETURN
      END

Compile the saved Fortran code

Here we are using version 11:

gofortran-11 test.f -o youapp.out

Note: you can change the youapp with whatever name you want to assign your output file:

Now, run the compiled program:

./youapp.out

Compile gnu fortan code

 

In this way, we can install and start compiling our Fortran codes on Ubuntu Linux operating systems. Learn more about this language at its official wiki page.

Other Articles:

 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.