After upgrading from Ubuntu 16 to 18.4, I am not able to install the rJava package in R.

I read other posts like this, but I didn't solve my issue. if I run install.packages("rJava") on R, Results is this:

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 '/home/salvatore/R/x86_64-pc-linux-gnu-library/3.5/rJava/libs/rJava.so':
  libjvm.so: cannot open shared object file: No such file or directory
Error: loading failed
Execution halted
ERROR: loading failed
* removing ‘/home/salvatore/R/x86_64-pc-linux-gnu-library/3.5/rJava’

when I run sudo R CMD javareconf:

Java interpreter : /usr/lib/jvm/java-8-oracle/jre/bin/java
Java version     : 1.8.0_181
Java home path   : /usr/lib/jvm/java-8-oracle
Java compiler    : /usr/lib/jvm/java-8-oracle/bin/javac
Java headers gen.: /usr/lib/jvm/java-8-oracle/bin/javah
Java archive tool: /usr/lib/jvm/java-8-oracle/bin/jar

trying to compile and link a JNI program 
detected JNI cpp flags    : -I$(JAVA_HOME)/include -I$(JAVA_HOME)/include/linux
detected JNI linker flags : -L$(JAVA_HOME)/jre/lib/amd64/server -ljvm
gcc -std=gnu99 -I"/usr/share/R/include" -DNDEBUG -I/usr/lib/jvm/java-8-oracle/include -I/usr/lib/jvm/java-8-oracle/include/linux     -fpic  -g -O2 -fdebug-prefix-map=/build/r-base-oNcpyf/r-base-3.5.1=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c conftest.c -o conftest.o
gcc -std=gnu99 -shared -L/usr/lib/R/lib -Wl,-Bsymbolic-functions -Wl,-z,relro -o conftest.so conftest.o -L/usr/lib/jvm/java-8-oracle/jre/lib/amd64/server -ljvm -L/usr/lib/R/lib -lR

JAVA_HOME        : /usr/lib/jvm/java-8-oracle
Java library path: $(JAVA_HOME)/jre/lib/amd64/server
JNI cpp flags    : -I$(JAVA_HOME)/include -I$(JAVA_HOME)/include/linux
JNI linker flags : -L$(JAVA_HOME)/jre/lib/amd64/server -ljvm
Updating Java configuration in /usr/lib/R

Any help would be welcome.


In my case, I also had to use: sudo R CMD javareconf, to make it work.

| improve this answer | |

On my clean installation of Ubuntu 18.04 LTS I did the following

sudo apt-get install r-base-dev default-jdk

(so I have R 3.4 from official repository).

Then in R or in RStudio 1.1.456 session:


and it ended with

* DONE (rJava)

Note: I'm using default OpenJDK 11 from Bionic repository -

$ update-java-alternatives -l
java-1.11.0-openjdk-amd64      1101      /usr/lib/jvm/java-1.11.0-openjdk-amd64
| improve this answer | |
  • I'm done. On R it work fine, the issues occour on Rstudio. – Salvo Comunque Aug 27 '18 at 16:29
  • I can't reproduce your issue using R 3.4. Why do you use R 3.5? – N0rbert Aug 27 '18 at 16:34
  • 1
    Perfect, I removed R 3.5 and installed R3.4, after installing R studio, all thing works fine. Thank you so mutch for your answer. – Salvo Comunque Aug 27 '18 at 17:02

Running Ubuntu 18.04 64-bit, R 3.60, and RStudio 1.2.1335.

In my case, I had to do the following:

  1. (Temporarily) disable Oracle Java 8 by renaming jdk.csh and jdk.sh to jdk.csh.old and jdk.sh.old (in etc/profile.d).

  2. Install java-11-openjdk.

  3. Add JAVA_HOME="/usr/lib/jvm/java-11-openjdk" to /etc/environment.

  4. Run "sudo update-alternatives --config java" and select "/usr/lib/jvm/java-11-openjdk-amd64.bin/java".

  5. Run "sudo R CMD javareconf".

  6. Logout/login.

  7. Open RStudio and install rJava.

rJava then installed with no errors.

| improve this answer | |

For me, I create a bash file using some of code of the previous answers. This is the bash file:

# JAVA_LD_LIBRARY_PATH library path necessary at run-time
# JAVA_CPPFLAGS  C preprocessor flags necessary to compile JNI programs
# JAVA_LIBS      libraries (as linker flags) necessary to compile JNI programs

export JAVA_HOME=/opt/apps/jdk
export JAVA_LD_LIBRARY_PATH=$JAVA_HOME/jre/lib/amd64/server
export JAVA_CPPFLAGS="-I$JAVA_HOME/include -I$JAVA_HOME/include/linux "
export JAVA_LIBS="-L$JAVA_HOME/jre/lib/amd64/server -ljvm" 

env|grep JAVA

R CMD javareconf
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.