Cyberithub

How to Install GFortran 7/8/9/10/11 on Ubuntu 20.04 LTS (Focal Fossa)

Advertisements

In this article, I will take you through the steps to install Gfortran 7/8/9/10/11 on Ubuntu 20.04 LTS. GFortran is a free and open source compiler of Fortran programming language. It is a part of GCC, the GNU Compiler Collection. The GFortran compiler is fully compliant with the Fortran 95 Standard and includes legacy F77 support. In addition, it includes a significant number of Fortran 2003 and Fortran 2008 features. It can be easily installed in almost any operating systems including Linux. Here we will see the steps to install different gfortran versions on Ubuntu 20.04 LTS System. More about Gfortran on official website.

How to Install GFortran 7/8/9/10/11 on Ubuntu 20.04 LTS (Focal Fossa)

How to Install GFortran 7/8/9/10/11 on Ubuntu 20.04 LTS (Focal Fossa)

Also Read: How to Install libtbb-dev package on Ubuntu 20.04 LTS (Focal Fossa)

Step 1: Prerequisites

a) You should have a running Ubuntu 20.04 LTS Server.

b) You should have sudo or root access to run privileged commands.

c) You should have apt or apt-get and add-apt-repository utility available in your System.

 

Step 2: Update Your Server

In the first step, you need to sync the System packages with the latest versions available from the Ubuntu repo using sudo apt update command as shown below.

cyberithub@ubuntu:~$ sudo apt update
Hit:1 https://download.docker.com/linux/ubuntu focal InRelease
Hit:2 https://dl.google.com/linux/chrome/deb stable InRelease
Hit:3 http://in.archive.ubuntu.com/ubuntu focal InRelease
Hit:4 http://security.ubuntu.com/ubuntu focal-security InRelease
Hit:5 http://in.archive.ubuntu.com/ubuntu focal-updates InRelease
Hit:6 http://in.archive.ubuntu.com/ubuntu focal-backports InRelease
Reading package lists... Done
Building dependency tree
Reading state information... Done

 

Step 3: Install GFortran

In the next step, you can download and install the current available gfortran package from default Ubuntu repo by using sudo apt install gfortran command as shown below. This will download and install the package along with all its dependencies.

cyberithub@ubuntu:~$ sudo apt install gfortran
[sudo] password for cyberithub:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
gfortran-9 libgfortran-9-dev libgfortran5
Suggested packages:
gfortran-multilib gfortran-doc gfortran-9-multilib gfortran-9-doc libcoarrays-dev
The following NEW packages will be installed:
gfortran gfortran-9 libgfortran-9-dev libgfortran5
0 upgraded, 4 newly installed, 0 to remove and 46 not upgraded.
Need to get 9,359 kB of archives.
After this operation, 36.1 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y
............................................

You also have the option to choose different Gfortran versions for the installation. As of now, you can choose to install from version 7 to 11.

For version 7

To install version 7 from default Ubuntu repo, you need to use sudo apt install gfortran-7 command as shown below.

cyberithub@ubuntu:~$ sudo apt install gfortran-7
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
gfortran-9 libgfortran-9-dev libgfortran5
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
cpp-7 gcc-7 gcc-7-base gcc-8-base libasan4 libcilkrts5 libgcc-7-dev libgfortran-7-dev libgfortran4 libmpx2 libubsan0
Suggested packages:
gcc-7-locales gcc-7-multilib gcc-7-doc gfortran-7-multilib gfortran-7-doc libcoarrays-dev
The following NEW packages will be installed:
cpp-7 gcc-7 gcc-7-base gcc-8-base gfortran-7 libasan4 libcilkrts5 libgcc-7-dev libgfortran-7-dev libgfortran4 libmpx2 libubsan0
0 upgraded, 12 newly installed, 0 to remove and 46 not upgraded.
Need to get 30.9 MB of archives.
After this operation, 99.7 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y
.......................................

For version 8

Similarly, version 8 can be installed by using sudo apt install gfortran-8 command as shown below.

cyberithub@ubuntu:~$ sudo apt install gfortran-8
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
gfortran-9 libgfortran-7-dev libgfortran-9-dev libgfortran4
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
cpp-8 gcc-8 libgcc-8-dev libgfortran-8-dev
Suggested packages:
gcc-8-locales gcc-8-multilib gcc-8-doc gfortran-8-multilib gfortran-8-doc libcoarrays-dev
The following NEW packages will be installed:
cpp-8 gcc-8 gfortran-8 libgcc-8-dev libgfortran-8-dev
0 upgraded, 5 newly installed, 0 to remove and 46 not upgraded.
Need to get 31.1 MB of archives.
After this operation, 102 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y
.......................................

For version 9

If you are looking to install version-9 then you need to use sudo apt install gfortran-9 command as shown below.

cyberithub@ubuntu:~$ sudo apt install gfortran-9
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
libgfortran-7-dev libgfortran-8-dev libgfortran4
Use 'sudo apt autoremove' to remove them.
Suggested packages:
gfortran-9-multilib gfortran-9-doc libcoarrays-dev
The following NEW packages will be installed:
gfortran-9
0 upgraded, 1 newly installed, 0 to remove and 46 not upgraded.
Need to get 7,936 kB of archives.
After this operation, 27.9 MB of additional disk space will be used.
.............................................

