Installing gfortran on macOS Sonoma

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:

  1. Installing gfortran via macports on Mac OS high sierra

  2. GFortran compiler error on Mac OS Big Sur

This entry was posted in gfortran, macOS. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *