Error when running Singularity container for R script - r

I am building a Singularity container to run a custom R script for tree segmentation using the LidR software package.
I have written the Singularity definition file as such:
Bootstrap: docker
From: ubuntu:20.04
%setup
touch test.R
touch treeSeg_dalponte2016.R
touch /home/ljeasson/R/x86_64-pc-linux-gnu-library/3.6/rgdal/libs/rgdal.so
%files
test.R
treeSeg_dalponte2016.R
/home/ljeasson/R/x86_64-pc-linux-gnu-library/3.6/rgdal/libs/rgdal.so
%post
# Disable interactivity, including region and time zone
export DEBIAN_FRONTEND="noninteractive"
export DEBCONF_NONINTERACTIVE_SEEN=true
# Update apt and install necessary libraries and repositories
apt update
apt install -y build-essential r-base-core software-properties-common dirmngr apt-transport-https lsb-release ca-certificates
add-apt-repository ppa:ubuntugis/ubuntugis-unstable
apt install -y libgdal-dev libgeos++-dev libudunits2-dev libproj-dev libx11-dev libgl1-mesa-dev libglu1-mesa-dev libfreetype6-dev libnode-dev libxt-dev libfftw3-dev
apt clean
# Install necessary R packages and dependencies
R -e "install.packages('lidR', dependencies = TRUE)"
R -e "install.packages('raster', dependencies = TRUE)"
R -e "install.packages('sf', dependencies = TRUE)"
R -e "install.packages('dplyr', dependencies = TRUE)"
R -e "install.packages('rgdal', dependencies = TRUE, repos='https://cran.rstudio.com', configure.args=c('--with-gdal-config=/opt/conda/bin/gdal-config', '--with-proj-include=/opt/conda/include', '--with-proj-lib=/opt/conda/lib', '--with-proj-share=/opt/conda/share/proj/'))"
R -e "install.packages('gdalUtils', dependencies = TRUE, repos='https://cran.rstudio.com')"
%test
#!/bin/bash
R --version
Rscript test.R
%runscript
#!/bin/sh
echo "Arguments received: $*"
Rscript treeSeg_dalponte2016.R $*
And build the container using singularity build ga_container.sif ga_container.def
The container builds without error, but when the container is run using ./ga_container <arguments>, this error always occurs:
Error: package or namespace load failed for 'rgdal' in dyn.load(file, DLLpath = DLLpath, ...):
unable to load shared object '/home/ljeasson/R/x86_64-pc-linux-gnu-library/3.6/rgdal/libs/rgdal.so':
libgdal.so.26: cannot open shared object file: No such file or directory
Execution halted
I know that the error is occuring because it cannot find the image for Rgdal, even though it seems I've attached to the container in the %setup and %files section:
%setup
touch test.R
touch treeSeg_dalponte2016.R
touch /home/ljeasson/R/x86_64-pc-linux-gnu-library/3.6/rgdal/libs/rgdal.so
%files
test.R
treeSeg_dalponte2016.R
/home/ljeasson/R/x86_64-pc-linux-gnu-library/3.6/rgdal/libs/rgdal.so
If the error is from incorrect file attachment, how do I ensure that the Rgdal (and other similar libraries) are attached correctly within the Singularity container?
Thanks in advance

This looks like an environmental issue causing the image to look at your locally installed R modules instead of using the ones installed in the image. Perhaps in your .Rprofile or R_LIBS/R_LIBS_USER. Try running with singularity run --cleanenv ..., or temporarily moving your .Rprofile if you have one, and see if that fixes it. If not, I have a few other observations.
First, the %setup block is creating root owned, empty files on the host OS if they don't exist already. An empty .so file would certainly cause problems. For the majority of cases you don't want to use %setup, as it directly modifies the host as root during sudo singularity build.
In the %files block you are copying the (potentially root owned/empty) to a path in the image that matches your home directory. Your $HOME is automatically mounted when you run/exec/shell an image, which will hide any files in the image at that location. When adding files to an image, you should always put them in a place they are unlikely to get clobbered by a mount. /opt/myapp or something similar usually works well. Additionally, test.R and treeSeg_dalponte2016.R are copied to /test.R and /treeSeg_dalponte2016.R inside the container, but relative paths are used in %runscript and %test. Singularity run/exec will attempt to run from the first path that exists in the container: $PWD (implicitly mounted, but this can fail silently), then $HOME (also implicitly mounted and can fail silently), then /. You can use singularity --verbose run ... to see if anything isn't being mounted correctly and add echo $PWD to %runscript to see where it's running from.
In %post when you install the rgdal package, you specify several paths with /opt/conda/... but conda is not installed or configured in the image. I'm not familiar with rgdal, so don't know if that would cause problems or not though.

