Installing R packages that need compiling in macOS is broken since Big Sur. Here is such an example.
> install.packages('nlme')
There is a binary version available but the source version is later:
binary source needs_compilation
nlme 3.1-150 3.1-151 TRUE
Do you want to install from sources the package which needs compilation? (Yes/no/cancel) Yes
installing the source package ‘nlme’
trying URL 'https://cran.rstudio.com/src/contrib/nlme_3.1-151.tar.gz'
Content type 'application/x-gzip' length 805592 bytes (786 KB)
==================================================
downloaded 786 KB
* installing *source* package ‘nlme’ ...
** package ‘nlme’ successfully unpacked and MD5 sums checked
** using staged installation
** libs
"gfortran-4.8" -fno-optimize-sibling-calls -fPIC -Wall -g -O2 -c chol.f -o chol.o
gfortran-4.8: warning: couldn’t understand kern.osversion ‘20.1.0
clang -mmacosx-version-min=10.13 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I/usr/local/include -fPIC -Wall -g -O2 -c corStruct.c -o corStruct.o
clang -mmacosx-version-min=10.13 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I/usr/local/include -fPIC -Wall -g -O2 -c gnls.c -o gnls.o
clang -mmacosx-version-min=10.13 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I/usr/local/include -fPIC -Wall -g -O2 -c init.c -o init.o
clang -mmacosx-version-min=10.13 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I/usr/local/include -fPIC -Wall -g -O2 -c matrix.c -o matrix.o
clang -mmacosx-version-min=10.13 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I/usr/local/include -fPIC -Wall -g -O2 -c nlOptimizer.c -o nlOptimizer.o
clang -mmacosx-version-min=10.13 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I/usr/local/include -fPIC -Wall -g -O2 -c nlme.c -o nlme.o
clang -mmacosx-version-min=10.13 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I/usr/local/include -fPIC -Wall -g -O2 -c nlmefit.c -o nlmefit.o
clang -mmacosx-version-min=10.13 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I/usr/local/include -fPIC -Wall -g -O2 -c pdMat.c -o pdMat.o
clang -mmacosx-version-min=10.13 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I/usr/local/include -fPIC -Wall -g -O2 -c pythag.c -o pythag.o
"gfortran-4.8" -fno-optimize-sibling-calls -fPIC -Wall -g -O2 -c rs.f -o rs.o
gfortran-4.8: warning: couldn’t understand kern.osversion ‘20.1.0
clang -mmacosx-version-min=10.13 -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/Library/Frameworks/R.framework/Resources/lib -L/usr/local/lib -o nlme.so chol.o corStruct.o gnls.o init.o matrix.o nlOptimizer.o nlme.o nlmefit.o pdMat.o pythag.o rs.o -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation
ld: framework not found CoreFoundation
clang-7: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [nlme.so] Error 1
ERROR: compilation failed for package ‘nlme’
* removing ‘/Library/Frameworks/R.framework/Versions/4.0/Resources/library/nlme’
* restoring previous ‘/Library/Frameworks/R.framework/Versions/4.0/Resources/library/nlme’
Warning in install.packages :
installation of package ‘nlme’ had non-zero exit status
The downloaded source packages are in
‘/private/var/folders/_3/b8whcf8d1bb8w_lr2rrscb5m0000gp/T/RtmpWCjwfs/downloaded_packages’
I've tried reinstalling xcode and Command Line Tools and completed Brew update + upgrade. Any suggestions appreciated.
This has been challenging, but here are the steps I used compile R packages from source on MacOS Big Sur:
(don't believe Software Update if it says 'up to date' - he lies - brew doctor
said my version was actually old)
sudo rm -rf /Library/Developer/CommandLineTools
sudo xcode-select --install
# WARNING: This can take several hours
brew install gcc
brew install llvm
brew cleanup
brew update
brew upgrade
brew reinstall gcc
brew reinstall llvm
sudo ln -s /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/* /usr/local/include/
# I believe you can safely ignore warnings like this:
#ln: /usr/local/include//tcl.h: File exists
#ln: /usr/local/include//tclDecls.h: File exists
#ln: /usr/local/include//tclPlatDecls.h: File exists
#ln: /usr/local/include//tclTomMath.h: File exists
#ln: /usr/local/include//tclTomMathDecls.h: File exists
#ln: /usr/local/include//tk.h: File exists
#ln: /usr/local/include//tkDecls.h: File exists
#ln: /usr/local/include//tkPlatDecls.h: File exists
~/.R/Makevars
file to include only these lines:LOC=/usr/local/gfortran
CC=$(LOC)/bin/gcc -fopenmp
CXX=$(LOC)/bin/g++ -fopenmp
CXX11 = $(LOC)/bin/g++ -fopenmp
CFLAGS=-g -O3 -Wall -pedantic -std=gnu99 -mtune=native -pipe
CXXFLAGS=-g -O3 -Wall -pedantic -std=c++11 -mtune=native -pipe
LDFLAGS=-L$(LOC)/lib -Wl,-rpath,$(LOC)/lib,-L/usr/local/lib
CPPFLAGS=-I$(LOC)/include -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include -I/usr/local/include
FLIBS=-L/usr/local/gfortran/lib/gcc/x86_64-apple-darwin19/10.2.0 -L/usr/local/gfortran/lib -lgfortran -lquadmath -lm
CXX1X=/usr/local/gfortran/bin/g++
CXX98=/usr/local/gfortran/bin/g++
CXX11=/usr/local/gfortran/bin/g++
CXX14=/usr/local/gfortran/bin/g++
CXX17=/usr/local/gfortran/bin/g++
# Compile the nlme package from source
install.packages("nlme", type = "source")
# To check whether openmp is enabled, compile data.table:
install.packages("data.table", type = "source")
Updating macOS to Ventura appears to have broken the compiler toolchain (again). Trying to build a package from source gave the error:
ld: unsupported mach-o filetype (only MH_OBJECT and MH_DYLIB can be linked) in '/usr/local/gfortran/lib/libgcc_ext.10.5.dylib'
collect2: error: ld returned 1 exit status
make: *** [data.table.so] Error 1
Call `pkgbuild::check_build_tools(debug = TRUE)` to diagnose the problem.
@obruzzi suggested deleting the new SDK in their answer below (which would then default to the v13.3 SDK), but generally this isn't good practice as you want the most up-to-date tools so that they 'work together' properly.
The solution that I would recommend is to reinstall the command line tools and gcc (Steps 1, 2 and 3, as described above), then edit your ~/.R/Makevars to:
LOC=/usr/local/Cellar/gcc/13.2.0
CC=$(LOC)/bin/gcc-13 -fopenmp
CXX=$(LOC)/bin/g++-13 -fopenmp
CXX11=$(LOC)/bin/g++-13 -fopenmp
CFLAGS=-g -O3 -Wall -std=gnu99 -mtune=native -pipe
CXXFLAGS=-g -O3 -Wall -std=c++11 -mtune=native -pipe
LDFLAGS=-L$(LOC)/lib -Wl,-rpath,$(LOC)/lib
CPPFLAGS=-I$(LOC)/include -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include -I/usr/local/include
# Previous vars
FLIBS=-L$(LOC)/lib -L/usr/local/Cellar/gcc/13.2.0/lib/gcc/current/ -lgfortran -lquadmath -lm
CXX1X=$(LOC)/bin/g++-13 -fopenmp
CXX98=$(LOC)/bin/g++-13 -fopenmp
CXX11=$(LOC)/bin/g++-13 -fopenmp
CXX14=$(LOC)/bin/g++-13 -fopenmp
CXX17=$(LOC)/bin/g++-13 -fopenmp
Then, running pkgbuild::check_build_tools(debug = TRUE)
returned Your system is ready to build packages!
and data.table compiled with openMP support as expected.