Problems installing R package "nloptr" on M1 mac - r

I'm trying to install the "lme4" library in R and RStudio, which worked before I was on an M1 Mac, but now it's not installing. The dependency that's having trouble is: "nloptr". Here's my current error:
clang++ -arch arm64 -std=gnu++11 -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 nloptr.so init_nloptr.o nloptr.o test-C-API.o test-runner.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 -Lnlopt/lib -lnlopt -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation
ld: warning: directory not found for option '-Lnlopt/lib'
ld: library not found for -lnlopt
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [nloptr.so] Error 1
ERROR: compilation failed for package ‘nloptr’
* removing ‘/Library/Frameworks/R.framework/Versions/4.1-arm64/Resources/library/nloptr’
Warning in install.packages :
installation of package ‘nloptr’ had non-zero exit status
The downloaded source packages are in
‘/private/var/folders/ht/y6qd6yfn67x086jtwxvh42tw0000gn/T/RtmpULtpZq/downloaded_packages’ ```
I'm on an M1 Mac with Monterey (12.1). I've installed the arm64 version of R. Here's my current version R 4.1.2:
> version
_
platform aarch64-apple-darwin20
arch aarch64
os darwin20
system aarch64, darwin20
status
major 4
minor 1.2
year 2021
month 11
day 01
svn rev 81115
language R
version.string R version 4.1.2 (2021-11-01)
nickname Bird Hippie
I've already tried the following in Terminal: brew install nlopt and brew install gcc.
Before this, I also had an error as follows:
ld: warning: directory not found for option '-L/opt/R/arm64/gfortran/lib/gcc/aarch64-apple-darwin20.2.0/11.0.0'
To fix that, based on Googling, I did this
export PATH=$PATH:/opt/R/arm64/gfortran/bin
and this
ln -sfn `xcrun --show-sdk-path` /opt/R/arm64/gfortran/SDK

On my machine (an M1 Mac running Big Sur), I've just tried install.packages("nloptr") without first doing brew install nlopt.
I get the same warning about -Lnlopt/lib:
ld: warning: directory not found for option '-Lnlopt/lib'
ld: library not found for -lnlopt
clang-13: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [nloptr.so] Error 1
ERROR: compilation failed for package ‘nloptr’
but prior to the warning I see:
checking if pkg-config knows NLopt... no
using NLopt via local cmake build on arm64
------------------ CMAKE NOT FOUND --------------------
CMake was not found on the PATH. Please install CMake:
- yum install cmake (Fedora/CentOS; inside a terminal)
- apt install cmake (Debian/Ubuntu; inside a terminal).
- pacman -S cmake (Arch Linux; inside a terminal).
- brew install cmake (MacOS; inside a terminal with Homebrew)
- port install cmake (MacOS; inside a terminal with MacPorts)
Alternatively install CMake from: <https://cmake.org/>
-------------------------------------------------------
install.packages("nloptr") succeeds after I install CMake with brew install cmake.
As for your initial issue with gfortran, I might suggest trying my instructions here. R recommends installing a specific build of gfortran and configuring compilers to find that installation via ~/.R/Makevars. You really should not need to mess with your PATH...

Unfortunately brew install cmake didn't help me, nor did installing nloptr from source, nor did configuring RStudio to use the correct PATH.
According to the documentation, in the section labeled Installing CMake (macOS and Linux only), the nloptr package installer expects to find the cmake executable either at:
The path specified by the CMAKE_BIN environment variable (spoiler alert: setting it didn't work), or
/Applications/CMake.app/Contents/bin/cmake.
Here's how I finally resolved the problem in a zsh shell:
brew install cmake
sudo mkdir -p /Applications/cmake.app/Contents/bin
cd /Applications/cmake.app/Contents/bin
sudo ln -s /opt/homebrew/bin/cmake
The downside to this approach is that it puts a non-application in the /Applications directory, but that I can live with.

Related

How to install RcppArmadillo on Apple Silicon (M1) Macs

I've been trying many different ways to install RcppArmadillo, but I don't get it to work
install.packages(c('Rcpp'))
Sys.setenv("PKG_CXXFLAGS"="-std=c++11")
install.packages(c('RcppArmadillo'),type = "source")
It gives me this error:
ld: warning: directory not found for option '-L/usr/local/gfortran/lib/gcc/x86_64-apple-darwin18/8.2.0'
ld: library not found for -lquadmath
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [RcppArmadillo.so] Error 1
ERROR: compilation failed for package ‘RcppArmadillo’
* removing ‘/Library/Frameworks/R.framework/Versions/4.0/Resources/library/RcppArmadillo’
Obviously, I don't have x86_64-apple-darwin18/8.2.0 since I installed gfortran for the apple silicon architecture.
Mac version:
macOS Big Sur
11.2.3
Apple M1
R version:
version
_
platform x86_64-apple-darwin17.0
arch x86_64
os darwin17.0
system x86_64, darwin17.0
status
major 4
minor 0.4
year 2021
month 02
day 15
svn rev 80002
language R
version.string R version 4.0.4 (2021-02-15)
nickname Lost Library Book
I think I found an issue. First, I follow this tutorial : R COMPILER TOOLS FOR RCPP ON MACOS
Once completed, it gaves me almost the same error as you mentioned in your post :
ld: warning: directory not found for option '-L/opt/R/arm64/gfortran/.....'
ld: library not found for -lgfortran clang: error: linker command failed
with exit code 1 (use -v to see invocation)
It seems, R is looking for gfortran in /opt/R/arm64 folder.
In the turorial it indicates that gfortran is installed in the /usr/local/gfortran folder.
I created a symbolic link in the /opt/R/arm64 which refers to /usr/local/gfortran with the command
ln -s /usr/local/gfortran /opt/R/arm64
One can set FLIBS in ~/.R/Makevars to one of the following options
# homebrew gfortran
FLIBS=-L/opt/homebrew/opt/gfortran/lib
# gfortran included in R
FLIBS=-L/opt/R/arm64/gfortran/lib
In addition one might want to also define F77 and FC as
F77 = /opt/R/arm64/gfortran/bin/gfortran
FC = /opt/R/arm64/gfortran/bin/gfortran
To verify, try installing the glmnet package which should now succeed.

rJava linker error licuuc with Anaconda & fopenmp error without Anaconda for macOS Sierra 10.12.4

I want to install rJava on macOS Sierra 10.12.4. My current Java version is Java version: 1.8.0_131, according to R CMD javareconf|grep version. I have installed Java with Homebrew's cask.
My Java runs the following
$ java -version
java version "1.8.0_131"
Java(TM) SE Runtime Environment (build 1.8.0_131-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode)
My R version is
R version 3.3.2 (2016-10-31) -- "Sincere Pumpkin Patch"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-apple-darwin11.4.2 (64-bit)
R Java config runs such that
$ R CMD javareconf
Java interpreter : /usr/bin/java
Java version : 1.8.0_131
Java home path : /Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre
Java compiler : /usr/bin/javac
Java headers gen.: /usr/bin/javah
Java archive tool: /usr/bin/jar
Non-system Java on macOS
trying to compile and link a JNI program
detected JNI cpp flags : -I$(JAVA_HOME)/../include -I$(JAVA_HOME)/../include/darwin
detected JNI linker flags : -L$(JAVA_HOME)/lib/server -ljvm
During startup - Warning messages:
1: Setting LC_COLLATE failed, using "C"
2: Setting LC_TIME failed, using "C"
3: Setting LC_MESSAGES failed, using "C"
4: Setting LC_MONETARY failed, using "C"
clang -I/Volumes/osx/201705_anaconda/anaconda/lib/R/include -DNDEBUG -I/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/../include -I/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/../include/darwin -I/Volumes/osx/201705_anaconda/anaconda/include -fPIC -I/Volumes/osx/201705_anaconda/anaconda/include -c conftest.c -o conftest.o
clang -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/Volumes/osx/201705_anaconda/anaconda/lib/R/lib -arch x86_64 -L/Volumes/osx/201705_anaconda/anaconda/lib -lgfortran -L/Volumes/osx/201705_anaconda/anaconda -o conftest.so conftest.o -L/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/server -ljvm -L/Volumes/osx/201705_anaconda/anaconda/lib/R/lib -lR -lintl -liconv -lc -Wl,-framework -Wl,CoreFoundation
JAVA_HOME : /Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre
Java library path: $(JAVA_HOME)/lib/server
JNI cpp flags : -I$(JAVA_HOME)/../include -I$(JAVA_HOME)/../include/darwin
JNI linker flags : -L$(JAVA_HOME)/lib/server -ljvm
Updating Java configuration in /Volumes/osx/201705_anaconda/anaconda/lib/R
Done.
which looks to be properly set-up as guided here and this SO thread unable to compile JNI program rJava so the JDK looks to be correctly installed.
I get the linker error with the command install.packages("rJava")
checking Java support in R... present:
interpreter : '/usr/bin/java'
archiver : '/usr/bin/jar'
compiler : '/usr/bin/javac'
header prep.: '/usr/bin/javah'
cpp flags : '-I/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/../include -I/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/../include/darwin'
java libs : '-L/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/server -ljvm'
...
ld: library not found for -licuuc
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [libjri.jnilib] Error 1
make[1]: *** [src/JRI.jar] Error 2
make: *** [jri] Error 2
ERROR: compilation failed for package ‘rJava’
I have got the same error despite Java sources: official JDK and Homebrew's Java return the same linker/library error.
What could be causing this linker/library licuuc error with Anaconda and how to fix it?
Old threads
Install rJava on macOS Sierra 10.12.1: linker error licuuc: a solution candidate with MacPorts
rJava on MacOS Sierra 10.12.15: unsupported option fopenmp
rJava load error in RStudio/R after "upgrading" to OSX Yosemite
Install xlsx and rJava on macOS Mavericks 10.9.5
Fixes that I have tried
options("java.home") returns NULL so options("java.home"="/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/") as guided by here
Unistalled Homebrew's Java and replaced it with Oracele's Java and JDK here: the same licuuc library/linker error persists. I tried sudo Rscript -e 'install.packages("rJava", repos="http://rforge.net", type="source")' guided by this but the same licuuc library not found error.
Updated macOS from El Capitan 10.11.6 to Sierra 10.12.4 but the same licuuc library/linker error persists.
Running without sudo, javac missing (following this here and this here). This error may be easiest to fix with pointing the javac directory there to /usr/bin/javac but better solution? When I run it with sudo, I got the same licuuc error.
R CMD javareconf -e LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$JAVA_LD_LIBRARY_PATH
Rscript -e 'install.packages("rJava", repos="http://rforge.net", type="source")'
Unable to locate an executable at "/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/bin/javac"
(-1)
make[2]: *** [org/rosuda/JRI/Rengine.class] Error 2
make[1]: *** [src/JRI.jar] Error 2
make: *** [jri] Error 2
ERROR: compilation failed for package ‘rJava’
After removal of Anaconda conda install anaconda-clean; anaconda-clean --yes; rm -rf ~/anaconda, guided here, and installing R with cask such that brew cask install r-app; sudo R CMD javareconf; Rscript -e 'install.packages("rJava", repos="http://rforge.net", type="source")' so getting a new error
clang: error: unsupported option '-fopenmp'
make[2]: *** [libjri.jnilib] Error 1
make[1]: *** [src/JRI.jar] Error 2
make: *** [jri] Error 2
ERROR: compilation failed for package ‘rJava’
What could be causing this error forenmp without Anaconda and how to fix it?
I provide the solution in two ways: Anaconda and the Brew way. I suggest you to use the Brew solution. In both cases, I have Oracle's JDK installed and if you do this, remember reconfigure Java for R with sudo R CMD javareconf.
Anaconda (solution to the licuuc error)
A similar linker error occurred here. The anaconda path messes things up as
$ R CMD config --ldflags
-L/Users/osx/anaconda3/lib/R/lib -lR -lpcre -llzma -lbz2 -lz -lm -liconv -licuuc -licui18n
so remove a path like below from ~/.bash_profile
export PATH="/Users/osx/anaconda3/bin:$PATH" #Removed to install rJava
so we should get something like
$ R CMD config --ldflags
-F/Library/Frameworks/R.framework/.. -framework R -lpcre -llzma -lbz2 -lz -licucore -lm -liconv
and after this rJava can be installed even with Anaconda with
sudo R CMD javareconf
Rscript -e 'install.packages("rJava", repos="http://rforge.net", type="source")'
congratulations!
Brew (solution to fopenmp error)
Your R installation should be done with Homebrew cask such that
brew cask install r-app
where brew's r package is not enough for this. Shortly, the forenmp problem is in the compiler not having the flag so we have to recompile the compiler. This solution is explained more thoroughly here. The problem in the question is for 3.3.* R and gcc solution used such that
#xcode-select --install #if Xcode commandline tools not installed
brew install homebrew/versions/gcc49 --without-multilib #Long ~70min compiling...
sudo chown -R $(whoami):admin /usr/local
brew link --overwrite --force gcc49
brew unlink gcc49 && brew link gcc49
brew install llvm
mkdir ~/.R; touch ~/.R/Makevars
echo "VER=-4.9
CC=gcc$(VER)
CXX=g++$(VER)
CXX1X=g++$(VER)
CFLAGS=-mtune=native -g -O2 -Wall -pedantic -Wconversion
CXXFLAGS=-mtune=native -g -O2 -Wall -pedantic -Wconversion
FLIBS=-L/usr/local/Cellar/gcc/4.9.3/lib/gcc/4.9" > ~/.R/Makevars
and now
sudo R CMD javareconf
Rscript -e 'install.packages("rJava", repos="http://rforge.net", type="source")'
and now rJava is working!
Solutions here mentioned also in the following because of similar problems
Install rJava on macOS Sierra 10.12.1: linker error licuuc
rJava on MacOS Sierra 10.12.15: unsupported option fopenmp

Error compiling R package samr: `ld: library not found for -lgfortran`

Im trying install.packages("samr")but it looks like its trying to compile the file using Fortran, but my Fortran doesn't seem to be on the path specified by the installer, my Fortran is from brew install gcc (I belive the correct path to be /usr/local/Cellar/gcc/7.1.0/lib/gcc/7). This is my error messge:
install.packages("samr")
--- Please select a CRAN mirror for use in this session ---
Package which is only available in source form, and may need
compilation of C/C++/Fortran: ‘samr’
Do you want to attempt to install these from sources?
y/n: y
installing the source package ‘samr’
trying URL 'https://cloud.r-project.org/src/contrib/samr_2.0.tar.gz'
Content type 'application/x-gzip' length 36702 bytes (35 KB)
==================================================
downloaded 35 KB
* installing *source* package ‘samr’ ...
** libs
gfortran -fPIC -g -O2 -c rankcol.f -o rankcol.o
clang -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 samr.so rankcol.o -L/usr/local/gfortran/lib/gcc/x86_64-apple-darwin15/6.1.0 -L/usr/local/gfortran/lib -lgfortran -lquadmath -lm -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation
ld: warning: directory not found for option '-L/usr/local/gfortran/lib/gcc/x86_64-apple-darwin15/6.1.0'
ld: warning: directory not found for option '-L/usr/local/gfortran/lib'
ld: library not found for -lgfortran
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [samr.so] Error 1
ERROR: compilation failed for package ‘samr’
* removing ‘/Library/Frameworks/R.framework/Versions/3.4/Resources/library/samr’
The downloaded source packages are in
‘/private/var/folders/v_/n5nqr5812074ct0zkldqhg500000gn/T/RtmpBn8P5n/downloaded_packages’
Warning message:
In install.packages("samr") :
installation of package ‘samr’ had non-zero exit status
It looks like environment specific issue. I am using R 3.3.3 on daily basis and I was able to install the package without any issues:
# this one is required by samr and is no longer available via CRAN
source("https://bioconductor.org/biocLite.R")
biocLite("impute")
# samr, itself, can be installed from CRAN
install.packages("samr")
library(samr)
However, I am using slightly different installation of gfortran - directly from the source:
https://gcc.gnu.org/wiki/GFortranBinaries
gfortran --version
GNU Fortran (GCC) 6.3.0
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
and that's the location of gfortran while installing it from project page
> which gfortran
/usr/local/bin/gfortran
> ls -l /usr/local/gfortran/lib/gcc/x86_64-apple-darwin16/6.3.0/ | wc -l
20
Maybe you can try installing it from the project page? Unfortunately, in case of macOS, there are lots of strong assumptions inside R packages (especially for Java and Fortran).

Installing Rmpfr in R 3.2.2 on Mac OS X (10.10.5 Yosemite)

I installed R on Mac OS using homebrew. Until now everything was smooth. Today, I wanted to use arbitrary precision on some calculations on R. I saw that Rmpfr package is easy to use, so I decided to install it. First, I used the usual:
install.packages("Rmpfr")
and I received this message:
checking for mpfr.h... no
configure: error: Header file mpfr.h not found; maybe use --with-mpfr-include=INCLUDE_PATH
ERROR: configuration failed for package ‘Rmpfr’
So I discovered that the mpfr headers were in /usr/local/Cellar/mpfr/3.1.3/include, but not in /usr/include or /usr/local/include
I symlink them, and also the libs in /usr/local/Cellar/mpfr/3.1.3/ to /usr/lib or /usr/local/lib. After that, it starts to compile, but then I received the following message:
clang -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/usr/local/Cellar/r/3.2.2_1/R.framework/Resources/lib -L/usr/local/opt/gettext/lib -L/usr/local/opt/readline/lib -L/usr/local/opt/openssl/lib -o Rmpfr.so Ops.o Summary.o convert.o init.o utils.o -lmpfr -lgmp -F/usr/local/Cellar/r/3.2.2_1/R.framework/.. -framework R -lintl -Wl,-framework -Wl,CoreFoundation
ld: library not found for -lmpfr
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [Rmpfr.so] Error 1
ERROR: compilation failed for package ‘Rmpfr’
Any ideas what can I do?
After several hours trying different options, finally I discovered a work around.
I simlink the libs to the R path:
cd /usr/local/Cellar/r/3.2.2_1/R.framework/Resources/lib
ln -s /usr/local/Cellar/mpfr/3.1.3/lib/libmpfr.* .
ln -s /usr/local/Cellar/gmp/6.0.0a/lib/libgmp* .
After that, I ran:
install.packages('Rmpfr', type = "source", configure.args=c('--with-mpfr-include=/usr/local/include','--with-mpfr-lib=/usr/local/lib'))
And everything compiled OK.
You can simply run brew link gmp mpfr to create the missing symlinks.

"Unable to compile a JNI program" when installing rJava [duplicate]

I am not able to call rJava package in R 3.0. I got the following message
Error: package ‘rJava’ was built before R 3.0.0: please re-install it
I am getting error when I tried to re-install rJava package. I have provided the output of R CMD javareconf
Java interpreter : /usr/bin/java
Java version : 1.7.0_21
Java home path : /usr/lib/jvm/java-7-openjdk-i386/jre
Java compiler : /usr/lib/jvm/java-7-openjdk-i386/jre/../bin/javac
Java headers gen.: /usr/lib/jvm/java-7-openjdk-i386/jre/../bin/javah
Java archive tool: /usr/lib/jvm/java-7-openjdk-i386/jre/../bin/jar
trying to compile and link a JNI progam
detected JNI cpp flags :
detected JNI linker flags :
gcc -std=gnu99 -I/usr/share/R/include -DNDEBUG -fpic -O2 -pipe -g -c conftest.c -o conftest.o
conftest.c:1:17: fatal error: jni.h: No such file or directory
compilation terminated.
make: *** [conftest.o] Error 1
Unable to compile a JNI program
Java library path:
JNI cpp flags :
JNI linker flags :
Updating Java configuration in /usr/lib/R
Done.
I am using Ubuntu 13.04. I also tried apt-get install r-cran-rjava which is not helping to solve the issue. Regarding jni.h there were some solution here. But, not sure how can I use the solution here.
I ran into the exact same issue. My solution was to install the openjdk-7-* via
sudo apt-get install openjdk-7-*
Followed that with
sudo R CMD javareconf
and I was then able to install rJava in R via install.packages("rJava").
While perhaps not the most elegant solution it appears to have solved my problems with getting rJava to work.
For those getting the error:
error: unable to load shared object '/some/dir/rJava/libs/rJava.so': libjvm.so:
cannot open shared object file: No such file or directory
I solved the error locating the library in the system and linking them to /usr/lib:
$sudo updatedb
$locate libjvm.so
/usr/lib/debug/usr/lib/jvm/java-7-openjdk-amd64/jre/lib/amd64/server/libjvm.so
/usr/lib/debug/usr/lib/jvm/java-7-openjdk-amd64/jre/lib/amd64/zero/libjvm.so
/usr/lib/jvm/java-7-openjdk-amd64/jre/lib/amd64/jamvm/libjvm.so
/usr/lib/jvm/java-7-openjdk-amd64/jre/lib/amd64/server/libjvm.so
/usr/lib/jvm/java-7-openjdk-amd64/jre/lib/amd64/zero/libjvm.so
$sudo ln -s /usr/lib/jvm/java-7-openjdk-amd64/jre/lib/amd64/server/libjvm.so /usr/lib/
Installing rJava from the distribution packages as proposed in this askUbuntu answer also works:
sudo apt-get install r-cran-rjava
NOTE: tried from a Debian system.
I was also facing same error which was on RHEL8.1 & i resolved it as follows:
yum --enablerepo=* install java-1.8*
later i ran same command which was giving me error logs of R server.
R CMD javareconf
which turns into following output.

Resources