M1 Mac: Cannot build packages requiring compilation

Hi,

This is my first M1 Mac and the experience has been a nightmare so far. I cannot install basically any package that requires compilation as it fails every time with error messages that don't mean anything to me. For some context, I am running on a Mac Studio with an M1 Max chip on MacOS Monterey 12.6.

More context:

  • I installed my R version 4.0.5 using rig
  • I have installed the XCode command line tools
  • I have used Homebrew to install many things that I had hoped would help but have no as of yet

Many forum answers across the web have me just trying random things hoping it will work but nothing so far.

As an example, when trying to install systemfonts:

ld: warning: directory not found for option '-L/opt/homebrew/Cellar/libpng/1.6.37/lib'
ld: library not found for -lpng16
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [systemfonts.so] Error 1
ERROR: compilation failed for package 'systemfonts'
* removing '/Users/matt/Library/R/4.0/library/systemfonts'
* restoring previous '/Users/matt/Library/R/4.0/library/systemfonts'
Warning in install.packages :
  installation of package 'systemfonts' had non-zero exit status

The downloaded source packages are in
	'/private/var/folders/64/sp5m83kj2_5fl9bcwbdv1s200000gn/T/Rtmp26np4o/downloaded_packages'

I honestly don't even know where to begin to figure out the issue. I have almost never had anything like this come up on any previous Mac I've owned.

1 Like

I have the same exact problem, not even sure where to begin. Really looking forward to someone helping with this.

I still have no solution. Thankfully I can install package binaries or from source when compilation is not required. But I have tried many things to no real success. Something is wrong with the toolchain for compilation on this M1 Mac and I don't really know how to fix it. Hoping an M1 Mac super user will show up and provide some guidance.

Was this guy's post the same problem and no one helped him?

Looks like a different issue to me, but I'm not totally sure.

I would say that other issue is unrelated. That appears to be a package that assumes x86 and dies on an Apple Silicon mac.

Your issue appears to me to be an issue with homebrew and possibly pkg-config. The configuration step of the build is picking up that there should be a copy of libpng16 in in /opt/homebrew/Cellar/libpng/1.6.37/lib/ but then when the linker stage runs, it's not finding it there. If you open a Terminal and run ls -l /opt/homebrew/Cellar/libpng/1.6.37/lib/ do you see libpng16.dylib and/or libpng16.a?

I have had homebrew packages accidentally get picked up by pkg-config before. They try to avoid it by not symlinking packages that macOS provides in the developer tools, but sometimes stuff happens with paths and wrong things slip in.

Do you have a ~/.R/Makevars file? That can often help get R packages to compile correctly. Here's my current working one from my M1 MacBook Pro:

MAKE=/usr/bin/make
CFLAGS=-isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk
CCFLAGS=-isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk
CXXFLAGS=-isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk

F77 = /opt/homebrew/bin/gfortran
FC = $(F77)
FLIBS = -L/opt/homebrew/opt/gcc/lib/gcc/current -lgfortran -lquadmath -lm

n.b. It assumes that you've got the homebrew gfortran installed for packages that need Fortran. You can remove those bits if you don't.

2 Likes

Thanks for your insights, @awatts.

If you open a Terminal and run ls -l /opt/homebrew/Cellar/libpng/1.6.37/lib/ do you see libpng16.dylib and/or libpng16.a ?

No, I get the following: "/opt/homebrew/Cellar/libpng/1.6.37/lib": No such file or directory (os error 2). For extra context, I do have libpng/1.6.38 and the result of ls -l is:

drwxr-xr-x    - matt 23 Sep 12:27  pkgconfig/
lrwxr-xr-x   10 matt 14 Sep 13:42  libpng.a -> libpng16.a
lrwxr-xr-x   14 matt 14 Sep 13:42  libpng.dylib -> libpng16.dylib
.rw-r--r-- 210k matt 23 Sep 12:27  libpng16.16.dylib
.rw-r--r-- 261k matt 14 Sep 13:42  libpng16.a
lrwxr-xr-x   17 matt 14 Sep 13:42  libpng16.dylib -> libpng16.16.dylib

Why isn't the...compiler...linker...using/finding 1.6.38? I hardly even know what questions to be asking.

Do you have a ~/.R/Makevars file?

I don't currently, and I don't recall having ever needed one in 10+ years running R on a Mac. Is this something new for Apple Silicon?

1 Like

The Makevars file is something that has always existed for R (cross plaform; Mac, Unix, Windows). It's just a way to ensure that the build environment that R picks up is correct by being explicit about various make variables.

My next debugging suggestion would be to run pkg-config --cflags --libs libpng16 to see what the compiler would currently be picking up. When I run it on my M1 MBP with macOS 12.5, XCode 14 (plus the command line utils), R 4.2.1, and homebrew up to date (I ran brew upgrade 5 minutes ago) I get:

-I/opt/homebrew/Cellar/libpng/1.6.38/include/libpng16 -L/opt/homebrew/Cellar/libpng/1.6.38/lib -lpng16

Maybe also try brew cleanup to clear out any cruft laying around from old packages.

Thanks for the explanation, @awatts.

My next debugging suggestion would be to run pkg-config --cflags --libs libpng16 to see what the compiler would currently be picking up.

-I/opt/homebrew/Cellar/libpng/1.6.38/include/libpng16 -L/opt/homebrew/Cellar/libpng/1.6.38/lib -lpng16

So I get the same result as you. Not sure why the compiler is trying to use a version that doesn't exist, when one does. I tried running brew upgrade and brew cleanup and still get the same failure due to:

ld: warning: directory not found for option '-L/opt/homebrew/Cellar/libpng/1.6.37/lib'
ld: library not found for -lpng16
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [systemfonts.so] Error 1
ERROR: compilation failed for package 'systemfonts'
* removing '/Users/matt/Library/R/arm64/4.2/library/systemfonts'
* restoring previous '/Users/matt/Library/R/arm64/4.2/library/systemfonts'

Hmm. Next suggestion. Go to /opt/homebrew/lib/pkgconfig/ and look at the contents of libpng.pc and libpng16.pc to see if one or both of them is out of date. Probably not if manually running pkg-config picked up the correct version.

Could you capture the whole traceback of it trying to install the package into a text file and attach it? I could see if I see anything in that.

I am unable to upload a text file on this website, so here is an ugly PDF of the traceback. Hope it is helpful. This is the results of the following command:

install.packages('systemfonts', type = 'source')

traceback.pdf (106.2 KB)

Honestly, at this point, I'd file a bug with systemfonts if I were you. Something wonky is going on with how they're configuring the package and it's breaking on some combination(s) of macOS version and architecture.

I hadn't tried building it on my mac until today (just tried to diagnose from your info) and it's trying to pick up libpng 1.6.37 on mine too, but I've never even had that version installed. This mac is only a month old.

Digging around, I ended up running cat $(R RHOME)/etc/Makeconf and see that by default it's setting PKG_CONFIG_PATH=/usr/lib/pkgconfig:/opt/R/arm64/lib/pkgconfig:/opt/X11/lib/pkgconfig and in the X11 pkgconfig it has its own libpng16.pc file, so that might be getting picked up and messing things up? Quite frankly that's the version of libpng it should be picking up because it's more stable (in path and version) than homebrew's.

Utterly baffled at this point.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.