R package rJava only loads if I launch R with sudo - r

I'm running R 3.0.2 on Ubuntu 14.04.1. I've installed the rJava package, but I can only get it to work in R/RStudio if I launch it with sudo. If I click on the application launcher, or just enter R in the command line, I get the following error when loading the rJava library:
> library(rJava)
Error : .onLoad failed in loadNamespace() for 'rJava', details:
call: dyn.load(file, DLLpath = DLLpath, ...)
error: unable to load shared object '/usr/lib/R/site-library/rJava/libs/rJava.so':
libjvm.so: cannot open shared object file: No such file or director
Error: package or namespace load failed for ‘rJava’
However, if I type 'sudo rstudio' or 'sudo R' in a terminal, rJava loads without a problem. Does anybody know what I'm doing wrong?
Possibly related issue: I installed rJava by entering
sudo apt-get install r-cran-rjava
at the command prompt; installing the normal way:
apt-get install r-cran-rjava
did not work for me, and neither did installing from within R (without launching it using sudo). When I tried to install this way, I ran into the same problem as addressed in this question.

The file /usr/lib/R/site-library/rJava/libs/rJava.so is probably not readable by any user other than root. You can check that with:
ls -l /usr/lib/R/site-library/rJava/libs/rJava.so
If the output is rw------- or rw-rw---- then simple users won't be able to read it. You can correct that with the chmod command:
chmod -R a+rX /usr/lib/R/site-library/rJava/

I tried the solution listed here to no avail. What worked on Ubuntu 14.04 was to create a soft link to libjvm.so in /usr/lib.
ln -s /usr/lib/jvm/java-8-oracle/jre/lib/amd64/server/libjvm.so /usr/lib

You need to unset the JAVA_HOME environmental variable before starting R:
unset JAVA_HOME

Saw this problem when running from Rscript. Sudo worked, Rscript as user did not. I first did chmod -R a+rX as damienfrancois suggested, but that did not work. Then I tried unset JAVA_HOME, as Gon suggested. That worked (or possibly that in combination with the chmod).

Related

Trouble loading rJava into R and RStudio [duplicate]

