How to install gfortran on Mac OS X

Why did I install gfortran? Well, not to write Fortran programs. I tried installing SciPy using pip install scipy, and I got a message that a Fortran compiler was needed.

Install

This is how I installed gfortran on my Mac:

Visit hpc.sourceforge.net, and select a binary distribution for your version of Mac OS X, e.g. gfortran-snwleo-intel-bin.tar.gz for Snow Leopard.

Download the file and open the download directory in Terminal.

To install gfortran do the following:

gunzip gfortran-snwleo-intel-bin.tar.gz
tar xvf gfortran-snwleo-intel-bin.tar -C /

Compile a hello world

Let’s do a quick test that gfortran was installed.

gfortran --version

To try compiling a small testprogram, enter the following into a file called radius.for (btw, the whitespace matters):

      program circle
      real r, area
 
c This program reads a real number r and prints
c the area of a circle with radius r.
 
      write (*,*) 'Give radius r:'
      read  (*,*) r
      area = 3.14159*r*r
      write (*,*) 'Area = ', area
 
      stop
      end

Compile and run the program:

gfortran -o radius radius.for
./radius

Enter a number, e.g. 3, to see the area of a circle with this radius.

Uninstall

To uninstall again (warning, I haven’t tried it):

tar -tf gfortran-snwleo-intel-bin.tar | sort -r | (cd /; xargs -p -n 1 rm -d)

or do the following to delete without prompting:

tar -tf gfortran-snwleo-intel-bin.tar | sort -r | (cd /; xargs -t -n 1 rm -d)

Leave a Reply