Related

RStudio in KDE Neon

How can I install RSudio in KDE Neon I have downloaded rstudio-1.4.1106-amd64.deb and I ran sudo dpkg -i rstudio-1.4.1106-amd64.deb in terminal, I got following:
dpkg: dependency problems prevent configuration of rstudio: rstudio depends on libclang-dev; however: Package libclang-dev is not installed. rstudio depends on libpq5; however: Package libpq5 is not installed.
dpkg: error processing package rstudio (--install): dependency problems - leaving unconfigured Processing triggers for desktop-file-utils (0.24-1ubuntu3) ... Processing triggers for mime-support (3.64ubuntu1) ... Processing triggers for hicolor-icon-theme (0.17-2) ... Processing triggers for shared-mime-info (1.15-1) ... Errors were encountered while processing:rstudio
There are some packages required that are not installed by default. Please try the following, which worked for me on Kubuntu 20.04 recently:
URL='https://download1.rstudio.org/desktop/bionic/amd64/rstudio-1.4.1106-amd64.deb'; FILE=`mktemp`; wget "$URL" -qO $FILE && sudo dpkg -i $FILE; rm $FILE
sudo apt --fix-broken install
First I had to find and fix broken packages:
apt --fix-broken install
Then the package installed correctly for me:
sudo dpkg -i rstudio-2022.02.3-492-amd64.deb

Using docker buildkit caching with R-packages

I'm trying to use the docker buildkit approach to caching packages to speed up adding packages to docker containers. I learned about it from the instructions for both python and apt-get packages and useful Stackexchange answer on caching python packages while building Docker. For Python and apt-get I am able to get this to work, but I can't get it to work for R packages.
In a Dockerfile for Python I'm able to change:
RUN pip install -r requirements.txt
to (and the comment looking bit at the top of the Dockerfile is needed)
# syntax=docker/dockerfile:experimental
RUN --mount=type=cache,target=/root/.cache/pip pip install -r requirements.txt
And then when I add a package to the requirements.txt file, rather than re-downloading and building the packages, pip is able to re-use all the work it has done. So buildkit cache mounts add a level of caching beyond the image layers of docker. It's a massive timesaver. I'm hoping to set up something similar for r-packages.
Here is what I've tried that works for apt-get but not r-packges. I've also tried with the install2.r script.
# syntax=docker/dockerfile:experimental
FROM rocker/tidyverse
RUN rm -f /etc/apt/apt.conf.d/docker-clean; echo 'Binary::apt::APT::Keep-Downloaded-Packages "true";' > /etc/apt/apt.conf.d/keep-cache
RUN --mount=type=cache,target=/var/cache/apt --mount=type=cache,target=/var/lib/apt \
apt update && apt install -y gcc \
zsh \
vim
COPY ./requirements.R .
RUN --mount=type=cache,target=/usr/local/lib/R/site-library Rscript ./requirements.R
I think I don't understand:
How buildkit works. Does it do the building of containers inside a container? ie the cache path is on the 'build container'?
What one needs to specify as the target for R to notice that it already has downloaded (and possibly built).
I suspect that it has something to do with the keep.source command when installing an R package, as discussed in this question

Install packages from source failing - Dockerfile

