Search code examples
rcmakeinstallationshared-librariesnlopt

NLoptr failed to install on Mac: unable to load shared object


I'm trying to install the package nloptr on Mac OS High Sierra in order to use many nice packages like lme4, and I'm using RStudio inside Anaconda, with R 3.4.2. I have also installed NLopt (downloaded from the NLopt website, not the development version. I have also installed Xcode command line tool. But I still can't install nloptr, and get this

Error: package or namespace load failed for ‘nloptr’ in dyn.load(file, DLLpath = DLLpath, ...):
unable to load shared object '/Users/lambda/anaconda3/lib/R/library/nloptr/libs/nloptr.so':
dlopen(/Users/lambda/anaconda3/lib/R/library/nloptr/libs/nloptr.so, 6): Symbol not found: _nlopt_add_equality_mconstraint
Referenced from: /Users/lambda/anaconda3/lib/R/library/nloptr/libs/nloptr.so
Expected in: flat namespace
in /Users/lambda/anaconda3/lib/R/library/nloptr/libs/nloptr.so

I already searched online for what to do, and did add the path where NLopt was installed to LD_LIBRARY_PATH in the /etc/ldpaths file, but I still get the same error. Yes, I have the same problem as in Trouble installing nloptr package on R 3.3.0, but I didn't find a solution that works there since the person who asked that question resolved it himself and I don't know what he meant when he said "installed as a root". Can anyone help?

Also, I got something like this when installing NLopt with CMake, and this happened to many different functions:

In file included from f77api.c:103:
./f77funcs.h:57:14: warning: 'nlopt_minimize_constrained' is deprecated
  [-Wdeprecated-declarations]
 *info = nlopt_minimize_constrained((nlopt_algorithm) *algorithm, 
         ^
./nlopt.h:348:35: note: 'nlopt_minimize_constrained' has been explicitly marked
  deprecated here
 int maxeval, double maxtime) NLOPT_DEPRECATED;
                              ^
./nlopt.h:320:43: note: expanded from macro 'NLOPT_DEPRECATED'
#  define NLOPT_DEPRECATED __attribute__((deprecated))
                                      ^

Would this be why the library isn't loaded?

BTW, I have installed Rcpp and updated it. Rcpp works on my computer.

Update: I changed the default C and C++ compiler to clang4 downloaded from R project. I tried the installation again, and got a different error:

Error: package or namespace load failed for ‘nloptr’ in dyn.load(file, DLLpath = DLLpath, ...):
unable to load shared object '/Users/lambda/anaconda3/lib/R/library/nloptr/libs/nloptr.so':
dlopen(/Users/lambda/anaconda3/lib/R/library/nloptr/libs/nloptr.so, 6): Library not loaded: @rpath/libnlopt.0.dylib
Referenced from: /Users/lambda/anaconda3/lib/R/library/nloptr/libs/nloptr.so
Reason: image not found
Error: loading failed

I have tried to tell R where the library is by Sys.geteng(DYLD_LIBRARY_PATH = "/usr/local/lib"), but it didn't work. It actually appears that R does know where the shared library is, according to the messages I got from installation, but it just can't load it. Please help!


Solution

  • Never mind, I reinstalled Anaconda, and installed the standalone R and RStudio (not within Anaconda), and those packages all installed successfully. Maybe it's a bad idea to use the RStudio inside Anaconda.

    Edit: For those who run into similar scenarios: I switched to Microsoft R Open (MRO), since I'm dealing a lot with linear algebra recently. Then when I tried to install nloptr in MRO, I got a similar error message that shared library failed to load because image not found. I reinstalled the CRAN R 3.4.3 and nloptr installed totally fine there. So I found where nloptr was installed in CRAN R 3.4.3 by .libPaths(), copied the nloptr folder to where MRO stores packages, and used the utility RSwitch to change the default version of R to MRO. Then I loaded nloptr in MRO, installed packages that depend on it, and used those packages; they worked fine. I don't know what's wrong with MRO that caused the error, but at least this worked.