For version 10

Likewise, to install version 10 you need to use sudo apt install gfortran-10 command as shown below.

cyberithub@ubuntu:~$ sudo apt install gfortran-10
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
libgfortran-7-dev libgfortran-8-dev libgfortran-9-dev libgfortran4
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
cpp-10 gcc-10 libasan6 libgcc-10-dev libgfortran-10-dev
Suggested packages:
gcc-10-locales gcc-10-multilib gcc-10-doc gfortran-10-multilib gfortran-10-doc libcoarrays-dev
The following NEW packages will be installed:
cpp-10 gcc-10 gfortran-10 libasan6 libgcc-10-dev libgfortran-10-dev
0 upgraded, 6 newly installed, 0 to remove and 46 not upgraded.
Need to get 40.8 MB of archives.
After this operation, 138 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y
......................................

For Latest Version(version 11)

The latest gfortran version 11 is not available through default Ubuntu repo. For that you need to add below repository using add-apt-repository command.

cyberithub@ubuntu:~$ sudo add-apt-repository ppa:ubuntu-toolchain-r/test
Toolchain test builds; see https://wiki.ubuntu.com/ToolChain

More info: https://launchpad.net/~ubuntu-toolchain-r/+archive/ubuntu/test
Press [ENTER] to continue or Ctrl-c to cancel adding it.

Hit:1 https://download.docker.com/linux/ubuntu focal InRelease
Hit:2 https://dl.google.com/linux/chrome/deb stable InRelease
Hit:3 http://in.archive.ubuntu.com/ubuntu focal InRelease
Hit:4 http://security.ubuntu.com/ubuntu focal-security InRelease
Hit:5 http://in.archive.ubuntu.com/ubuntu focal-updates InRelease
Get:6 http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu focal InRelease [17.5 kB]
Hit:7 http://in.archive.ubuntu.com/ubuntu focal-backports InRelease
Get:8 http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu focal/main i386 Packages [15.2 kB]
Get:9 http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu focal/main amd64 Packages [17.0 kB]
Get:10 http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu focal/main Translation-en [7,444 B]
Fetched 57.1 kB in 3s (20.8 kB/s)
Reading package lists... Done

After adding the repository, you need to update the package cache using sudo apt update command as shown below.

cyberithub@ubuntu:~$ sudo apt update
Hit:1 https://download.docker.com/linux/ubuntu focal InRelease
Hit:2 https://dl.google.com/linux/chrome/deb stable InRelease
Hit:3 http://security.ubuntu.com/ubuntu focal-security InRelease
Hit:4 http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu focal InRelease
Hit:5 http://in.archive.ubuntu.com/ubuntu focal InRelease
Hit:6 http://in.archive.ubuntu.com/ubuntu focal-updates InRelease
Hit:7 http://in.archive.ubuntu.com/ubuntu focal-backports InRelease
Reading package lists... Done
Building dependency tree
Reading state information... Done

Now you can download and install the latest gfortran-11 package from repo using sudo apt install gfortran-11 command as shown below.

cyberithub@ubuntu:~$ sudo apt install gfortran-11
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
libgfortran-10-dev libgfortran-7-dev libgfortran-8-dev libgfortran-9-dev libgfortran4
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
cpp-11 gcc-11 gcc-11-base libasan6 libatomic1 libcc1-0 libgcc-11-dev libgcc-s1 libgfortran-11-dev libgfortran5 libgomp1 libitm1 liblsan0 libquadmath0
libtsan0 libubsan1
Suggested packages:
gcc-11-locales gcc-11-multilib gcc-11-doc gfortran-11-multilib gfortran-11-doc libcoarrays-dev
The following NEW packages will be installed:
cpp-11 gcc-11 gcc-11-base gfortran-11 libgcc-11-dev libgfortran-11-dev
The following packages will be upgraded:
libasan6 libatomic1 libcc1-0 libgcc-s1 libgfortran5 libgomp1 libitm1 liblsan0 libquadmath0 libtsan0 libubsan1
11 upgraded, 6 newly installed, 0 to remove and 53 not upgraded.
Need to get 48.2 MB of archives.
After this operation, 131 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y
.............................................

 

Step 4: Check Version

You can verify the gfortran installation by checking the current installed version using gfortran --version command as shown below.

cyberithub@ubuntu:~$ gfortran --version
GNU Fortran (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0
Copyright (C) 2019 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.

 

Step 5: Write Your First Program

Once gfortran installed successfully, it is time to test the compiler by writing a simple Fortran program. You can create a file example.f90 with below contents in it. This program will basically display "This is an example program" on the output.

cyberithub@ubuntu:~$ nano example.f90
program example
  print *, 'This is an example program!'
end program example

After saving the above file, you can compile the program using gfortran example.f90 -o example command as shown below.

cyberithub@ubuntu:~$ gfortran example.f90 -o example

Once the compilation is successful, you can run the program by using ./example as shown below.

cyberithub@ubuntu:~$ ./example
This is an example program!

1 thought on “How to Install GFortran 7/8/9/10/11 on Ubuntu 20.04 LTS (Focal Fossa)”

  1. Currently I am having a problem in installing gfortran-9 in the ubuntu 20.04 enviroment using previous sudo commands.

    Reply

Leave a Comment