I have tried to compile a simple helloworld.f95 on my Mac, using emacs. The root of emacs was set in the directory containing helloworld.f95. I used the following commands

M-x compile
gfortran -o helloworld helloworld.f95

I got the following error:

-*- mode: compilation; default-directory: "~/Desktop/fortran/" -*-
Compilation started at Wed Sep  4 17:37:51

gfortran -o helloworld helloworld.f95
/bin/bash: gfortran: command not found

Compilation exited abnormally with code 127 at Wed Sep  4 17:37:51

I have previously been able to compile with the same command on the terminal. Fortran was installed on my mac by following the instructions on this link: http://www.lapk.org/gfortran/gfortran.php?OS=7

The location in which gfortran is installed is at:

Desktop$ which gfortran

How do I compile .f95 files from emacs, as well as run them?

  • 1
    Possibly the PATH is not set in the shell launched by emacs. A workaround (before to clear things up) could be to provide the full pathname in the command. /usr/local/bin/gfortran -o ...
    – JeanPierre
    Commented Sep 4, 2019 at 12:05
  • This worked, thank you!
    – Tian
    Commented Sep 4, 2019 at 13:32
  • 2
    It's a defect of macOS that it only sets PATH correctly for terminals, not for GUI applications. Commented Sep 4, 2019 at 22:17
  • 2
    OSX users often recommend using github.com/purcell/exec-path-from-shell
    – phils
    Commented Sep 4, 2019 at 22:23

1 Answer 1


As mentioned by @JeanPierre , the problem was because the location of gfortran was not added to the PATH environment variable for the emacs shell. Thus the shell did not know where to look to find the command. To do so (and to make the PATH environment readily available for editing) I added this code block into my initialisation file: ~/.emacs.d/init.el

;; defining "PATH"
(setenv "PATH" (mapconcat identity

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.