javardockerdockerfilerjava

Installing rJava package with Docker


I am trying to install the 'rJava' package in my RStudio docker image using my Dockerfile:

FROM rocker/tidyverse:3.6.1

RUN mkdir -p /rstudio
RUN mkdir -p /rscripts

RUN apt-get update && \
    apt-get install -y openjdk-11-jdk && \
    apt-get install -y liblzma-dev && \
    apt-get install -y libbz2-dev

RUN R -e "install.packages(c('rJava','mailR'))"

Following this SO post I added the above part with the apt-get commands but still I get the same error:

java libs : '-L/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/amd64/server -ljvm' checking whether Java run-time works... ./configure: line 3766: /usr/bin/java: No such file or directory no configure: error: Java interpreter '/usr/bin/java' does not work ERROR: configuration failed for package ‘rJava’

So there is a missing file or directory but I don't know what changes I should make.

[EDIT 1]:

So, following Dirk's suggestion, I entered in the rstudio container and ran apt-get install r-cran-rjava which seems to work.
But when I install rJava package I get a new error:

error: Cannot compile a simple JNI program. Make sure you have Java Development Kit installed and correctly registered in R. If in doubt, re-run "R CMD javareconf" as root.

I tried to enter again in the container and run R CMD javareconf but that did not change the error. I also tried the following commands found on this article:

sudo apt-get install default-jre
sudo apt-get install default-jdk

But I still get:

Cannot compile a simple JNI program.


Solution

  • The reason why Dirk's answer doesn't work for you is because you're using rocker/tidyverse as a base image instead of the r-base that Dirk is using.

    In the rocker/tidyverse documentation on Docker Hub they are discouraging the use of apt install --no-install-recommends -y r-cran-rjava:

    do not use apt-get install r-cran-* to install R packages on this stack. The requested R version and all R packages are installed from source in the version-stable stack. Installing R packages from apt (e.g. the r-cran-* packages) will install the version of R and versions of the packages that were built for the stable debian release (e.g. debian:stretch), giving you a second version of R and different packages. Please install R packages from source using the install.packages() R function (or the install2.r script), and use apt only to install necessary system libraries (e.g. libxml2). If you would prefer to install only the latest verions of packages from pre-built binaries using apt-get, consider using the r-base stack instead.

    Since rJava is supposed to be installed with apt install r-cran-rjava, but the rocker/... base images explicitly tell you not to do that, it looks like you will not be able to use rJava with any of the rocker/... base images..