How to install Fortran compiler

Fortran (previously FORTRAN- Formula Translation) is a general-purpose, imperative programming language that is especially suited to numeric computation and scientific computing, originally developed by IBM. It is one of the most popular languages in the area of high-performance computing and is the language used for programs that benchmark and rank the world’s fastest supercomputers.

In this tutorial I’ll try to discuss on how to install and use Fortran compiler on different operating systems like Mac, Linux or Windows. Better not to talk too much, let’s head towards our tutorial.

On OS X

Mac OS X
Well, in Apple Mac computers, the best solution for using Fortran is to use gfortran as there are up to date distributions. Here,  I’ll be doing all these staffs on my OS X Mountain Lion (10.8.4). But don’t worry, these steps will be more or less similar on other versions too.
1.  In order to use gfortran, you need to first download Xcode from App Store. You may find App Store on your dock or in the Launchpad. Now download and install Xcode. Open it, and go to Preferences    + , ] or [Xcode > Preferences]. Click on Downloads > Components, and then click to install Command Line Tools. After downloading it you’re set to use gfortran on your Mac.

2.  Now you need to download gfortran from HPC.  For simplicity, I would recommend only to install the relevant “gfortran only” distributions. Here I’m downloading gfortran-mlion.tar.gz (4.8).
3. After downloading it open up Terminal [Applications > Utilities > Terminal]. Change your directory to the folder where you’ve downloaded your gfortran zip file. Here, I’ve downloaded it on Downloads folder. So I’ve to type cd Downloads in the terminal window to change my directory. 
 
4. Now to install the binaries type  sudo tar -xvf gfortran-mlion.tar -C /  hit return. It’ll install everything in /usr/local. So now, gfortran is installed on your Mac.
5.  Now, you’ll need a text editor to write/edit your code. Here, I’m using TextEdit to write my code. Open it & write a fortran code here. Save it with the extension .f or .f90 or .f95 . .f for fortran 77, .f90 for fortran 90 and .f95 for fortran 95. I’m writing a fortran code on my TextEdit, giving it a name such as “ab.f95” and finally saving it on my Desktop.
6.  I’ll now have to change directory to my Desktop. Open up Terminal, type cd, cd Desktop and then type gfortran ab.f95. Now you’ll see there is a file named a.out on the desktop. This is the compiled object file. Open it with Terminal and write your input here as well as see the output.

Using IDE like Eclipse

Eclipse is a powerful IDE for programming. In order to use Fortran with eclipse you must install gfortran first.
1.  After properly installing gfortran you need to download Java JDK/JRE in your system in order to work with the Eclipse. Download it on your Mac & install it.
2.  Open up Terminal, type in javac and press enter. If you see a window like below then it means Java has been successfully installed in your system.

3.  After properly installing Java JDK/JRE, download Eclipse for Parallel Application Developers & extract it.
4.  On OS X 10.8(Mountain Lion), you will need to modify the Info.plist file for the Eclipse executable, and then re-register the Eclipse application with the Launch Service database.
–> In the Finder, locate the Eclipse application. Right-click on it, and click Show Package Contents.
–> Open the Contents folder.
–> Right-click on Info.plist, select Open With, and select Other. Choose TextEdit, and click OK. The Info.plist file will open in TextEdit.
–> Add an LSEnvironment dictionary below the line and above the CFBundleExecutable line. An example is shown below. Be sure to change the path appropriately for your system; this example adds /usr/local/bin/gfortran and /usr/local/bin to an otherwise-typical path. When you finish editing the file, save it, and quit TextEdit.

        LSEnvironment
               
                        PATH
                        /usr/local/bin/gfortran:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
               
        CFBundleExecutable
                eclipse

–> Now open the Terminal utility, and type the following command. Replace “/Applications/eclipse/Eclipse.app” with the correct path to the Eclipse application on your system.

/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister
    -v -f
    /Application/eclipse/Eclipse.app

