OS X package installation depends on gfortran-4.8 - r

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

Related

RcppExports.o Error 127 when trying to install Monocle3 [duplicate]

I'm trying to use packages that require Rcpp in R on my M1 Mac, which I was never able to get up and running after purchasing this computer. I updated it to Monterey in the hope that this would fix some installation issues but it hasn't. I tried running the Rcpp check from this page but I get the following error:
> Rcpp::sourceCpp("~/github/helloworld.cpp")
ld: warning: directory not found for option '-L/opt/R/arm64/gfortran/lib/gcc/aarch64-apple-darwin20.2.0/11.0.0'
ld: warning: directory not found for option '-L/opt/R/arm64/gfortran/lib'
ld: library not found for -lgfortran
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [sourceCpp_4.so] Error 1
clang++ -arch arm64 -std=gnu++14 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I../inst/include -I"/Library/Frameworks/R.framework/Versions/4.1-arm64/Resources/library/Rcpp/include" -I"/Library/Frameworks/R.framework/Versions/4.1-arm64/Resources/library/RcppArmadillo/include" -I"/Users/afredston/github" -I/opt/R/arm64/include -fPIC -falign-functions=64 -Wall -g -O2 -c helloworld.cpp -o helloworld.o
clang++ -arch arm64 -std=gnu++14 -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/Library/Frameworks/R.framework/Resources/lib -L/opt/R/arm64/lib -o sourceCpp_4.so helloworld.o -L/Library/Frameworks/R.framework/Resources/lib -lRlapack -L/Library/Frameworks/R.framework/Resources/lib -lRblas -L/opt/R/arm64/gfortran/lib/gcc/aarch64-apple-darwin20.2.0/11.0.0 -L/opt/R/arm64/gfortran/lib -lgfortran -lemutls_w -lm -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation
Error in Rcpp::sourceCpp("~/github/helloworld.cpp") :
Error 1 occurred building shared library.
I get that it can't "find" gfortran. I installed this release of gfortran for Monterey. When I type which gfortran into Terminal, it returns /opt/homebrew/bin/gfortran. (Maybe this version of gfortran requires Xcode tools that are too new—it says something about 13.2 and when I run clang --version it says 13.0—but I don't see another release of gfortran for Monterey?)
I also appended /opt/homebrew/bin: to PATH in R so it looks like this now:
> Sys.getenv("PATH")
[1] "/opt/homebrew/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/Library/TeX/texbin:/Applications/RStudio.app/Contents/MacOS/postback"
Other things I checked:
Xcode command line tools is installed (which clang returns /usr/bin/clang).
Files ~/.R/Makevars and ~/.Renviron don't exist.
Here's my session info:
R version 4.1.1 (2021-08-10)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Monterey 12.1
Matrix products: default
LAPACK: /Library/Frameworks/R.framework/Versions/4.1-arm64/Resources/lib/libRlapack.dylib
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] compiler_4.1.1 tools_4.1.1 RcppArmadillo_0.10.7.5.0
[4] Rcpp_1.0.7
Background
Currently (2023-02-20), CRAN builds R 4.2 binaries for Apple silicon using Apple clang from Command Line Tools for Xcode 13.1 and using an experimental fork of GNU Fortran 12.
If you obtain R from CRAN (i.e., here), then you need to replicate CRAN's compiler setup on your system before building R packages that contain C/C++/Fortran code from their sources (and before using Rcpp, etc.). This requirement ensures that your package builds are compatible with R itself.
A further complication is the fact that Apple clang doesn't support OpenMP, so you need to do even more work to compile programs that make use of multithreading. You could circumvent the issue by building R itself and all R packages from sources with LLVM clang, which does support OpenMP, but that approach is onerous and "for experts only".
There is another approach that has been tested by a few people, including Simon Urbanek, the maintainer of R for macOS. It is experimental and also "for experts only", but it works on my machine and is much simpler than learning to build R yourself.
Instructions for obtaining a working toolchain
Warning: These come with no warranty and could break at any time. Some level of familiarity with C/C++/Fortran program compilation, Makefile syntax, and Unix shells is assumed. Everyone is encouraged to consult official documentation, which is more likely to be maintained than answers on SO. As usual, sudo at your own risk.
I will try to address compilers and OpenMP support at the same time. I am going to assume that you are starting from nothing. Feel free to skip steps you've already taken, though you might find a fresh start helpful.
I've tested these instructions on a machine running Big Sur, and at least one person has tested them on a machine running Monterey. I would be glad to hear from others.
Download an R 4.2 binary from CRAN here and install. Be sure to select the binary built for Apple silicon.
Run
$ sudo xcode-select --install
in Terminal to install the latest release version of Apple's Command Line Tools for Xcode, which includes Apple clang. You can obtain earlier versions from your browser here. However, the version that you install should not be older than the one that CRAN used to build your R binary.
Download the gfortran binary recommended here and install by unpacking to root:
$ curl -LO https://mac.r-project.org/tools/gfortran-12.0.1-20220312-is-darwin20-arm64.tar.xz
$ sudo tar xvf gfortran-12.0.1-20220312-is-darwin20-arm64.tar.xz -C /
$ sudo ln -sfn $(xcrun --show-sdk-path) /opt/R/arm64/gfortran/SDK
The last command updates a symlink inside of the gfortran installation so that it points to the SDK inside of your Command Line Tools installation.
Download an OpenMP runtime suitable for your Apple clang version here and install by unpacking to root. You can query your Apple clang version with clang --version. For example, I have version 1300.0.29.3, so I did:
$ curl -LO https://mac.r-project.org/openmp/openmp-12.0.1-darwin20-Release.tar.gz
$ sudo tar xvf openmp-12.0.1-darwin20-Release.tar.gz -C /
After unpacking, you should find these files on your system:
/usr/local/lib/libomp.dylib
/usr/local/include/ompt.h
/usr/local/include/omp.h
/usr/local/include/omp-tools.h
Add the following lines to $(HOME)/.R/Makevars, creating the file if necessary.
CPPFLAGS += -I/usr/local/include -Xclang -fopenmp
LDFLAGS += -L/usr/local/lib -lomp
Run R and test that you can compile a program with OpenMP support. For example:
if (!requireNamespace("RcppArmadillo", quietly = TRUE))
install.packages("RcppArmadillo")
Rcpp::sourceCpp(code = '
#include <RcppArmadillo.h>
#ifdef _OPENMP
# include <omp.h>
#endif
// [[Rcpp::depends(RcppArmadillo)]]
// [[Rcpp::export]]
void omp_test()
{
#ifdef _OPENMP
Rprintf("OpenMP threads available: %d\\n", omp_get_max_threads());
#else
Rprintf("OpenMP not supported\\n");
#endif
}
')
omp_test()
OpenMP threads available: 8
If the C++ code fails to compile, or if it compiles without error but you get linker warnings or you find that OpenMP is not supported, then one of us has probably made a mistake. Please report any issues.
References
Everything is a bit scattered:
R Installation and Administration manual [link]
R for macOS Developers page [link]
I resolved this issue by adding a path to the homebrew installation of gfortran to my ~/.R/Makevars following these instructions: https://pat-s.me/transitioning-from-x86-to-arm64-on-macos-experiences-of-an-r-user/#gfortran
I just avoided the issue until MacOS had things working more smoothly. so I either Windows Developer Virtual Machine (VM) or run my code development in another environment. I'm not too impressed with the updated and "faster" chipset, but that it doesn't work with much. Slow to implement and work-a-rounds often are a must.
Tested the following process for making multithread data.table work in a M2 MacBook Pro (macOS Monterey)
Steps are mostly the same with this answer by the user inferator.
Download and install R from CRAN
Download and install RStudio with developer tools
Run the following commands in terminal to install OpenMP
curl -O https://mac.r-project.org/openmp/openmp-12.0.1-darwin20-Release.tar.gz
sudo tar fvxz openmp-12.0.1-darwin20-Release.tar.gz -C /
Add compiler flags to connect clan w/ OpenMP. In terminal, write the following:
cd ~
mkdir .R
nano .R/Makevars
Inside the opened Makevars file paste the following lines. Once finished, hit command+O and then Enter to save. Do a command+X to close the editor.
CPPFLAGS += -Xclang -fopenmp
LDFLAGS += -lomp
Download and run the installer for gfortran by downloading gfortran-ARM-12.1-Monterey.dmg from the respective GitHub repo
This concludes the steps regarding enabling OpenMP and (hopefully) Rcpp in R under a M2 chip system.
Now, for testing that everything works with data.table I did the following
Open RStudio and run
install.packages("data.table", type = "source")
If everything is done correctly, the package should compile without any errors and return the following when running getDTthreads(verbose = TRUE):
OpenMP version (_OPENMP) 201811
omp_get_num_procs() 8
R_DATATABLE_NUM_PROCS_PERCENT unset (default 50)
R_DATATABLE_NUM_THREADS unset
R_DATATABLE_THROTTLE unset (default 1024)
omp_get_thread_limit() 2147483647
omp_get_max_threads() 8
OMP_THREAD_LIMIT unset
OMP_NUM_THREADS unset
RestoreAfterFork true
data.table is using 4 threads with throttle==1024. See ?setDTthreads.
[1] 4

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

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.

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.

Why is Rpy2 not installing in my OSX Sierra terminal?

I have seen a number of different people post this problem (example: Tried to guess R's HOME but no R command in the PATH. OsX 10.6 and Installing rpy2 on Mac OSX 10.8.5), but I have yet to find a viable solution.
I have ensured that I have Python-2.7 installed in my terminal, and since I had recently upgraded by RStudio, I thought that my R was also updated. But everytime I tried to run either:
pip install rpy2
or
easy_install rpy2
I kept getting the same error of Error: Tried to guess R's HOME but no command 'R' in the PATH.
After some googling, I decided to try to download the latest version of R (v 3.4.1), even though I'm very certain it was already updated to begin with. But now I get the following behemoth of an error (I should note that I have condensed it):
In file included from ./rpy/rinterface/_rinterface.c:122:
./rpy/rinterface/sequence.c:2173:1: warning: unused function 'ComplexVectorSexp_AsSexp' [-Wunused-function]
ComplexVectorSexp_AsSexp(PyObject *pyfloat) {
^
11 warnings generated.
cc -bundle -undefined dynamic_lookup -arch x86_64 -arch i386 -Wl,-F. build/temp.macosx-10.12-intel-2.7/./rpy/rinterface/_rinterface.o -L/usr/local/lib -Lbuild/temp.macosx-10.12-intel-2.7 -L/usr/local/lib -lpcre -llzma -lbz2 -lz -licucore -lm -liconv -lr_utils -o build/lib.macosx-10.12-intel-2.7/rpy2/rinterface/_rinterface.so -fopenmp -F/Library/Frameworks/R.framework/.. -framework R
clang: error: unsupported option '-fopenmp'
clang: error: unsupported option '-fopenmp'
error: command 'cc' failed with exit status 1
----------------------------------------
Command "/usr/bin/python -u -c "import setuptools, tokenize;__file__='/private/tmp/pip-build-zT8DaX/rpy2/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-bDnbE5-record/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /private/tmp/pip-build-zT8DaX/rpy2/
So now I have a completely different error occurring. I thought that upgrading my R would automatically add it to my environmental variable PATH, but now I'm really at a loss at how to deal with this. I have tried to google this new error but I can't seem to find any solutions that are geared towards installing Rpy2 on a Mac OSX with Python-2.7 and R-3.4. Can anybody offer any kind of advice?
ADDENDUM
I have now tried a variety of other approaches, including downloading XCODE, and updating my GCC compiler, (one such approach is delineated in http://cs.millersville.edu/~gzoppetti/InstallingGccMac.html), and I have also tried to reinstall GCC with the command brew reinstall gcc --without-multilib. I have also edited my PATH environmental variables to include R, CC, and have still had no luck.
I have now figured out that rpy2 installation is contingent upon the right kind of compiler being used. The default compiler for Apple is clang, which does not support rpy2's set up process. So, after acquiring the gcc compiler via homebrew, you can then run brew ls gcc to see the file paths of what gcc compilers are present on your computer. I then ran the following commands in the terminal to change the default compiler from clang to the file path gcc-7, then set the proper flags (which I deduced from reading the error messages yielded by the compiler before), and then install rpy2.
export CC=/usr/local/Cellar/gcc/7.2.0/bin/x86_64-apple-darwin16.7.0-gcc-7
export CFLAGS="-W"
pip install rpy2==2.8.6
I should note that I installed rpy2 version 2.8.6 since my scripts are in Python 2.7, and rpy2 2.9.0 is only compatible with Python 3.x. Furthermore, I am aware that this answer might not adequately describe the problem for everyone, so I encourage other users to expand and/or correct this solution.
The error about "no R in the PATH" is exactly about what it says. To reproduce, open a terminal and enter "R": there should not be any such command found. The solution is to have R findable in the PATH.
Now starting with R-3.4 the tools needed to compile R and and R extensions in C have changed, and the requirements are less standard than one might hope for. This can be debated to be an issue with R and OS X, and there is an open issue in the rpy2 tracker about it.
edit: Note that the issue was resolved and a precompiled binary wheel for rpy2-2.9.3 is now available on pypi.
It's possible to do the GCC switch with a single env command:
env CC=/usr/local/Cellar/gcc/7.2.0/bin/gcc-7 pip install rpy2
The reason clang doesn't work with RPy2 is because clang 4.0.0 doesn't include the -fopenmp flag, which is used by R. There's an ongoing open problem thread on the RPy2 Bitbucket about it.

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