I recently "upgraded" from OSX Mountain Lion to Yosemite and from R 3.1.3 to 3.2. Immediately after the upgrade, when I opened R or RStudio I got a pop-up message saying that I needed to install Java 6. In addition, loading rJava or any package that depends on rJava (e.g., xlsx) caused RStudio to crash (R also crashed when I tried this by opening R.app directly).
After trying a few fixes found on Stack Overflow and elsewhere (more details below), I am at a point where loading rJava or any package that depends on rJava no longer causes R to crash, but results in the following error:
library(rJava)
Error : .onLoad failed in loadNamespace() for 'rJava', details:
call: dyn.load(file, DLLpath = DLLpath, ...)
error: unable to load shared object '/Library/Frameworks/R.framework/Versions/3.2/Resources/library/rJava/libs/rJava.so':
dlopen(/Library/Frameworks/R.framework/Versions/3.2/Resources/library/rJava/libs/rJava.so, 6): Library not loaded: #rpath/libjvm.dylib
Referenced from: /Library/Frameworks/R.framework/Versions/3.2/Resources/library/rJava/libs/rJava.so
Reason: image not found
Error: package or namespace load failed for ‘rJava’
However, if I invoke R from the command line and then load rJava or any package that depends on rJava, it seems to work (or at least I don't get any error messages).
I've tried a number of different attempted fixes, some of them a few times, and can't quite remember exactly what I did in what order (didn't realize this would be such a morass and wasn't really keeping track), but here's the gist of it:
Added the following to my .bash_profile (per this SO answer):
export JAVA_HOME="/usr/libexec/java_home -v 1.8"
export LD_LIBRARY_PATH=$JAVA_HOME/jre/lib/server
Reconfigured java from the command line as follows:
sudo R CMD javareconf -n
Checked options("java.home") and discovered this was set to NULL. I tried setting it to the following (per this SO question):
options("java.home"="/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/jre")
Installed the latest Java Development Kit and reinstalled rJava from source (can't remember where I found that one).
At some point while trying all of these, I was able to load rJava without crashing R, but instead got the error message posted above. In addition, when I quit RStudio, it would seem to close normally, but then an "RStudio quit unexpectedly" message would pop up, indicating that the program had crashed while trying to close.
I finally decided to install Java for OS X 2014-001 (Java 6), as I seemed to be running out of options. Now, when I opened R or RStudio the "This software needs Java 6" pop-up message no longer appeared. However, I was still getting the .onLoad failed in loadNamespace() for 'rJava' error message posted above.
In reviewing some of the posts I'd already looked at, I noticed another SO answer that I'd missed before, which recommended opening RStudio with the following command line code that gives RStudio the correct path to java:
LD_LIBRARY_PATH=$(/usr/libexec/java_home)/jre/lib/server: open -a RStudio
That opened an RStudio window and I was also able to load rJava and packages that depend on it without getting an error.
Lastly, I tried running R from the command line (which I hadn't done before). It turns out that on the command line, loading rJava or any package that depends on rJava works and does not throw any errors.
So, I can now get rJava to work if I open RStudio from the command line with the code that gives RStudio the java path (as noted above). However, I'd like to find a way to fix the underlying problem, whatever it may be, so that RStudio can be opened in the usual Mac way, without needing a command line kludge. I'm also concerned that having an old version of Java installed could cause problems down the road.
Does anyone have any ideas about how to diagnose and solve this issue?
I had the same problem and went through the same steps as you. The final step to allow starting RStudio through Finder/Spotlight was to link libjvm.dylib to /usr/local/lib:
sudo ln -f -s $(/usr/libexec/java_home)/jre/lib/server/libjvm.dylib /usr/local/lib
-f flag is added to force overwriting existing file/link
On OSX El Capitan 10.11, the user doesn't have permission to write to /usr/lib. So instead, use the following command:
sudo ln -s $(/usr/libexec/java_home)/jre/lib/server/libjvm.dylib /usr/local/lib
You can load the libjvm.dylib explictly, by run like
dyn.load('/Library/Java/JavaVirtualMachines/jdk1.8.0_66.jdk/Contents/Home/jre/lib/server/libjvm.dylib')
library(rJava)
On macOS High Sierra (10.13.1) and Java Version 9 you have to use a slightly different JVM path (notice the missing jre folder in the path compared to the instructions for earlier Java versions):
sudo ln -f -s $(/usr/libexec/java_home)/lib/server/libjvm.dylib /usr/local/lib
You also have to notify R about the JVM:
MY_R_VERSION=$(Rscript -e "cat(with(R.version, sprintf('%s.%s', major, substring(minor, 1, 1))))")
ln -s /usr/local/lib/libjvm.dylib /Library/Frameworks/R.framework/Versions/$MY_R_VERSION/Resources/lib/
Following command works:
sudo ln -s $(/usr/libexec/java_home)/jre/lib/server/libjvm.dylib /usr/local/lib
After that, in RStudio, loading rJava works through loading of the "xlsx" package.
library("xlsx")
P.S. #1 Environment: Mac OS X El Capitan 10.11.3+ with RStudio 0.99.491+ and R 3.2.3+. (I've now tested this also on macOS Sierra (10.12) and R.3.3.1.)
P.S. #2 I find that openxlsx is much faster and doesn't rely on Java to work, so I'm now using that package. Hope that helps others.
You should be able to use the CRAN rJava, without needing to recompile rJava or do any additional steps by linking the shared library to the R frameworks library directory.
sudo ln -s $(/usr/libexec/java_home)/jre/lib/server/libjvm.dylib /Library/Frameworks/R.framework/Libraries
This is how I configured it working properly on two Macs running Mac OS X El Capitan (10.11.6):
Uninstall 'rJava' by issuing the following commands in a terminal window:
Rscript -e 'remove.packages("rJava")'
sudo Rscript -e 'remove.packages("rJava")'
Download and install the Java software from Oracle: https://www.java.com/en/download/mac_download.jsp
Add the following lines to /Users/<userid>/.bashrc using your favorite editor:
# Set JAVA_HOME so rJava package can find it
export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)/jre
Close and restart any terminal, R, and RStudio windows (to pick up the changes to .bashrc).
Run the following command in a Terminal window:
sudo ln -sf $(/usr/libexec/java_home)/jre/lib/server/libjvm.dylib /usr/local/lib
Run the following command in a Terminal window:
sudo Rscript -e 'install.packages("rJava", repos="http://rforge.net", type="source")'
Here is the easy steps for it:
remove the rJava package: remove.packages(rJava)
close R
install latest Java on you mac
open terminal and type this command: sudo R CMD javareconf
Open R and install rJava with this command:
install.packages("rJava", dependencies=TRUE, type="source")
The image not found issue exists with a brand-new install on OSX High Sierra with the latest Java SE 10 JDK.
I was able to solve the path issue with rJava using the fix found on the rJava Github issues page: https://github.com/s-u/rJava/issues/78
R CMD javareconf JAVA_CPPFLAGS=-I/System/Library/Frameworks/JavaVM.framework/Headers
Here what worked for me on MAC:
in your ~/.profile or ~/.bashrc add this line:
%export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home
1.1 % source .profile (or % source .bashrc)
% sudo ln -s $(/usr/libexec/java_home)/jre/lib/server/libjvm.dylib /usr/local/lib
remove.packages(rJava)
remove.packages(Rweka)
From the terminal enter this command:
%sudo R CMD javareconf
install.packages("rJava", dependencies = TRUE, type = "source")
install.packages("rJava", dependencies = TRUE, type = "source")
Load your library("rJava", "RWeka")
Best of luck.
Maybe another simple answer which does not touch your filesystem:
$ install_name_tool -add_rpath /Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/server /Library/Frameworks/R.framework/Versions/3.4/Resources/library/rJava/libs/rJava.so
(Substitute jdk1.8.0_131.jdk with your JDK path.)
Check the location of libjvm.dylib file.
Try this, in my case this worked:
dyn.load('/Library/Java/JavaVirtualMachines/jdk-10.jdk/Contents/Home/lib/server/libjvm.dylib')
library(rJava)
I went through all the troubleshooting I could find, then installed jdk-11.0.1_osx-x64_bin.dmg from: Oracle downloads
Everything worked perfectly after that.
Check the version of the library that was not loaded when you run library('rJava') and match it to the java version you need installed.
I noticed that 'rJava' is pretty strict about JDK version while loading in Rstudio. In my case (Mac OS. 10.14.6 - Mojave) I had installed jdk-13 which was tested against jdk-11.
I have linked my JDK version to a non-existent directory that was assumed to be valid by Rstudio (JDK 11):
sudo ln -sf /Library/Java/JavaVirtualMachines/jdk-13.jdk/ /Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk
and suprisingly it works like a charm...
BTW., there was no problem to run 'rJava' following the previous instructions in native R.
Here you can download the legacy Java version 6 for El Capitan: https://support.apple.com/kb/DL1572?viewlocale=en_US&locale=en_US
After doing so, the rJava package works for me.
This line below has solved the same problem I was having with rJava package as some others in this discussion did. I'm sure there are more than one solution to this problem and I sincerely thank them all for their contributions because sometimes one line like the one below saves a lot of time from going to trash!
sudo ln -s $(/usr/libexec/java_home)/jre/lib/server/libjvm.dylib /usr/local/lib
Run the following on the command line: sudo R CMD javareconf
Several solutions above have mentioned this, but they also suggest that one remove and subsequently re-install the rJava package. I found those additional steps to be unnecessary.
You can change the reference in R by using the following command:
sudo install_name_tool -change /Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home/lib/server/libjvm.dylib /Library/Java/JavaVirtualMachines/jdk-13.0.1.jdk/Contents/Home/lib/server/libjvm.dylib /Library/Frameworks/R.framework/Versions/3.6/Resources/library/rJava/libs/rJava.so

trouble installing and loading rJava on mac El Capitan

Have issue installing rJava, tried most troubleshooting steps here and doesn't work.
It's returning error
Loading required package: rJava
Error: package or namespace load failed for 'rJava':
.onLoad failed in loadNamespace() for 'rJava', details:
call: dyn.load(file, DLLpath = DLLpath, ...)
error: unable to load shared object '/Library/Frameworks/R.framework/Versions/3.4/Resources/library/rJava/libs/ rJava.so':
I'm on latest ver of R/Rstudio and Mac El Capitan
I also tried this method 1 which didn't work
sudo R CMD javareconf
Then, in the R interpreter:
install.packages('rJava', type='source')
install.packages('xlsx', type='source')
method 2
sudo ln -s $(/usr/libexec/java_home)/jre/lib/server/libjvm.dylib /usr/local/lib
error returned ln: /usr/local/lib/libjvm.dylib: File exists
method 3
install.packages("rJava", type = "binary")
error: tar: Failed to set default locale
to deal with default locale error
system("defaults write org.R-project.R force.LANG en_US.UTF-8")
upon loading rJava, still same error:
image not found
So looks like it's installed but won't load
Here is my solution
Define path to R libraries, here R 3.4 version and open dir:
cd /Library/Frameworks/R.framework/Versions/3.4/Resources/lib
Remove libjvm.dylib:
rm libjvm.dylib
Create link to libjvm.dylib from Java libraries, here jdk1.8.0_151 version:
ln -s /Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home/jre/lib/server/libjvm.dylib libjvm.dylib
now go to R or RStudio and try:
library("rJava")
I did the same thing for java 9, it worked perfectly (macOS High Sierra), thanks:
cd /Library/Frameworks/R.framework/Versions/3.4/Resources/lib
rm libjvm.dylib
ln -s /Library/Java/JavaVirtualMachines/jdk-9.0.1.jdk/Contents/Home/lib/server/libjvm.dylib libjvm.dylib
I also had a lot of trouble with this and eventually found the following SO thread to be the most relevant: rJava load error in RStudio/R after "upgrading" to OSX Yosemite
Basically, starting from no installation of rJava, and starting in the terminal:
Form the sym link (note, the -f flag will overwirte existing links avoiding the error you encountered in "method 2".
$ sudo ln -f -s $(/usr/libexec/java_home)/jre/lib/server/libjvm.dylib /usr/local/lib
Then in RStudio:
Ensure options('java.home') isn't NULL, and set if necessary:
options("java.home"="/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre")
Ensure the JAVA_HOME and LD_LIBRARY_PATH environment variables are set, and set if not:
Sys.setenv(JAVA_HOME='/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home')
Sys.setenv(LD_LIBRARY_PATH='$JAVA_HOME/jre/lib/server')
After this I was able to install and load the rJava package in RStudio with
install.packages('rJava')
library(rJava)
I have tried most of the recommendations on this site, but none worked. The only working solution for me was on this Github site site.
Hope this solves your issues.
The problem was rJava wont install in RStudio (Version 1.0.136). The following worked for me (macOS Sierra version 10.12.6) (found here):
Step-1: Download and install javaforosx.dmg from here
Step-2: Next, run the command from inside RStudio:
install.packages("rJava", type = 'source')

Failed to lock directory when installing R package using devtools::install_github()

When installing an R package from GitHub using devtools::install_github(), the following error was returned:
ERROR: failed to lock directory ‘/Library/Frameworks/R.framework/Versions/3.3/Resources/library’ for modifying
Try removing ‘/Library/Frameworks/R.framework/Versions/3.3/Resources/library/00LOCK-jrosen48-prcr-4f6f783’
When I navigate using Terminal on Macintosh OS 10.12 to that location, it is not available ( R.framework did not appear in the "Frameworks" directory).
How can I try removing ‘/Library/Frameworks/R.framework/Versions/3.3/Resources/library/00LOCK-jrosen48-prcr-4f6f783’? Or, is there another workaround for this?
Try removing /Library/Frameworks/R.framework/Versions/3.3/Resources/library/00LOCK-jrosen48-prcr-4f6f783
Remove using rm command from your command line:
:$ rm -rf /Library/Frameworks/R.framework/Versions/3.3/Resources/library/00LOCK-jrosen48-prcr-4f6f783
Then install the package again.

RQuantLib fails to load

I've recently updated all packages. Now RQuantLib fails to load. What can I do? I've tried to remove and reinstall the package in version 0.3.12 both from CRAN and from the source file, neither helps.
> library(RQuantLib)
Error : .onLoad failed in loadNamespace() for 'RQuantLib', details:
call: if (is.character(qc) && nchar(qc) > 1) {
error: missing value where TRUE/FALSE needed
In addition: Warning message:
running command 'bash -c 'type -p quantlib-config' 2>/dev/null' had status 1
Error: package or namespace load failed for ‘RQuantLib’
I'm working in RStudio with R 3.0.2 on Mac OS Mavericks. What can I do?
I had the same issue and after some hours of struggle I manage to use something not very rigorous but it works!
It seems that R has some issues evaluating system("bash -c 'type -p quantlib-config'").
After having installed QuantLib, typing the command bash -c 'type -p quantlib-config' in Terminal gives us the path of our quantlib-config which is for me /opt/local/bin/quantlib-config.
So I went back to my source folder for RQuantLib (version 0.4.0) and replaced qc in file R/inline.Rby:
qc <- as.character("/opt/local/bin/quantlib-config")
and rebuilt the package using the terminal command R CMD install RQuantLib/.
As I said, not very sexy but it works.
A restart of R and Rstudio is necessary.
Hope it helps the next ones and that this will be corrected in future versions.

Why does rJava not work on Ubuntu 14.04 using OpenJDK 7?

Hi I'm having issues with the rJava package from cran.
I have installed
sudo apt-get install openjdk-7-jdk
sudo apt-get install r-cran-rjava
and ran
sudo R CMD javareconf
# Java interpreter : /usr/bin/java
# Java version : 1.7.0_55
# Java home path : /usr/lib/jvm/java-7-openjdk-amd64/jre
# Java compiler : /usr/bin/javac
# Java headers gen.: /usr/bin/javah
# Java archive tool: /usr/bin/jar
I then try to run R and load rJava and get the following error:
R
> library(rJava)
Error : .onLoad failed in loadNamespace() for 'rJava', details:
call: dyn.load(file, DLLpath = DLLpath, ...)
error: unable to load shared object '/usr/lib/R/site-library/rJava/libs/rJava.so':
libjvm.so: cannot open shared object file: No such file or directory
Error: package or namespace load failed for ‘rJava’
I'm on Ubuntu 14.04 64 bit and am using R version 3.1.0 (2014-04-10) -- "Spring Dance"
UPDATE: Actually this is not specific to OpenJDK, I just tried oracle java 8 and got the same result. Also I found this workaround here which I am reluctant to use since it is indeed a workaround and doesn't really explain why it's necessary. The package system should have handled this in my opinion. Seems like libjvm.so is the problem and I have it located here
/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-oracle/jre/lib/amd64/server/libjvm.so
and for some reason rJava fails to find them despite updating with sudo R CMD javareconf.
UPDATE 2: The plot thickens: If I run R as sudo it works.
Thankful for pointers.
you can solve this problem by opening rstudio in super user like
sudo rstudio
then inside R run
install.packages('rJava')
I had the same problem with a similar configuration (R 3.1.0, Ubuntu 12.10, 32-bit). I found the answer was in getting LD_LIBRARY_PATH set properly, as described here:
error: unable to load installed packages just now except that the subdirectory in question is 'client' not 'server'. So now I'm setting my environment like this:
export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-i386
export LD_LIBRARY_PATH=$JAVA_HOME/jre/lib/i386:$JAVA_HOME/jre/lib/i386/client
I was able to solve this permanentelly using this answer: https://stackoverflow.com/a/25932828/3939832
This is usefull if you have Oracle java 7 or 8 installed. Exporting variables in my case was not a permanent solution. You should check what R is using as environment by doing:
Sys.getenv("JAVA_HOME")
and then you can use that environment by creating a java.conf file on /etc/ld.so.conf.d/ as stated in the above answer link.
if you are using oracle java then use following command :
sudo R CMD javareconf
won't help use:
sudo R CMD javareconf **JAVA_HOME**=(path where java home is located)
I tried many things but didnt worked. Then I tried using
sudo rstudio
and then
install.packages('rJava')
Its working. Coool
Installing the rJava package on Ubuntu is not quite as simple as most other R packages. Some quick notes on how to do it(Source: https://www.r-bloggers.com/installing-rjava-on-ubuntu/).
Install the Java Runtime Environment (JRE).
sudo apt-get install -y default-jre
Install the Java Development Kit (JDK).
sudo apt-get install -y default-jdk
Update where R expects to find various Java files.
sudo R CMD javareconf
Install the package.
> install.packages("rJava")
If you have a RStudio session open, then exit and restart it. This is important (a running RStudio session will not pick up these changes!).

Resources