Yesterday, I was helping a physics graduate student install gfortran on his MacBook running on Sonoma. The installation of gfortran is easy. One can use homebrew (I recommend it) to install gfortran. If you don’t have homebrew already installed on your computer, you can follow the installation instruction here. Once homebrew is installed, all you have to do is to open an Apple terminal and run the command:
brew install gcc
Note that gfortran is included in gcc. You can also install gfortran using MacPorts. If you don’t have MacPorts installed, it’s good to have one and you can follow the instruction here. Once MacPorts is installed, in an Apple terminal, run the command:
sudo port gcc12 +gfortran && sudo port select --set gcc mp-gcc12
When the student was testing it by attempting to compile a simple fortran program, there was an error:
ld: library not found for -lSystem
collect2: error: ld returned 1 exit status
This happened when the system library is not in the search paths of gcc. To fix this issue, first open the file
sudo nano /private/etc/zprofile
and add the following two lines at the end:
if [ -z "${LIBRARY_PATH}" ]; then
export LIBRARY_PATH="/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib"
else
export LIBRARY_PATH="$LIBRARY_PATH:/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib"
fi
and save the file by pressing ctrl+x and close the terminal. Open a new Apple terminal and test it again. It will work flawlessly this time unless there is another issue.
Sources:
I installed homebrew on my Mac (Sonoma 14.5) like you said. And then used it to install gcc like you said. But I am getting this compiler error:
gfortran: fatal error: cannot execute ‘f951’: execvp: No such file or directory
compilation terminated.
Marc,
I am not familiar with the error but by Google search I found a relevant page at stackoverflow. It looks like a simple fix.
I’ve been trying to install Gfortran on my MacBook Pro with an Apple M3 chip, macOS Sonoma v. 14.6.1, built in October 2023.
I’ve followed your instructions and been able to install Xcode CLT, and to get the downloads for both home-brew and macports. However, when I use the commands to install them, the computer bounces me out everytime.
I don’t know what you mean by “the computer bounces me out”. What does the error message say for each case?