unable to install R packages in Docker container

Viewed 32

** UPDATED QUESTION **

I am running an R Studio server docker container and when login cannot import libraries (e.g. rvest) I installed in R studio with all dependecies installed.

docker-compose.yml


    version: '3.9'
    services:
    
      rstudio:
        build: ./docker/rstudio
        container_name: etl
        environment:
          - PASSWORD=yourpassword
        ports:
          - 8787:8787

Dockerfile


    FROM rocker/rstudio
    
    RUN apt-get clean all && \
      apt-get update && \
      apt-get upgrade -y && \
      apt-get install -y \
        python3-pip \
      && apt-get clean all && \
      apt-get purge && \
      rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
    
    RUN /usr/local/lib/R/bin/R -e 'install.packages("ODBC", repos="https://packagemanager.rstudio.com/cran/__linux__/focal/latest")' && \
       /usr/local/lib/R/bin/R -e 'install.packages("RODBC", repos="https://packagemanager.rstudio.com/cran/__linux__/focal/latest")' && \
       /usr/local/lib/R/bin/R -e 'install.packages("DBI", repos="https://packagemanager.rstudio.com/cran/__linux__/focal/latest")' && \
       /usr/local/lib/R/bin/R -e 'install.packages("rvest", repos="https://packagemanager.rstudio.com/cran/__linux__/focal/latest")'

Error


    > library(rvest)
    Error: package or namespace load failed for ‘rvest’ in dyn.load(file, DLLpath = DLLpath, ...):
     unable to load shared object '/usr/local/lib/R/site-library/xml2/libs/xml2.so':
      libxml2.so.2: cannot open shared object file: No such file or directory
    

The file '/usr/local/lib/R/site-library/xml2/libs/xml2.so' actually exists, but #2 not : '/usr/local/lib/R/site-library/xml2/libs/xml2.so.2' file exists

1 Answers

The first error message is key:

unable to load shared object '/usr/local/lib/R/site-library/xml2/libs/xml2.so

These RSPM packages do not install system dependencies (as they can't, they are simple tar.gz archives; the site offers some help). You could either just nstall the pre-made binaries from Ubuntu (r-cran-{odbc,rodbc,dbi,rvest}) or take care of the system-dependencies via eg

apt install libxml2 libodbc1

taking care of the XML and ODBC libraries.

Edit: In response to your edited question look at this search for file libxml2.so.2 at packages.ubuntu.com pointing you to Ubuntu package libxml2. Which is exactly what I wrote earlier: you need sudo apt install libxml2 because the CRAN (source) package does not do it for you (and cannot). See r2u for an alternative.

Related