Help installing gfortran on MacBook Pro with M1pro

I just got a MacBook Pro with the M1 pro chip. Now I’m trying to install gfortran.

First I installed Xcode from the App Store on my Mac. During the installation it did not ask me anything except whether I wanted to install Rosetta, which I did. I’m not sure if Command Line Tools are installed, but when I type
xcode-select -p
it returns
/Applications/Xcode.app/Contents/Developer

Next I went to http://hpc.sourceforge.net. and clicked on gfortran-11.2-bin.tar.gz (gfortran only). I then went to the Downloads directory on my Mac and typed
sudo tar -xvf gfortran-11.2-bin.tar -C /.

There appeared to be one error at the beginning of the install,
x usr/local/: Can’t restore time
but the rest of the install looked fine and it ended with
x usr/local/bin/gfortran
tar: Error exit delayed from previous errors.

I tried to compile a test program but it failed,
gfortran -o mytest.x mytest.f90
ld: library not found for -lSystem
collect2: error: ld returned 1 exit status

I tried re-installing gfortran from Downloads (this time without the period at the end):
sudo tar -xvf gfortran-11.2-bin.tar -C /

The problem persists. I googled this and found several suggestions but some seem outdated and I am not sure how to proceed. Any suggestions on how to fix this problem and get gfortran running on my M1pro?

I recommend installing gfortran via Homebrew. After installing Homebrew, execute the following command in a terminal window:

brew install gcc

The last time I tried this, it installed version 11.3.0.

1 Like

Note conda is also an option:

conda install -c conda-forge gfortran

2 Likes

Thanks for the suggestions about brew and conda. I expect I’ll install one of these eventually. But in the meantime I have searched a bit and discovered that I do indeed have Command Line Tools. I read that it has been automatically installed ever since Xcode 6.1 (and yesterday I installed 13.3.1). You can see it by going to Xcode - Preferences - Locations, at which point you can see if/where Command Line Tools are installed.

So my problem is not related to Command Line Tools…

I’ve had same problem with Intel ifort compiler. Adding

-L/Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk/usr/lib/

option to every compiler invocation has fixed the problem. Surely your path to System library might be different.

My setup looks different and I can’t figure out what to point to it with -L . If I look inside /Library/Developer/, the only thing I see is a subdirectory called PrivateFrameWorks/

On the other hand, if I open Xcode and go to Xcode - Preferences - Locations, it tells me that the location for Command Line Tools is /Applications/Xcode.app . But don’t see anything that looks relevant in subdirectories of Xcode.app . So I am mystified.

I’m no expert on MacOS so it was trial and error way to find the fix. The directory given above is a location of (among many others) libSystem.tbd file (not libSystem.dylib as one might guess). Try to find that file in your filesystem, maybe it will work.

In a terminal window while logged in with admin privileges, execute the command

sudo xcode-select --install

to make sure the command line tools are installed. This was necessary with all of the 10.x MacOS versions. I don’t know about the 11.x and 12.x versions, but it is someting to check.

I have ordered a new Mac laptop too, so whatever you get working will be of interest to me when it finally is delivered.

Problem solved. Thank you msz59. Following your suggestion I searched for libSystem.tbd and I found it in /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/lib

So I compiled using
gfortran mytest.f90 -L /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/lib

and it worked.

In retrospect it might have been better to install Homebrew first. I just installed it a moment ago. In the process it downloaded Command Line Tools and put it in /Library/Developer/ . It appears that gfortran expects it there, because now gfortran works without specifying -L .

Xcode is huge (about 12 GB) and it’s possible that I’ll never need anything except Command Line Tools. Also, now I have 2 copies of Command Line Tools. So it might have been better to skip Xcode, install Homebrew, and to use it to install packages like gfortran. I just used it to install gnuplot and it worked perfectly.

At one time, it was possible to install the Apple/gnu command line tools without installing the full Xcode. However, I don’t think you can do that now. I’m like you, I don’t really need the whole Xcode install, but these days disk space isn’t as precious as it used to be, so I just install it and move on.

It’s fairly easy but not well advertised by Apple: Install Xcode Command Line Tools Directly · Mac Install Guide

You can also follow the development of Asahi Linux, and when it fully rolls out you can use gcc on linux on your M1 Mac. Which would probably give you an easier time installing the GCC Fortran compiler in future.

Since installing gfortran I’ve noticed the following:

(1) In one case I needed to add -fallow-argument-mismatch to get a code to compile with the latest gfortran. This was related to compiling a large code with a subroutine that someone else wrote, that I never use, so I was not worried about coding errors.

(2) I have not done a systematic performance test. But I did run a few minute long job on my 2016 MacBook Pro, and also ran it on my M1pro. This was a serial code with no MPI and no OpenMP. The M1pro executed about 2x faster.

You might be in for an interesting result once you start running multi-threaded code on your M1pro. From several tests I have seen the single core performance of the Apple Arm CPU is great, but does not transfer to multi-thread or multi-process performance. Would be interested to see whether you will arrive at the some conclusion.

Would be interested in your experiences here. My Mac M1 with 8 cores has 4 performance cores and 4 efficiency cores. In my very non-scientific experience it seems that limiting to 4 cores is often beneficial.

It depends, I guess. At the moment I do not have access to an M1 machine, but a a while ago I tested my fem code on a M1 Macbook Air. The solvers use BLAS routines a lot (such as DGEMM) and with -framework accelerate the M1 was 2x faster than my Intel Macbook Pro 2020 with OpenBLAS, both without multi-threading. The M1 uses the AMX2 coprocessor and hardly touches the CPU. Each cluster of 4 CPUs has one AMX2, so multithreaded BLAS is not an option with accelerate on an M1, having only 4 high-performance cores. In order to get to a comparable speed on the Intel machine I had to use multti-threaded BLAS with all the four CPUs and the fans making a lot of noise. The Macbook Air doesn’t have a fan and therefore is silent.

You just need to install brew first,

Then in the terminal, type

brew install gfortran mpich

Done.

The latest version on brew is perhaps 11.3.0. There are other more latest version of gfortran, but the brew seems very easy to have mpi installed together with gfortran.

You probably also need to install Make.

I learned from @wiremoons,

The Xcode stuff you mentioned is for Intel OneAPI, however Intel OneAPI does not optimize for M1, so the speed of Intel Fortran will be the same as gfortran with -O0 or at best -Og. Also the Python distribution in Intel OneAPI will fail when install on M1 Mac. You need to de-select the Python part if you want to install OneAPI on M1 Mac.

It your code is memory bound, you will see performance gain from M1 chip, since Apple uses HBM which is designed particularly for M1.
if your code is CPU bound, you perhaps will not see too much performance gain from M1 chip especially if your Intel chip is relatively not too old.


PS.

However I am not sure if gfortran (or gcc) is fully compatible with M1 chip now. I personally have some code run with the same 11.2.0 gfortran on Linux well and can also run on Windows, but on M1 it can give me segment 11 error. If you have things like below (procedure point to some function) you may encounter compatibility issues.

image

Thanks CRquantum. I just did
brew install gfortran mpich
and it installed without any problems. I recompiled and reran a serial code, and it executed about 25% faster than the version of gfortran that I installed previously without using brew. I will try compiling and running an MPI job later today or tomorrow.

2 Likes

gfortran 12 intended to patch at least one of the segfault errors on M1. I do not know if it has happened.

1 Like