Support

Using rJava with RStudio Server Pro, Launcher and Kubernetes

Follow

If you’re using RStudio Server Pro with Launcher and Kubernetes, you’re probably also using our r-session-complete docker images, or perhaps the Dockerfiles on which the images are based as the basis for your own containers.

 

These container resources are provided as-is and free of charge, as a convenience to the community. We recently discovered however, that the rJava package did not work inside the container out-of-the-box and in this article we will outline the steps required to get rJava working. 

 

The error you see when you try to load rJava:

>library(rJava)
Error: package or namespace load failed for 'rJava':
  .onlaod failed in loadNamespace() for 'rJava', details:
    call: dyn.load(file, DLLpath = DLLpath, ...)
    error: unable to load shared object '/opt/R/4.0.2/lib/R/library/rJava/libs/rJava.so':
    libjvm.so: cannot open shared object file: No such file or directory

 

The resolution described here is already applied to newer container builds, but if you’re running an older version of RStudio Server Pro with Launcher and Kubernetes and need to use rJava, the steps outlined below will hopefully be of use.

 

You can find a complete example of the Dockerfile we use on GitHub

 

We won’t walk through the complete example here since we’re focused on the rJava fixes. Instead, we’ll highlight the important sections and provide the additional information required to fix the issue so that you can apply them as appropriate to your own Dockerfile. Many of the lines below will likely already exist in your Dockerfile, either exactly as they’re presented here, or perhaps combined with other commands.

 

For the rest of these steps to work, we need to set the R version. In our Dockerfile above we set this with an environment variable, so we can use the same value in the rest of the file.

 

ARG R_VERSION=4.0.2

 

Then we need to install the Java JDK. It’s important to install the JDK (Java Development Kit) rather than the JRE (Java Runtime Environment) as the JDK contains additional development tools required by rJava.

 

RUN apt-get install -y openjdk-8-jdk

 

Install the rJava package itself:

 

RUN /opt/R/${R_VERSION}/bin/R -e 'install.packages("rJava", repos="https://packagemanager.rstudio.com/cran/__linux__/bionic/latest")'

 

Configure R to use the newly installed Java version:

 

RUN /opt/R/${R_VERSION}/bin/R CMD javareconf JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64/

 

Now for the fix itself. We need to force the installed R version to use it's own ldpaths startup script when it starts inside the container.

 

RUN mkdir /etc/rstudio && echo "Path: /opt/R/${R_VERSION}\nScript: /opt/R/${R_VERSION}/lib/R/etc/ldpaths" > /etc/rstudio/r-versions

 

With that added to the Dockerfile, when the new image is built and in use, you’ll be able to launch sessions that properly allow rJava to run.

 

This last step is necessary to ensure that R starts with the appropriate LD_LIBRARY_PATH variable set. Without it, rJava won’t work in the containerised session, even if you manually set the correct LD_LIBRARY_PATH environment variable manually in R yourself.








Comments