I am trying to install some packages from source (including package that I have created that installed fine with R console or even when R CMD install.
However, while building docker-image using a docker file. I get this error with for this line in the docker file
RUN R -e 'install.packages("RcppDIUtilsPackage_1.0.tar.gz",repos=NULL,type="source")'
I also tried many other commands including R CMD INSTALL all work fine to install the package except within the docker image build.
Here is the error i am encountering.
Installing package into ‘/usr/local/lib/R/site-library’
(as ‘lib’ is unspecified)
Warning: invalid package ‘RcppDIUtilsPackage_1.0.tar.gz’
Error: ERROR: no packages specified
Warning message:
In install.packages("RcppDIUtilsPackage_1.0.tar.gz", repos = NULL, :
installation of package ‘RcppDIUtilsPackage_1.0.tar.gz’ had non-zero exit status
Thanks!!
Edit: The Dockerfile
FROM rocker/r-ver:3.4.4
WORKDIR /home/ubuntu/projects/DService
RUN apt-get update -qq && apt-get install -y \
libssl-dev \
libcurl4-gnutls-dev
RUN R -e "install.packages('plumber')"
RUN R -e "install.packages('Rcpp')"
RUN R -e 'install.packages("RcppDIUtilsPackage_1.0.tar.gz",repos=NULL,type="source")'
COPY / /
EXPOSE 8000
CMD ["Rscript", "DService.R"]
command: sudo docker build --no-cache -t dservice-docker-image .
This is an indirect solution to your problem, because I was not able to resolve the same issue.
The root of the issue may have something to do with the host environment that created the Docker image from the Dockerfile. Specifically, the R instance that is spun up to install the local packages may not being able to access the path to where your local packages are stored.
The solution for me was to just avoid local packages. Move any local repositories to remote repositories, and reference them in the Dockerfile instead. e.g.
RUN R -e "devtools::install_github('dmanuge/shinyFilesWidget') ; system('echo 14')"
After that, rebuilt your Docker image and run it accordingly. While this is not a direct solution, I reached the critical threshold of debugging and needed to move on. :)

Issues installing mxnet GPU R package for Amazon deep learning AMI

I am having trouble installing mxnet GPU for R on Amazon deep learning linux AMI. The environment variables are such a mess that it’s a nightmare for any non-expert sys-admin to figure out.
Step 1: install the ridiculous amount of missing/broken programs and R packages
sudo yum install R
sudo yum install libxml2-devel
sudo yum install cairo-devel
sudo yum install giflib-devel
sudo yum install libXt-devel
sudo R
install.packages("devtools")
library(devtools)
install_github("igraph/rigraph")
install.packages(‘DiagrammeR’)
install.packages(‘roxygen2’)
install.packages(‘rgexf’)
install.packages(‘influenceR’)
install.packages(‘Cairo’)
install.packages(“imager”)
Step 2: edit the config.mk file
cd /src/mxnet
cp make/config.mk .
echo "USE_BLAS=openblas" >>config.mk
echo "ADD_CFLAGS += -I/usr/include/openblas" >>config.mk
echo "ADD_LDFLAGS += -lopencv_core -lopencv_imgproc -lopencv_imgcodecs" >>config.mk
echo "USE_CUDA=1" >>config.mk
echo "USE_CUDA_PATH=/usr/local/cuda" >>config.mk
echo "USE_CUDNN=1" >>config.mk
*note even though the USE_CUDA_PATH is set, it STILL cannot find libcudart.so and needs to be linked in the make command (shown later)
Step 3: make new config file so make command can find libcudart.so
/etc/ld.so.conf.d/cuda.conf
add /usr/local/cuda-8.0/lib64
sudo ldconfig
note this was posted by nvidia but does absolutely nothing to help the make rpkg
Step 4: set up R directories
Rscript -e "install.packages('devtools', repo = 'https://cran.rstudio.com')"
cd R-package
Rscript -e "library(devtools); library(methods); options(repos=c(CRAN='https://cran.rstudio.com'));
install_deps(dependencies = TRUE)"
cd ..
step 5: make
cd /src/mxnet
sudo make -j8
Result:
make CXX=g++ DEPS_PATH=/home/ec2-user/src/mxnet/deps -C /home/ec2-user/src/mxnet/ps-lite ps
cd /home/ec2-user/src/mxnet/dmlc-core; make libdmlc.a USE_SSE=1 config=/home/ec2-user/src/mxnet/config.mk; cd /home/ec2-user/src/mxnet
make[1]: Entering directory /home/ec2-user/src/mxnet/dmlc-core'
make[1]:libdmlc.a' is up to date.
make[1]: Leaving directory /home/ec2-user/src/mxnet/dmlc-core'
make[1]: Entering directory/home/ec2-user/src/mxnet/ps-lite'
make[1]: Nothing to be done for ps'.
make[1]: Leaving directory/home/ec2-user/src/mxnet/ps-lite'
ar crv lib/libmxnet.a
*note, even when changing the config.mk file, the make command always returns ‘nothing to update’
Step 6: attempt to make rpkg
Cd /src/mxnet
Sudo make rpkg
Error:
Error: package or namespace load failed for ‘mxnet’:
.onLoad failed in loadNamespace() for 'mxnet', details:
call: dyn.load(file, DLLpath = DLLpath, ...)
error: unable to load shared object '/usr/lib64/R/library/mxnet/libs/libmxnet.so':
libcudart.so.8.0: cannot open shared object file: No such file or directory
Error: loading failed
Execution halted
ERROR: loading failed
So it’s looking in a location that doesn’t exist: /usr/lib64/R/library/mxnet/libs/
When the file actually lives:
/home/ec2-user/src/mxnet/R-package/inst/libs/libmxnet.so
or
/home/ec2-user/src/mxnet/lib/libmxnet.so
What I’ve tried so far:
sudo LD_LIBRARY_PATH=/usr/local/cuda/lib64 make rpkg
This will fix the missing libcudart.so.8.0 issue but it is simply replace with:
libmklml_intel.so: cannot open shared object file: No such file or directory as well as the original ‘cannot find libmxnet.so
Also tried:
1. actually creating directories (/usr/lib64/R/library/mxnet/libs/) and then copying libmxnet.so there
Result: same error
adding /home/ec2-user/src/mxnet/R-package/inst/libs/ to the make command
sudo LD_LIBRARY_PATH=/home/ec2-user/src/mxnet/R-package/inst/libs make rpkg
Result: same error
a ridiculous amount of environment labels all of which failed:
export MXNET_HOME=/usr/lib64/R/library/mxnet/libs/
export MXNET_HOME=/usr/lib64/R/library/mxnet/libs/libmxnet.so
sudo ldconfig /usr/local/cuda/lib64
sudo ln -s /usr/lib64/R/library/mxnet/libs /usr/lib
sudo ln -s /usr/lib64/R/library/mxnet/libs/libmxnet.so /usr/lib
sudo ln -s /usr/local/lib/libmklml_intel.so /usr/lib
sudo ln -s /usr/local/lib/libiomp5.so /usr/lib
sudo ln -s /usr/local /usr/lib
export LD_LIBRARY_PATH=/usr/local/cuda-8.0/lib64/libcudart.so.8.0
export LD_LIBRARY_PATH=/usr/lib64/R/library/mxnet/libs/libmxnet.so /usr/lib
export LD_LIBRARY_PATH=/usr/local/cuda-8.0/targets/x86_64-linux/lib/:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=/usr/local/cuda-8.0/lib64/libcudart.so.8.0
In all ONE of these worked, because I briefly got mxnet R package working before it fell apart again. I’ve dropped 50+ hours into this installation, which, frankly is ridiculous. Tougher to install the software then it is to program an actual net....
I don’t have 5+ years of linux sys admin knowledge so if you’d like please be a bit more helpful then ‘fix environment variables.’ I can tell that’s obviously what’s wrong yet have no idea what ‘fix environment variables’ entails.
To top it off, even after successful install of the R package, it STILL won’t work until setting Rstudio server’s config file to: rsession-ld-library-path=/opt/local/lib:/usr/local/cuda/lib64
Did you try the following when running any sudo commands.
sudo -E make -j8
This means that it will preserve the env variables when running as superuser. You shouldn't have to add a new config file for the make to find the libraries. Just preserving the env variables using the above command should be enough.

adding R package for installation from Github in Dockerfile

How must I add installation instruction for a R package using github with my Dockerfile.
The usual command in R environment is:
devtools::install_github("smach/rmiscutils")
But no success so far. Tried to add github repo to installation instructions:
RUN install2.r --error \
-r 'http://cran.rstudio.com' \
-r 'http://github.com/smach/rmiscutils'
But I get an error:
error in download. Status was '404 Not found'
Maybe using vanilla R call but can't figure the command.
Any Hint?
Try this:
RUN installGithub.r smach/rmiscutils \
&& rm -rf /tmp/downloaded_packages/
A good practice is to remove all downloaded packages to reduce image size.
Here added command when installing from Bioconductor (in case someone ends here searching for help)
RUN install2.r -r http://bioconductor.org/packages/3.0/bioc --deps TRUE \
phyloseq \
&& rm -rf /tmp/downloaded_packages/

Resources