Installing R on OSX Big Sur (EDIT: and Apple M1) for use with Rcpp and openMP - r

There's probably a gazillion threads on OSX+Rcpp+openMP, but the bottom line right now appears to be this (per coatless):
Unfortunately, with R 4.0.0 the CRAN distributed version of R loses
the ability to use OpenMP without a custom setup.
I came across other ideas, including compiling llvm yourself, using homebrew or macports to install R and/or llvm and/or gcc, and then figuring out how to use the right compiler and/or flags with (R)cpp. However, I find this all very confusing.
I am not a mac user, but it seems to me that setting up a mac to compile Rcpp packages or code snippets with openMP seems to be too difficult for most mac users. However, I would like my R package on github to be used by more users, and since it relies on openMP, I am losing that audience.
Can someone provide the necessary steps to set up R on mac in a way that it can compile Rcpp code with openMP? I'd like to turn that into a quick tutorial.
EDIT: I should have added - on Apple Silicon, because there are some extra confusions where things go - /usr/local vs /opt

I spent a day figuring this out (original post here); here are the steps I used to compile R packages from source with openMP:
Install xcode from the app store (instructions for installing xcode) then install/reinstall the xcode command line tools from the terminal:
# To delete an existing command line tools installation:
sudo rm -rf /Library/Developer/CommandLineTools
# To install the command line tools
sudo xcode-select --install
Install gcc via Homebrew (instructions for installing Homebrew) or, if you already have gcc installed, skip to step 3.
# WARNING: This can take several hours
brew install gcc
To avoid "legacy" version issues:
brew cleanup
brew update
brew upgrade
brew reinstall gcc
Link some headers into /usr/local/include
sudo ln -s /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/* /usr/local/include/
# You can 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
Check your version of gfortran (cd /usr/local/gfortran/lib/gcc/x86_64-apple-darwin19/; ls) then edit your ~/.R/Makevars file (if you don't have a file called Makevars in your ~/.R/ directory) and 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
CPPFLAGS=-I$(LOC)/include -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include
# (check that the version of gfortran - in this case 10.2.0 - matches the version specified in FLIBS)
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++
Open R and install a package to test that it compiles with openMP enabled (when asked, compile from source = "Yes"):
install.packages("data.table", type = "source")
Unfortunately, I do not believe a more "simple" setup exists.

Eventually, I found a process that works on a M1 mac with Big Sur.
Head over to https://mac.r-project.org/, it contains most things you will need
Download and install R via R-4.1-branch.pkg. The CRAN version might also work, but I used the installer from mac.r-project.org, which required opening the osx security settings to allow the installation.
Install RStudio, start it, and let it install the developer tools. Alternatively, run sudo xcode-select --install in Terminal.
Head to https://mac.r-project.org/openmp/. Download openmp-11.0.1-darwin20-Release.tar.gz and install it (see Terminal commands below).
curl -O https://mac.r-project.org/openmp/openmp-11.0.1-darwin20-Release.tar.gz
sudo tar fvx openmp-11.0.1-darwin20-Release.tar.gz -C /
Now we need to add compiler flags so that clan uses openMP. In Terminal, create the Makevars file.
cd ~
mkdir .R
nano .R/Makevars
in nano, paste these additional compiler flags into the Makevars file:
CPPFLAGS += -Xclang -fopenmp
LDFLAGS += -lomp
Hit Control+O, Control+X to save and close
Head over to the gfortran page: https://github.com/fxcoudert/gfortran-for-macOS/releases
Use the installer gfortran-ARM-11.0-BigSur.pkg to install gfortran.
For some reason it appears to install in /usr/local/gfortran, but R expects it in /opt. The mac-R team likes to separate arm64 and intel related files. We could go and fix paths, or simply also install gfortran under /opt. Download the tar file gfortran-ARM-11.0-BigSur.tar.xz. You can use curl, or just download it and point tar in the command line to it.
cd /opt/R/arm64/
sudo mkdir gfortran
sudo tar -xzyf gfortran-ARM-11.0-BigSur.tar.xz -C /opt/R/arm64/
(replace gfortran-ARM-11.0-BigSur.tar.xz with /users/YOURUSERNAME/downloads/gfortran-ARM-11.0-BigSur.tar.xz)
Now it should work.
Not an expert in OSX, but doing this so others can figure out how to use my R package. I'd like to streamline the process some more, but wiping the mac, reinstalling osx and testing it takes so much time.

Related

'RcppArmadillo.h' file not found when installing RcppArmadillo from source

RcppArmadillo 0.8.400.0.0 works just fine on my computer, both from sourceCpp and cxxfunction. Now I want to upgrade to 0.8.500.0, which requires compilation from source. Then I got the error
ccache /usr/local/clang6/bin/clang++ -std=gnu++11 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I"/Library/Frameworks/R.framework/Versions/3.5/Resources/library/Rcpp/include" -I/usr/local/include -I/Applications/CPLEX_Studio128/cplex/include -I/Applications/CPLEX_Studio128/concert/include -DIL_STD -fPIC -Wall -g -O2 -c RcppArmadillo.cpp -o RcppArmadillo.o
RcppArmadillo.cpp:22:10: fatal error: 'RcppArmadillo.h' file not found
#include <RcppArmadillo.h>
^~~~~~~~~~~~~~~~~
1 error generated.
I got this error whether I ran install.packages("RcppArmadillo") in RStudio or in R console outside RStudio. My compiler is clang4, and I have no problem installing RcppParallel or compiling my own Rcpp code that uses RcppArmadillo and RcppParallel. R version: 3.5.0, MacOS High Sierra.
Edit: I looked at https://github.com/RcppCore/RcppArmadillo/issues/143, which is very relevant. I did install clang6 and gfortran 6.1, both downloaded from the CRAN website. I also changed the .R/Makevars file to reflect the changes to clang6. I have also installed ccache as suggested, to make compilation faster. Here's my Makevars:
F77 = /usr/local/gfortran/bin/gfortran
FC = $F77
FLIBS=-L/usr/local/gfortran/lib/gcc/x86_64-apple-darwin16/6.3.0 -L/usr/local/gfortran/lib -lgfortran -lquadmath -lm
CC=ccache /usr/local/clang6/bin/clang
SHLIB_CXXLD=ccache /usr/local/clang6/bin/clang++
CXX= ccache /usr/local/clang6/bin/clang++ -Wall
CXX1X= ccache /usr/local/clang6/bin/clang++
CXX98= ccache /usr/local/clang6/bin/clang++
CXX11= ccache /usr/local/clang6/bin/clang++
CXX14= ccache /usr/local/clang6/bin/clang++
CXX17= ccache /usr/local/clang6/bin/clang++
LDFLAGS=-L/usr/local/clang6/lib
From the outputs in the R console when I was installing the package (added to the error message above), I can tell that clang6 was used for compilation. I also found the downloaded RcppArmadillo tarball, unpacked it, and ran ./configure from the command line. Nothing seemed wrong there; no config.log file was generated. This doesn't seem to be a configuration problem. Then I still got the exact same error when trying to install the package in R, after configuration, in libs. I also didn't get any message about -fopenmp. It seems that my problem is a bit different from that in the GitHub ticket.
R 3.5.x requires clang6 and adding clang6 to your system PATH variable.
Steps:
x Download and install the clang-6.0.0.pkg official package from CRAN.
x Delete your ~/.R/Makevars as it is likely set to use clang4. To do this, open Terminal and type:
sudo rm ~/.R/Makevars
x Then add to your ~/.bash_profile the following:
if [ -d "/usr/local/clang6/bin" ] ; then
export PATH="/usr/local/clang6/bin:$PATH"
fi
Note: If you are using a shell other than bash you will need to add the above into the appropriate file, e.g. for zsh add to ~/.zshrc, as this was not implemented in the official installer.
I should have a few cycles to finish writing a new unofficial Rtools installer a bit later. See: https://github.com/coatless/r-macos-rtools/issues/7
Note: Alternatively, you can remove the paths individual with
sudo vi ~/.R/Makevars
Pressing I to enter insert mode, deleting lines with clang4 in the path, and then pressing ESC + :wq to write changes to file.
Never mind, this problem went away with version 0.8.600. Later, this problem appeared again when I upgraded to RcppArmadillo 0.9.100.5.0. Somehow I only got this error when building from source; I downloaded the binary manually from the CRAN website, and used R CMD INSTALL to install the prebuilt version, and it worked; somehow R doesn't seem to know that there is a binary available for the newest version.
I did install clang6 and modified .R/Makevars to use clang6, but the same problem persisted; probably something else caused the problem.

tidyverse error for dependencies 'broom', 'modelr' are not available [duplicate]

I'm trying to install the package lars. Ubuntu 11.04 Natty 64-bit. From building I get:
* installing *source* package âlarsâ ...
** libs
gfortran -fpic -O3 -pipe -g -c delcol.f -o delcol.o
gcc -shared -o lars.so delcol.o -lgfortran -lm -L/usr/lib64/R/lib -lR
/usr/bin/ld: cannot find -lgfortran
collect2: ld returned 1 exit status
make: *** [lars.so] Error 1
ERROR: compilation failed for package âlarsâ
gfortran is installed and when I run gfortran --version I get
gfortran --version GNU Fortran
(Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2
sudo ldconfig -v gives the error
/sbin/ldconfig.real: Cannot stat /usr/lib/libgfortran.so: No such file or directory
I have already removed and reinstalled gfortran. What do I need to fix this?
I had the same problem when trying to install the CRAN package VGAM on Ubuntu 12.10 64bit. I already had r-base-dev installed, but Andrew Redd's second comment to Dirk Eddelbuettel's answer worked for me.
Specifically, I was getting two errors:
/usr/bin/ld: cannot find -lgfortran
/usr/bin/ld: cannot find -lquadmath
Which were fixed by the lines:
sudo ln -s /usr/lib/x86_64-linux-gnu/libgfortran.so.3 /usr/lib/libgfortran.so
sudo ln -s /usr/lib/x86_64-linux-gnu/libquadmath.so.0 /usr/lib/libquadmath.so
Note that only the first line would be necessary to take care of the problem from the original post. The second line fixed of my additional error with lquadmath.
For the Debian / Ubuntu family, we usually recommend
$ sudo apt-get install r-base-dev
as it pulls in all packages commonly needed for compiling. And this approach gets tested all the time as the automated package builders rely on this (as well as additional per-package Build-Depends). The gfortran package is listed here too; maybe you have a broken link from a prior installation so I'd also try dpkg --purge gfortran; apt-get install gfortran. That said, dozens of R packages (and R itself) use Fortran so there should not be any magic here.
It looks like other suggestions already fixed your problem, but your question also applied to me but the solution was different in my case. My problem was that my gcc and g++ versions differed from my gfortran version. I used the following to switch them so that they were all the same.
Check what version of gcc, g++, and gfortran you have:
g++ --version
gcc --version
gfortran --version
Match them so that they are all the same:
sudo update-alternatives --config g++
sudo update-alternatives --config gcc
sudo update-alternatives --config gfortran
In my case, I only had one version of gfortran so I simply changed the g++ and gcc versions to match that of gfortran.
I use Centos and I can't get r-base-dev. I have also installed gfortran and its version matches that of gcc and g++; it still didn't work. However, I solved this problem by creating ~/.R/Makevars, using
cd ~
mkdir .R
touch Makevars
I found the directory where I installed gfortran (apparently the problem is that R can't find it) by
which gfortran
It said I installed gfortran in usr/bin/gfortran.
Then I added flags to .R/Makevars to tell R to use:
F77 = /usr/bin/gfortran
FC = $F77
FLIBS = -L/usr/bin/gfortran
You can edit the Makevars file this way:
vi .R/Makevars
Now you have entered the vi program that can edit text files. Type i to edit; you will see INSERT by the bottom of the terminal window. Then you can input what I put above. To save the changes and quit vi, press the esc key, and type :wq.
I'm not totally sure if I put the FLIBS line correctly, since it's very different for MacOS. In MacOS, there's a directory under gfortran that has the libraries to link to, but apparently gfortran is not a directory in linux. At least this worked for me, and also solved the problem of /usr/bin/ld: cannot find -lquadmath, so I installed R packages requiring gfortran smoothly.
Same problem installing R package minqa on ubuntu 12.04, R3.1.0., an x86 32bits (actually it was part of the caret package installation).
Solved by
sudo ln -s /usr/lib/i386-linux-gnu/libgfortran.so.3 /usr/lib/libgfortran.so
r-base-dev reinstall didn't work and I didn't try to re-install gfortran because of all the dependencies.
Depending on the system/version,
ls -l /usr/lib/libgfortran.so
checks that the link exists/is right.
For anyone who reaches this page with the same error on a Mac, try the following:
Install Homebrew and run:
brew install gcc
Then, create a file ~/.R/Makevars with the contents (being mindful that this corresponded to gcc version 9.1.0):
VER=-9
CC=gcc$(VER)
CXX=g++$(VER)
CFLAGS=-mtune=native -g -O2 -Wall -pedantic -Wconversion
CXXFLAGS=-mtune=native -g -O2 -Wall -pedantic -Wconversion
FLIBS=-L/usr/local/Cellar/gcc/9.1.0/lib/gcc/9
R v3.6.0
gcc v9.1.0
Homebrew v2.1.6
macOS v10.14.5
Just leaving this here for future reference as in my case (Amazon Linux EC2 AMI) the issue was merely with the naming of the symbolic link and not with its location.
sudo ln -s /usr/lib64/libgfortran.so.3 /usr/lib64/libgfortran.so
sudo ln -s /usr/lib64/libquadmath.so.0 /usr/lib64/libquadmath.so
I didn't have to install any libraries. Posting what worked for me, maybe it will be useful for someone.
I had ~/.R/Makevars defining to use CC=gcc-8.
Default gcc on my machine is 7.4.0, but I installed gcc-8.
At the same time I didn't have gfortran 8, but only 7.4.0.
Commenting out the line in Makevars makes compilation fall back to use default gcc-7, and it was successfully using gfortran-7 lib then.
If you are using gcc44, you'll need:
yum install gcc44-gfortran
For future lost souls, it also helps to verify compiler versions all match (per https://askubuntu.com/questions/276892/cannot-find-lgfortran). In my case gcc and gfortran were both 4.8.4, but g++ was 4.6.
As a follow-on to Megatron's answer for Mac homebrew, I had a similar problem with dependencies:
ERROR: configuration failed for package ‘openssl’
removing ‘/usr/local/lib/R/4.1/site-library/openssl’
Warning in install.packages :
installation of package ‘openssl’ had non-zero exit status
just typed brew install openssl into bash and it worked on next packages.install.

Connecting kdb+ to R

I'm trying to run a make file to install qserver on my OSX machine.
The make file gives me an error when I try to run. see code below:
make
/Library/Frameworks/R.framework/Resources/bin/R CMD gcc -g -O -fpic -m64 -shared -D KXVER=3 -I. -I/Library/Frameworks/R.framework/Resources/share/include base.c c.o -lpthread -L/Library/Frameworks/R.framework/Resources/share/lib -lR -o qserver.so
/Library/Frameworks/R.framework/Resources/bin/R: /Library/Frameworks/R.framework/Resources/bin/R: cannot execute binary file
make: *** [all] Error 126
Hoping someone can give me a pointer here on how to fix it.
Do you have the right version of gcc? I build with gcc-4.8 and have no issues. I recall a thread on the kdb list that mentioned the build required some features available only in later gcc versions. Give that a try.
[EDIT]
Nevermind, just saw that the error says the R binary was not found. Edit the make file to use the appropriate path for your R installation. If you don't have R installed, I suggest installing with homebrew:
brew tap homebrew/science
brew install r
After that you can replace the path in the Makefile as appropriate. I personally changed the Makefile to use $(shell R RHOME) to find the R home path, rather than assume a specific location.
JPC was partially right about gcc.
My system had two different versions of gcc loaded and the default was the standard one. I changed the makefile to run 'gcc-4.8'.
Once I finished that I got a new error: the make file couldn't locate my 'R.h' file...so i just had to reference where my r h-files were located....once I did that the make problem executed.

OS X package installation depends on gfortran-4.8

I'm using the OS X devel version of R
R Under development (unstable) (2014-05-27 r65787)
Platform: x86_64-apple-darwin13.1.0 (64-bit)
with GNU Fortran (GCC) 4.2.3 (from Simon's site) and version 5.1.1 of Xcode's command line tools.
When building from source, I get errors like:
gfortran-4.8 -fPIC -g -O2 -c altmov.f -o altmov.o
make: gfortran-4.8: No such file or directory
I'm not sure what to do here. I could create an alias to the version I have but I'm not sure if it really needs 4.8 or not.
Thanks,
Max
Type these two lines in your terminal, direct quote:
curl -O http://r.research.att.com/libs/gfortran-4.8.2-darwin13.tar.bz2
sudo tar fvxj gfortran-4.8.2-darwin13.tar.bz2 -C /
It will download you the gfortran for Mavericks (which is missing in your system at the moment) and will install it in your system. At least, this solved the same problem for me
(I'm running late 2011 MacPro, Mavericks).
Edit: I now think this is best accomplished using Homebrew:
brew reinstall gcc48 --with-fortran
While I think the following procedure will work, it is not as clean. However, I still recommend configuring ~/.R/Makevars as described below.
To build on the answer by #TheBigMike, I used the following procedure to install gfortran-4.8 alongside homebrew, and then configured R to select this fortran compiler.
First, download and unpack gfortran 4.8:
su admin
curl -O http://r.research.att.com/libs/gfortran-4.8.2-darwin13.tar.bz2
mkdir /tmp/gfortran
tar fvxz gfortran-4.8.2-darwin13.tar.bz2 -C /tmp/gfortran
Delete the gfortran file alias that interferes with homebrew:
rm /tmp/gfortran/usr/local/bin/gfortran
Install gfortran 4.8
cp -r /tmp/gfortran/usr/local/ /usr/local
To tell R to use gfortran 4.8, create a file called ~/.R/Makevars:
mkdir -p ~/.R
touch ~/.R/Makevars
Put the following into ~/.R/Makevars:
FLIBS=""
F77="gfortran-4.8"
FC="gfortran-4.8"
Now restart R. Once R has been restarted, any packages that depend upon fortran 4.8 should now work. homebrew should be undisturbed.
I encountered the same problem, albeit on R Studio.
The following worked for me, although, I had to reinstall all my R packages after (note: the re-installations took awhile and look like quite a bit was going on, unsure if these were the appropriate steps, use at own risk):
Download latest R Studio version and then following in terminal:
brew install gcc
brew update && brew reinstall r
brew link --overwrite r
This is explained nicely in Rcpp FAQ, section 2.16. Basically you need to install gfortran (homebrew install gcc or the other way) and then modify Makeconf to point FLIBS to the relevant fortran libraries.
These were the steps which worked for me:
Install gcc48 via homebrew
Make sure that gfortran and gfortran-4.8 point to the newly installed versions.
Comment the FLIBS variable in /Library/Frameworks/R.framework/Resources/etc/Makeconf.
The step 3 was the most important one, because it is designed to work with the gfortran which is installed in the way #TheBigMike suggested, i.e. by downloading and extracting the archive gfortran-4.8.2-darwin13.tar.bz2.
The following worked for me on OSX High Sierra and RStudio 1.1.423:
Install gcc/gfortran via Homebrew: brew install gcc
Edit /Library/Frameworks/R.framework/Resources/etc/Makeconf and replace all instances of gfortran-4.8 with gfortran
Also replace FLIBS = -L/usr/local/lib/gcc/x86_64-apple-darwin13.0.0/4.8.2 with FLIBS = -L/usr/local/lib/gcc/7/ -lgfortran -lquadmath -lm.
Reattempt installing the package.
Install gcc/gfortran via Homebrew: brew install gcc
Make sure you replace it is not done automatically: /Library/Frameworks/R.framework/Resources/etc/Makeconf and replace all instances of gfortran-4.8(or any number) with gfortran
Replace FLIBS = -L/usr/local/gfortran/lib/gcc/x86_64-apple-darwin15/6.1.0 -L/usr/local/gfortran/lib -lgfortran -lquadmath -lm to
FLIBS = -L/usr/local/lib/gcc/7/ -lgfortran -lquadmath -lm
Reinstall the package for me was robustbase that was required by fpc library
Got the same issue and found a statement in the R cran site (https://cran.ism.ac.jp/) In the latest OSX such as High Sierra, some necessary libs are not included and these have to be installed. The page kindly explains this, quoting
Important: this release uses Clang 6.0.0 and GNU Fortran 6.1, neither
of which is supplied by Apple. If you wish to compile R packages from
sources, you will need to download and install those tools - see the
tools directory.
I resolved the issues (missing gfortran etc.) with applying the following two packages, here.
clang-6.0.0.pkg (OS X 10.11+, signed, 64-bit)
MD5-hash: c29700c4e7b2914073ef7e741eb105bc
(ca. 418Mb)
gfortran-6.1.pkg (OS X 10.11+, signed, 64-bit)
MD5-hash: 201026216e8b373d9cd2efc0cc474bb8
(ca. 73Mb)
This answer by thebigmike suggests:
Type these two lines in your terminal, direct quote:
curl -O http://r.research.att.com/libs/gfortran-4.8.2-darwin13.tar.bz2
sudo tar fvxj gfortran-4.8.2-darwin13.tar.bz2 -C /
but
curl -OL http://r.research.att.com/libs/gfortran-4.8.2-darwin13.tar.bz2
Solves the problem for me!
Updated version of RStudio (Version 0.98.1091) seems to fix this.
Make sure R is updated to version >= 4.0 (for previous versions this might help: https://cran.ism.ac.jp/bin/macosx/tools/)
Download and install the GNU fortran compiler found here: https://mac.r-project.org/tools/
Make sure you add /usr/local/gfortran/bin to your PATH in order to use this compiler. This can be done, e.g. by
export PATH=$PATH:/usr/local/gfortran/bin
If you are having trouble installing software that depends on FORTRAN on Max OS 10.15 or later. The best solution is to update R to at least version 4.0.0
Installing gcc via homebrew give me gfortran:
brew install gcc
Then:
$which gfortran
/usr/local/bin/gfortran

Building R package and error "ld: cannot find -lgfortran"

I'm trying to install the package lars. Ubuntu 11.04 Natty 64-bit. From building I get:
* installing *source* package âlarsâ ...
** libs
gfortran -fpic -O3 -pipe -g -c delcol.f -o delcol.o
gcc -shared -o lars.so delcol.o -lgfortran -lm -L/usr/lib64/R/lib -lR
/usr/bin/ld: cannot find -lgfortran
collect2: ld returned 1 exit status
make: *** [lars.so] Error 1
ERROR: compilation failed for package âlarsâ
gfortran is installed and when I run gfortran --version I get
gfortran --version GNU Fortran
(Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2
sudo ldconfig -v gives the error
/sbin/ldconfig.real: Cannot stat /usr/lib/libgfortran.so: No such file or directory
I have already removed and reinstalled gfortran. What do I need to fix this?
I had the same problem when trying to install the CRAN package VGAM on Ubuntu 12.10 64bit. I already had r-base-dev installed, but Andrew Redd's second comment to Dirk Eddelbuettel's answer worked for me.
Specifically, I was getting two errors:
/usr/bin/ld: cannot find -lgfortran
/usr/bin/ld: cannot find -lquadmath
Which were fixed by the lines:
sudo ln -s /usr/lib/x86_64-linux-gnu/libgfortran.so.3 /usr/lib/libgfortran.so
sudo ln -s /usr/lib/x86_64-linux-gnu/libquadmath.so.0 /usr/lib/libquadmath.so
Note that only the first line would be necessary to take care of the problem from the original post. The second line fixed of my additional error with lquadmath.
For the Debian / Ubuntu family, we usually recommend
$ sudo apt-get install r-base-dev
as it pulls in all packages commonly needed for compiling. And this approach gets tested all the time as the automated package builders rely on this (as well as additional per-package Build-Depends). The gfortran package is listed here too; maybe you have a broken link from a prior installation so I'd also try dpkg --purge gfortran; apt-get install gfortran. That said, dozens of R packages (and R itself) use Fortran so there should not be any magic here.
It looks like other suggestions already fixed your problem, but your question also applied to me but the solution was different in my case. My problem was that my gcc and g++ versions differed from my gfortran version. I used the following to switch them so that they were all the same.
Check what version of gcc, g++, and gfortran you have:
g++ --version
gcc --version
gfortran --version
Match them so that they are all the same:
sudo update-alternatives --config g++
sudo update-alternatives --config gcc
sudo update-alternatives --config gfortran
In my case, I only had one version of gfortran so I simply changed the g++ and gcc versions to match that of gfortran.
I use Centos and I can't get r-base-dev. I have also installed gfortran and its version matches that of gcc and g++; it still didn't work. However, I solved this problem by creating ~/.R/Makevars, using
cd ~
mkdir .R
touch Makevars
I found the directory where I installed gfortran (apparently the problem is that R can't find it) by
which gfortran
It said I installed gfortran in usr/bin/gfortran.
Then I added flags to .R/Makevars to tell R to use:
F77 = /usr/bin/gfortran
FC = $F77
FLIBS = -L/usr/bin/gfortran
You can edit the Makevars file this way:
vi .R/Makevars
Now you have entered the vi program that can edit text files. Type i to edit; you will see INSERT by the bottom of the terminal window. Then you can input what I put above. To save the changes and quit vi, press the esc key, and type :wq.
I'm not totally sure if I put the FLIBS line correctly, since it's very different for MacOS. In MacOS, there's a directory under gfortran that has the libraries to link to, but apparently gfortran is not a directory in linux. At least this worked for me, and also solved the problem of /usr/bin/ld: cannot find -lquadmath, so I installed R packages requiring gfortran smoothly.
Same problem installing R package minqa on ubuntu 12.04, R3.1.0., an x86 32bits (actually it was part of the caret package installation).
Solved by
sudo ln -s /usr/lib/i386-linux-gnu/libgfortran.so.3 /usr/lib/libgfortran.so
r-base-dev reinstall didn't work and I didn't try to re-install gfortran because of all the dependencies.
Depending on the system/version,
ls -l /usr/lib/libgfortran.so
checks that the link exists/is right.
For anyone who reaches this page with the same error on a Mac, try the following:
Install Homebrew and run:
brew install gcc
Then, create a file ~/.R/Makevars with the contents (being mindful that this corresponded to gcc version 9.1.0):
VER=-9
CC=gcc$(VER)
CXX=g++$(VER)
CFLAGS=-mtune=native -g -O2 -Wall -pedantic -Wconversion
CXXFLAGS=-mtune=native -g -O2 -Wall -pedantic -Wconversion
FLIBS=-L/usr/local/Cellar/gcc/9.1.0/lib/gcc/9
R v3.6.0
gcc v9.1.0
Homebrew v2.1.6
macOS v10.14.5
Just leaving this here for future reference as in my case (Amazon Linux EC2 AMI) the issue was merely with the naming of the symbolic link and not with its location.
sudo ln -s /usr/lib64/libgfortran.so.3 /usr/lib64/libgfortran.so
sudo ln -s /usr/lib64/libquadmath.so.0 /usr/lib64/libquadmath.so
I didn't have to install any libraries. Posting what worked for me, maybe it will be useful for someone.
I had ~/.R/Makevars defining to use CC=gcc-8.
Default gcc on my machine is 7.4.0, but I installed gcc-8.
At the same time I didn't have gfortran 8, but only 7.4.0.
Commenting out the line in Makevars makes compilation fall back to use default gcc-7, and it was successfully using gfortran-7 lib then.
If you are using gcc44, you'll need:
yum install gcc44-gfortran
For future lost souls, it also helps to verify compiler versions all match (per https://askubuntu.com/questions/276892/cannot-find-lgfortran). In my case gcc and gfortran were both 4.8.4, but g++ was 4.6.
As a follow-on to Megatron's answer for Mac homebrew, I had a similar problem with dependencies:
ERROR: configuration failed for package ‘openssl’
removing ‘/usr/local/lib/R/4.1/site-library/openssl’
Warning in install.packages :
installation of package ‘openssl’ had non-zero exit status
just typed brew install openssl into bash and it worked on next packages.install.

Resources