5.  Open up Eclipse,  choose a Workspace for saving your files and click ok. On Eclipse toolbar go to Windows > Open Perspective > Others. Now check whether Fortran is there or not.

[ If it is not go to help > Install new software. Click add. A new window will be opened. Now type ‘juno’ in the Name and http://download.eclipse.org/tools/ptp/updates/juno in the Location field and click OK. Now click on Fortran Development Tools (Photran), under it check ‘Fortran Development Tools’ & ‘Rephraser Engine End-User Runtime’, click next. Accept the license agreements and click finish. 
Your plugins will be installed]
6.  On Eclipse toolbar goto File > New > Project > Fortran > Fortran Project, click Next.
Type in your project name[here I’m naming it ‘abcd’] and select ‘Executable (GNU Fortran on Windows)’ from the Project type. Click Next > Finish.
7.  Now a new project will be created. From the left pane right click abcd, select New > Fortran Source File. A new window’ll be opened, on the Source file I’m naming it ‘trial.f95’, click finish.

8.  A new window will be opened. Here, edit the source code as you like. Now click Debug from the toolbar. After finishing debugging (or compiling) click Run > Run As > 2. Local Fortran Application. Select a configuration (gdb). Your program will run and you’ll see your output right below your source code

By now I think you’ve got how to use Fortran on Mac.

Uninstalling gfortran– HPC distributes gfortran as a tar-file. Therefore each file contained in the tar-file should be removed. The easiest way to do this is probably to run the command:

For sh/bash:
for file in $(tar tfz [tar-file]); do sudo rm -f /$file; done

For csh/tcsh:
foreach file ( `tar tfz [tar-file]` )
sudo rm -f /$file
end

where [tar-file] is the package, for example- gfortran-mlion.tar  [that i’ve used here] etc. This will remove all files but not directories, which may contain other files that are not to be removed. To remove empty directories only run this command:

For sh/bash:
for file in $(tar tfz [tar-file]); do sudo rmdir -p /$file; done

For csh/tcsh:
foreach file ( `tar tfz [tar-file]` )
sudo rmdir -p /$file
end

On Linux

I’m using Ubuntu 12.04 LTS for this tutorial. But the procedure is more or less similar for other Linux Distros too.

Using Terminal

1.  First open up your terminal and type sudo apt-get install gfortran and hit enter (you can also install it from Ubuntu Software Center). It’ll probably gonna ask you for the password. Enter your password and gfortran compiler will be installed in your system.
2.  Now open a text editor to write/edit your Fortran code. Here, I’m using gedit for this purpose. You may use other editors also.
3.  Now before you proceed to write any program save it with the extension .f or .f90 or .f95 [.f for Fortran 77, .f90 for Fortran 90 and .f95 for Fortran 95]. I’m here giving the file name ‘ex1.f90’ [that means I’m writing this code in Fortran 90] and saving this file on my Desktop.
4.  Now let’s write a simple program, such as-
print*, ‘Hello World!’
end
and save it
5.  Now again head back to your Terminal window to compile and run your program.
I’ve saved my file on Desktop. So I need to change my directory to Desktop. In order to do that type cd Desktop and you’ll see that your directory has been changed to Desktop.
6.  Now to compile our file ‘ex1.f90’ type gfortran ex1.f90 and press enter. Nothing will happen in the Terminal window but you’ll see a executable file named ‘a.out’ in the Desktop. To execute this file type ./a.out in the terminal
 and press enter. You’ll now see the output of our program. And it is Hello World!
7.  These are the steps to use Fortran via Terminal on Linux. Now if you want to rename the output file just type mv a.out ab and look on the Desktop. You’ll now see the output file is named ‘ab’. To execute it type ./ab
You can also do this whole compiling and name changing thing in one line. Type gfortran ex1.f90 -o abc & you’ll get a executable file named ‘abc’ on your Desktop. Running this program is same as mentioned above, just type ./abc and see the output

Using IDE like NetBeans-

If you’re not so much comfortable with this Terminal command line thing, then there is a good solution for you. It’s NetBeans, a IDE that has a wonderful user friendly GUI. Let’s talk a bit about it.
1.  Install gfortran and NetBeans from Ubuntu Software Center.
You can also download NetBeans from its official site (Download the package All). It’ll be a .sh file. In order to execute that file open Terminal and type sh [file name] . Replace [file name] with the actual file name.
2.  After installing both gfortran and NetBeans respectively, open up NetBeans. First go to Tools > Plugins > Installed. Now click on C/C++ and activate it (If you don’t see C/C++ here just go to the Available Plugins and install it from there)
3.  Now again go to Tools > Options and click on the C/C++ section. Here if you see the Fortran Compiler field is empty, click on the button right on it and browse for the gfortran file and select it.
There is a another lot easier way to do this. Open Terminal and type whereis gfortran and it’ll show you the specified path. Copy it from there and paste it in the Fortran Compiler field. Click OK and you’re all set.
4.  Goto File > New Project. Click on the Samples and in Samples click on C/C++ and in the Projects select the Fortran Hello World Application.  Click Next, give a name in the Project Name field. Here I’m giving the name ‘examples’ and click finish.
5.   Now on the left pane, you’ll see Projects. And under it you’ll see examples > Header Files, Resource Files, Source Files, Important Files. Do not touch the Header Files, Resource Files and Important Files. Click on the Source File, under it you’ll find two individual example files like bar.f, foo.f  to show you some demo. We won’t need these files, so delete it.
Now right click on the Source Files and select New > Fortran File (Free Format). Give a name such as ‘ex2’ and select the extension f90.
6. A file will be opened with a simple hello world program written on it. Click the run button from the tool bar [or press F6] and see the output below.
Now write your own program here and execute it.
These are the ways of using Fortran on Linux.

On Windows

In Microsoft Windows computers using Fortran is a little bit complicated. But don’t be afraid. I’ll discuss the whole process here elaborately. As the above methods here I’ll be using the gfortran compiler for Windows too. Now let’s get to our tutorial.

Install Cygwin

In order to use gfortran we need to install Cygwin as it has all the GNU distributed compiler itself.
1.  Download Cygwin, choose the setup.exe package according to the version of your Windows. (x86 for 32bit Windows & x64 for 64bit Windows). Once it’s downloaded, run & install the application (I would recommend to use the mirror site- http://cygwin.mirrors.hoobly.com when you’re prompted to download the actual package.)
2.  After proper installation a new window ( Cygwin pakage setup window ) will be opened. Here find ‘Devel Default’, and under it find ‘gcc-fortran:GNU Compiler Collection(Fortran)’, select it. Now go for the next one ‘gdb: The GNU Debugger’, select it. And for the final one find ‘make: The GNU version of the make utility’ and select it.

Now finally click Next > Next and install it.
3.  Now you’ve to set the environment variables.
The variables for Cygwin is:
;C:Cygwinbin;C:Cygwinusrbin;C:Cygw­inusrlocalbin;C:Cygwinlib;C:Cygwin­usrlib     
[Note that this will work only when you installed Cygwin in the C: drive]
Copy it and right click on your computer, click properties. Select Advanced system settings, click Environment Variables. Now under ‘System variables’ find ‘path’ and click Edit [this is very very important- don’t click anywhere else or don’t click delete or backspace. If you do you’ve to reinstall your OS.]
Click on the Variable value [to deselect it, so that you don’t do anything stupid], go to the end of it and paste the above variable right at the end of the previous variable. Finally click ok.

Your Cygwin is ready to use.

Using Command line

Cygwin works like the Terminal of Mac or Linux. So we’ve to use the UNIX commands here to work with the Cygwin.
1.  Open Cygwin Terminal shell from Start menu.
2.   Now we need a text editor to write/edit our code. Here I’ll be using Notepad++ for this purpose. Open it, write your fortran code here and save it with a Fortran extension (.f or .f90 or .f95). For example I’m naming it ‘helloworld.f95’ and saving (Ctrl+Alt+S) it on my Desktop.

3.  Now I’ve to change my directory to Desktop using Cygwin. to do that use the cd command. Here the path address for my Desktop is C:UsersUserDesktop. So for me the command will be- cd C:/Users/User/Desktop (Though Windows use backslash ”, we’ll have to use forward slash ‘/’ as we’re writing UNIX commands here). Type ls to see the files on Desktop. You’ll see the ‘helloworld.f95’ file there.
4.  To execute your code type gfortran helloworld.f95 , this will execute your code and will save executable object file named ‘a.exe’ on your Desktop. To run it type ./a , you’ll see your output below.

5.  These are the steps to use Fortran via Cygwin. Now if you want to rename the output file just type mv a.exe abc and look on the Desktop. You’ll now see the output file is named ‘ab’. To execute it type ./abc
You can also do this whole compiling and name changing thing in one line. Type gfortran helloworld.f95 -o abcd & you’ll get a executable file named ‘abc’ on your Desktop. Running this program is same as mentioned above, just type ./abcd and see the output

 

Using IDE like Eclipse

1. If you prefer to have a GUI, then there is a wonderful IDE named Eclipse. After properly installing Cygwin you need to download Java JDK/JRE in your system in order to work with the Eclipse. Download it according to the version of your Windows & install it.

2. Now go to start menu, type in cmd and open up Command Prompt
[for windows 8 users, press Win key + R to open up Run. Now type cmd here and enter ]
In the Command Promt type in javac and press enter. If you see a window like below then it means Java has been successfully installed in your system. Now go to step 3.
[ Unfortunately if your Java do not work properly, it’ll show you an error then. So you need to setup your environment path of the JRE or JDK. To do that open up Computer goto C drive(or where you’ve installed your Windows OS). Now goto Program Files >  Java > JDK > bin. Right click on any of the files inside bin and click properties. Copy the Location path from there.
Now again goto Computer and right click and select properties. From the left pane click Advanced system settings > Environment variables. Now under the ‘user variables’ click on New. Type ‘path’ in the Variable name and paste that root address in the Variable value field.
 Now your Java will be working perfectly. to check it type javac in the cmd. ]
3.  After properly installing Java JDK/JRE, download Eclipse for Parallel Application Developers according to the version of your Windows & extract it. I’ve extracted it on my Program Files. You can also create a shortcut of it on your Desktop for an easy access.
4.  Open up Eclipse,  choose a Workspace for saving your files and click ok. On Eclipse toolbar go to Windows > Open Perspective > Others. Now check whether Fortran is there or not.
[ If it is not go to help > Install new software. Click add. A new window will be opened. Now type ‘juno’ in the Name and http://download.eclipse.org/tools/ptp/updates/juno in the Location field and click OK. Now click on Fortran Development Tools (Photran), under it check ‘Fortran Development Tools’ & ‘Rephraser Engine End-User Runtime’, click next. Accept the license agreements and click finish.

Your plugins will be installed. ]

5.  On Eclipse toolbar goto File > New > Project > Fortran > Fortran Project, click Next.
Type in your project name[here I’m naming it ‘abcd’] and select ‘Executable (GNU Fortran on Windows)’ from the Project type. Click Next > Finish.
6.  Now a new project will be created. From the left pane right click abcd, select New > Fortran Source File. A new window’ll be opened, on the Source file I’m naming it ‘example.f90’, click finish.
7.  A new window will be opened like below-

Here, edit the source code as you like. For example I’m writing here a simple program to print out Hello World-
PRINT*, ‘Hello World’
END
Now click Debug from the toolbar. After finishing debugging (or compiling) click Run > Run As > 2. Local Fortran Application.

Now select the configuration MinGW gdb and click ok. Your program will run and you’ll see your output right below your source code


Using IDE like NetBeans

NetBeans is a much more advanced IDE than Eclipse having a better GUI.
1.  After installing Cygwin, download NetBeans (download the all pacakage), install it.
2.  Open it, go to (on the toolbar) Tools > Plugins > Installed. Now click on C/C++ and activate it (If you don’t see C/C++ here just go to the Available Plugins and install it from there)

3.  Now again go to Tools > Options and click on the C/C++ section. Here if you see the Fortran Compiler field is empty, click on the button right on it and browse for the gfortran file and select it. There is a another lot easier way to do this. Open Command Prompt and type where gfortran and it’ll show you the specified path. Copy it from there and paste it in the Fortran Compiler field. Click OK and you’re all set.

4.  Go to File > New Project. Click on the Samples and in Samples click on C/C++ and in the Projects select the Fortran Hello World Application.  Click Next, give a name in the Project Name field. Here I’m giving the name ‘example’ and click finish.
5. Now on the left pane, you’ll see Projects. And under it you’ll see example -> Header Files, Resource Files, Source Files, Important Files. Do not touch the Header Files, Resource Files and Important Files. Click on the Source File, under it you’ll find two individual example files like bar.f, foo.f  to show you some demo. We won’t need these files, so delete it.
Now right click on the Source Files and select New > Fortran File (Free Format). Give a name such as ‘abcd’ and select the extension f90.

6.  A file will be opened with a simple hello world program written on it. Click the run button from the tool bar [or press F6] and see the output right below the source code.

Now write your own program here and execute it.

Using IDE like Code::Blocks-

check this video-

Troubleshooting

1. If you don’t know what type of Windows (32bit or 64bit) you’re using, just right click on Computer, select Properties. Here you’ll find your system type. Note that x86 applications are for 32bit Windows while x64 is stands for 64bit Windows.
2. You may face problems while starting up Eclipse like this-

Well, this not a very critical problem to solve. Find the eclipse.ini file in your executed folder. Try to modify it according to your configuration.
Try these steps one by one-
–> Adding the full path to javaw.exe –vm C:Programsjdk1.6bin\binjavaw.exe
–> Removing the –vm option all together
–> Removing –launcher.XXMaxPermSize
–> Changing the value 512m of –launcher.XXMaxPermSize ( So that your JVM can allocate the required memory space )
–> Reducing -Xmx to 512m
–> Reducing -Xms to 512m
–> Changing the version of Java -Dosgi.requiredJavaVersion=1.5 (change this 1.5 according to the version of Java installed on your system. To determine the version type javac -version in your cmd and it’ll show the installed version.)

If you encounter “Java was started but returned exit code=1” error while starting the eclipse, modify the -vm argument to point to jvm.dll. That is-
-vm
C:Program FilesJavajre7binclientjvm.dll
Also note that the -vm option must occur before the -vmargs option, since everything after -vmargs is passed directly to the JVM

Last but not the least

There is another portable shortcut solution if you don’t like these installation procedures. Go to  Ideone and perform your programming online
By this our Fortran installation tutorial ends here. Always remember the Fortran extensions, that is .f for Fortran77, .f90 for Fortran90, .f95 for Fortran95, .f03 for Fortran03 and .f08 for Fortran08. I’ve already tested this whole tutorial in seven individual computers running OS-Apple  Mac OS X 10.7 (Lion), OS X 10.8 (Mountaion Lion), Linux Ubuntu, Linux Mint, Microsoft Windows 7 (32bit & 64bit) & Windows 8 (64bit). I hope there won’t be any trouble while following this tutorial.
If you’re new to Fortran, then here are some books & tutorials for you to start with.

Below is a presentation produced by Dr. C.-K. Shene (Professor,  Michigan Technological University ). I downloaded these slides from his website

I tried to make it as simple as possible. But if you encounter any difficulty with the above steps feel free to ask it here. I’ll try to answer your problems within a day or two. So, Happy Programming!

“I hear and I forget. I see and I remember. I do and I understand.”
– Confucius
Chinese philosopher & reformer (551 BC – 479 BC)