如何将R包安装到Ubuntu的Docker容器中。

huangapple go评论72阅读模式
英文:

How to install R packages to docker container from ubuntu

问题

I see your Dockerfile and the issue you're facing. It appears that the installation of R packages within the Dockerfile is not working as expected. Specifically, the error message suggests that the 'tidyverse' package is not found when you run the container.

One possible issue could be that the 'tidyverse' package is not being installed due to a missing repository configuration. You might want to add the following line to your Dockerfile before installing R packages:

RUN Rscript -e "install.packages('tidyverse', repos='https://cloud.r-project.org')"

This line explicitly specifies the CRAN repository where R should look for the 'tidyverse' package.

After making this change, rebuild your Docker image, and try running the container again. It should now install the 'tidyverse' package successfully.

Remember to make similar adjustments for other R packages if needed, specifying the appropriate repositories if they are not found by default.

英文:

I am brand new with Docker. I'm trying create a container that will run a Snakemake pipeline. This pipeline will contain R and Python scripts. Eventually, I'll need to add in a conda environment and clone and build a git hub repo. But for now, I'm just trying to get my python and R packages installed correctly.

I started off following the advice from this question to create an environment that has both Python and R.

This is my Dockerfile so far.

FROM ubuntu:latest

ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get install -y --no-install-recommends build-essential r-base python3.9 python3-pip python3-setuptools python3-dev

WORKDIR /app

RUN pip3 install biopython
RUN pip3 install pandas
RUN pip3 install numpy


RUN Rscript -e "install.packages('tidyverse')"
RUN Rscript -e "install.packages('dplyr')"
RUN Rscript -e "install.packages('testit')"
RUN Rscript -e "install.packages('stringr')"
RUN Rscript -e "install.packages('BiocParallel')"
RUN Rscript -e "install.packages('MPRAnalyze')"

COPY test.R /app

CMD ["Rscript", "test.R"]

The test.R script contains this,

library(dplyr)
print("dplyr working!")
library(testit)
print("testit working!")
library(stringr)
print("stringr working!")
library(tidyverse)
print("tidyverse working!")
library(BiocParallel)
print("BiocParallel working!")
library(MPRAnalyze)
print("MPRAnalyze working!")

When I run docker build, it seems to work just fine. Everything installs.

 => [internal] load build definition from Dockerfile                       0.0s
 => => transferring dockerfile: 706B                                       0.0s
 => [internal] load .dockerignore                                          0.0s
 => => transferring context: 2B                                            0.0s
 => [internal] load metadata for docker.io/library/ubuntu:latest           1.3s
 => [ 1/13] FROM docker.io/library/ubuntu:latest@sha256:dfd64a3b4296d8c9b  0.0s
 => [internal] load build context                                          0.0s
 => => transferring context: 65B                                           0.0s
 => CACHED [ 2/13] RUN apt-get update && apt-get install -y --no-install-  0.0s
 => CACHED [ 3/13] WORKDIR /app                                            0.0s
 => CACHED [ 4/13] RUN pip3 install biopython                              0.0s
 => CACHED [ 5/13] RUN pip3 install pandas                                 0.0s
 => CACHED [ 6/13] RUN pip3 install numpy                                  0.0s
 => [ 7/13] RUN Rscript -e "install.packages('tidyverse')"               893.1s
 => [ 8/13] RUN Rscript -e "install.packages('dplyr')"                    12.1s
 => [ 9/13] RUN Rscript -e "install.packages('testit')"                    2.8s 
 => [10/13] RUN Rscript -e "install.packages('stringr')"                   4.0s 
 => [11/13] RUN Rscript -e "install.packages('BiocParallel')"              1.4s 
 => [12/13] RUN Rscript -e "install.packages('MPRAnalyze')"                1.4s 
 => [13/13] COPY test.R /app                                               0.0s 
 => exporting to image                                                     1.3s 
 => => exporting layers                                                    1.3s 
 => => writing image sha256:328a4d8297eb16cef5f415b1067a1592d538db3ce15f9  0.0s
 => => naming to docker.io/library/wut        

But when I try to run docker run it doesn't look like it is working.

Attaching package: 'dplyr'

The following objects are masked from 'package:stats':

    filter, lag

The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union

[1] "dplyr working!"
[1] "testit working!"
[1] "stringr working!"
Error in library(tidyverse) : there is no package called 'tidyverse'
Execution halted

Any ideas as to what is going on? I'm open to new approaches to solving this, as long as I can eventually install a conda environment and clone the github repo (linked above).

答案1

得分: 1

我解决这个问题的方法是使用你的Dockerfile构建镜像。所有的包都似乎成功安装,正如你在上面的输出中指出的。

当我启动容器时,我注意到tidyverse包丢失。当我尝试进行交互式安装时,问题显现:

➜ docker run -it test bash --login
root@aee05f0e225e:/app# R

R version 4.1.2 (2021-11-01) -- "Bird Hippie"

...

> library(tidyverse)
Error in library(tidyverse) : 没有名为 'tidyverse' 的包
> install.packages("tidyverse")
正在将软件包安装到 '/usr/local/lib/R/site-library'
(as 'lib' is unspecified)
also installing the dependencies 'gargle', 'curl', 'ids', 'openssl', 'systemfonts', 'textshaping', 'googledrive', 'googlesheets4', 'httr', 'ragg', 'rvest', 'xml2'

...

* 正在安装 *source* 包 'curl' ...
** 包 'curl' 解压缩成功,MD5 校验通过
** 使用分阶段安装
Using PKG_CFLAGS=
Using PKG_LIBS=-lcurl
--------------------------- [ANTICONF] --------------------------------
配置失败,因为找不到 libcurl。请尝试安装:
 * deb: libcurl4-openssl-dev (Debian, Ubuntu, 等等)
 * rpm: libcurl-devel (Fedora, CentOS, RHEL)
如果已安装 libcurl,请检查 'pkg-config' 是否在您的
PATH 中,并且 PKG_CONFIG_PATH 包含 libcurl.pc 文件。如果 pkg-config
不可用,您可以通过以下方式手动设置 INCLUDE_DIR 和 LIB_DIR:
R CMD INSTALL --configure-vars='INCLUDE_DIR=... LIB_DIR=...'
-------------------------- [ERROR MESSAGE] ---------------------------
<stdin>:1:10: fatal error: curl/curl.h: No such file or directory
compilation terminated.
--------------------------------------------------------------------
ERROR: 为包 'curl' 配置失败
* 正在移除 '/usr/local/lib/R/site-library/curl'
* 正在安装 *source* 包 'openssl' ...
** 包 'openssl' 解压缩成功,MD5 校验通过
** 使用分阶段安装
Using PKG_CFLAGS=
--------------------------- [ANTICONF] --------------------------------
配置失败,因为找不到 openssl。请尝试安装:
 * deb: libssl-dev (Debian, Ubuntu, 等等)
 * rpm: openssl-devel (Fedora, CentOS, RHEL)
 * csw: libssl_dev (Solaris)
 * brew: openssl (Mac OSX)
如果已安装 openssl,请检查 'pkg-config' 是否在您的
PATH 中,并且 PKG_CONFIG_PATH 包含 openssl.pc 文件。如果 pkg-config
不可用,您可以通过以下方式手动设置 INCLUDE_DIR 和 LIB_DIR:
R CMD INSTALL --configure-vars='INCLUDE_DIR=... LIB_DIR=...'
-------------------------- [ERROR MESSAGE] ---------------------------
tools/version.c:1:10: fatal error: openssl/opensslv.h: No such file or directory
    1 | #include <openssl/opensslv.h>
      |          ^~~~~~~~~~~~~~~~~~~~
compilation terminated.
--------------------------------------------------------------------
ERROR: 为包 'openssl' 配置失败
* 正在移除 '/usr/local/lib/R/site-library/openssl'
* 正在安装 *source* 包 'systemfonts' ...
** 包 'systemfonts' 解压缩成功,MD5 校验通过
** 使用分阶段安装
Using PKG_CFLAGS=
Using PKG_LIBS=-lfontconfig -lfreetype
--------------------------- [ANTICONF] --------------------------------
配置无法找到 fontconfig freetype2 库。请尝试安装:
 * deb: libfontconfig1-dev (Debian, Ubuntu, 等等)
 * rpm: fontconfig-devel (Fedora, EPEL)
 * csw: fontconfig_dev (Solaris)
 * brew: freetype (OSX)
如果已安装 fontconfig freetype2,请检查 'pkg-config' 是否在您的
PATH 中,并且 PKG_CONFIG_PATH 包含 fontconfig freetype2.pc 文件。如果 pkg-config
不可用,您可以通过以下方式手动设置 INCLUDE_DIR 和 LIB_DIR:
R CMD INSTALL --configure-vars='INCLUDE_DIR=... LIB_DIR=...'
-------------------------- [ERROR MESSAGE] ---------------------------
<stdin>:1:10: fatal error: fontconfig/fontconfig.h: No such file or directory
compilation terminated.
--------------------------------------------------------------------
ERROR: 为包 'systemfonts' 配置失败
* 正在移除 '/usr/local/lib/R/site-library/systemfonts'
* 正在安装 *source* 包 'xml2' ...
** 包 'xml2' 解压缩成功,MD5 校验通过
** 使用分阶段安装
Using PKG_CFLAGS=
Using PKG_LIBS=-lxml2
------------------------- ANTICONF ERROR ---------------------------
配置失败,因为找不到 libxml-2.0。请尝试安装:
 * deb: libxml2-dev (Debian, Ubuntu, 等等)
 * rpm: libxml2-devel (Fedora, CentOS, RHEL)
 * csw: libxml2_dev (Solaris)
如果已安装 libxml-2.0,请检查 'pkg-config' 是否在您的
PATH 中,并且 PKG_CONFIG_PATH 包含 libxml-2.0.pc 文件。如果 pkg-config
不可用,您可以通过以下方式手动设置 INCLUDE_DIR 和 LIB_DIR:
R CMD INSTALL --configure-vars='INCLUDE_DIR=... LIB_DIR=...'
--------------------------------------------------------------------
ERROR: 为包 'xml2' 配置失败
* 正在移除 '/usr/local/lib/R/site-library/xml2'
ERROR: 'ids' 的依赖关系 'openssl' 不可用
* 正在移除 '/usr/local/lib/R/site-library/ids'
ERROR: 'textshaping' 的依赖关系 'systemfonts' 不可用
* 正在移除 '/usr/local/lib/R/site-library/textshaping'
ERROR: 'httr' 的依赖关系 'curl''openssl' 不可用
* 正在移除 '/usr/local/lib/R/site-library/httr'
ERROR: 'gargle' 的依赖关系 'httr''openssl' 不可用
* 正在移除 '/usr/local/lib/R/site-library/gargle'
ERROR: 'ragg' 的依赖关系 'systemfonts''textshaping' 不可用
* 正在移除 '/usr/local/lib/R/site-library/rag

<details>
<summary>英文:</summary>

The way I approached this problem was taking your `Dockerfile` and building the image from it. All packages appear to install successfully, as you also indicate in the above output.

When I spin up a container, I can notice that the `tidyverse` package is missing. When I try installing it interactively, the problem is revealed:

```bash
➜ docker run -it test bash --login
root@aee05f0e225e:/app# R

R version 4.1.2 (2021-11-01) -- &quot;Bird Hippie&quot;

...

&gt; library(tidyverse)
Error in library(tidyverse) : there is no package called &#39;tidyverse&#39;
&gt; install.packages(&quot;tidyverse&quot;)
Installing package into &#39;/usr/local/lib/R/site-library&#39;
(as &#39;lib&#39; is unspecified)
also installing the dependencies &#39;gargle&#39;, &#39;curl&#39;, &#39;ids&#39;, &#39;openssl&#39;, &#39;systemfonts&#39;, &#39;textshaping&#39;, &#39;googledrive&#39;, &#39;googlesheets4&#39;, &#39;httr&#39;, &#39;ragg&#39;, &#39;rvest&#39;, &#39;xml2&#39;

...

* installing *source* package &#39;curl&#39; ...
** package &#39;curl&#39; successfully unpacked and MD5 sums checked
** using staged installation
Using PKG_CFLAGS=
Using PKG_LIBS=-lcurl
--------------------------- [ANTICONF] --------------------------------
Configuration failed because libcurl was not found. Try installing:
 * deb: libcurl4-openssl-dev (Debian, Ubuntu, etc)
 * rpm: libcurl-devel (Fedora, CentOS, RHEL)
If libcurl is already installed, check that &#39;pkg-config&#39; is in your
PATH and PKG_CONFIG_PATH contains a libcurl.pc file. If pkg-config
is unavailable you can set INCLUDE_DIR and LIB_DIR manually via:
R CMD INSTALL --configure-vars=&#39;INCLUDE_DIR=... LIB_DIR=...&#39;
-------------------------- [ERROR MESSAGE] ---------------------------
&lt;stdin&gt;:1:10: fatal error: curl/curl.h: No such file or directory
compilation terminated.
--------------------------------------------------------------------
ERROR: configuration failed for package &#39;curl&#39;
* removing &#39;/usr/local/lib/R/site-library/curl&#39;
* installing *source* package &#39;openssl&#39; ...
** package &#39;openssl&#39; successfully unpacked and MD5 sums checked
** using staged installation
Using PKG_CFLAGS=
--------------------------- [ANTICONF] --------------------------------
Configuration failed because openssl was not found. Try installing:
 * deb: libssl-dev (Debian, Ubuntu, etc)
 * rpm: openssl-devel (Fedora, CentOS, RHEL)
 * csw: libssl_dev (Solaris)
 * brew: openssl (Mac OSX)
If openssl is already installed, check that &#39;pkg-config&#39; is in your
PATH and PKG_CONFIG_PATH contains a openssl.pc file. If pkg-config
is unavailable you can set INCLUDE_DIR and LIB_DIR manually via:
R CMD INSTALL --configure-vars=&#39;INCLUDE_DIR=... LIB_DIR=...&#39;
-------------------------- [ERROR MESSAGE] ---------------------------
tools/version.c:1:10: fatal error: openssl/opensslv.h: No such file or directory
    1 | #include &lt;openssl/opensslv.h&gt;
      |          ^~~~~~~~~~~~~~~~~~~~
compilation terminated.
--------------------------------------------------------------------
ERROR: configuration failed for package &#39;openssl&#39;
* removing &#39;/usr/local/lib/R/site-library/openssl&#39;
* installing *source* package &#39;systemfonts&#39; ...
** package &#39;systemfonts&#39; successfully unpacked and MD5 sums checked
** using staged installation
Using PKG_CFLAGS=
Using PKG_LIBS=-lfontconfig -lfreetype
--------------------------- [ANTICONF] --------------------------------
Configuration failed to find the fontconfig freetype2 library. Try installing:
 * deb: libfontconfig1-dev (Debian, Ubuntu, etc)
 * rpm: fontconfig-devel (Fedora, EPEL)
 * csw: fontconfig_dev (Solaris)
 * brew: freetype (OSX)
If fontconfig freetype2 is already installed, check that &#39;pkg-config&#39; is in your
PATH and PKG_CONFIG_PATH contains a fontconfig freetype2.pc file. If pkg-config
is unavailable you can set INCLUDE_DIR and LIB_DIR manually via:
R CMD INSTALL --configure-vars=&#39;INCLUDE_DIR=... LIB_DIR=...&#39;
-------------------------- [ERROR MESSAGE] ---------------------------
&lt;stdin&gt;:1:10: fatal error: fontconfig/fontconfig.h: No such file or directory
compilation terminated.
--------------------------------------------------------------------
ERROR: configuration failed for package &#39;systemfonts&#39;
* removing &#39;/usr/local/lib/R/site-library/systemfonts&#39;
* installing *source* package &#39;xml2&#39; ...
** package &#39;xml2&#39; successfully unpacked and MD5 sums checked
** using staged installation
Using PKG_CFLAGS=
Using PKG_LIBS=-lxml2
------------------------- ANTICONF ERROR ---------------------------
Configuration failed because libxml-2.0 was not found. Try installing:
 * deb: libxml2-dev (Debian, Ubuntu, etc)
 * rpm: libxml2-devel (Fedora, CentOS, RHEL)
 * csw: libxml2_dev (Solaris)
If libxml-2.0 is already installed, check that &#39;pkg-config&#39; is in your
PATH and PKG_CONFIG_PATH contains a libxml-2.0.pc file. If pkg-config
is unavailable you can set INCLUDE_DIR and LIB_DIR manually via:
R CMD INSTALL --configure-vars=&#39;INCLUDE_DIR=... LIB_DIR=...&#39;
--------------------------------------------------------------------
ERROR: configuration failed for package &#39;xml2&#39;
* removing &#39;/usr/local/lib/R/site-library/xml2&#39;
ERROR: dependency &#39;openssl&#39; is not available for package &#39;ids&#39;
* removing &#39;/usr/local/lib/R/site-library/ids&#39;
ERROR: dependency &#39;systemfonts&#39; is not available for package &#39;textshaping&#39;
* removing &#39;/usr/local/lib/R/site-library/textshaping&#39;
ERROR: dependencies &#39;curl&#39;, &#39;openssl&#39; are not available for package &#39;httr&#39;
* removing &#39;/usr/local/lib/R/site-library/httr&#39;
ERROR: dependencies &#39;httr&#39;, &#39;openssl&#39; are not available for package &#39;gargle&#39;
* removing &#39;/usr/local/lib/R/site-library/gargle&#39;
ERROR: dependencies &#39;systemfonts&#39;, &#39;textshaping&#39; are not available for package &#39;ragg&#39;
* removing &#39;/usr/local/lib/R/site-library/ragg&#39;
ERROR: dependencies &#39;httr&#39;, &#39;xml2&#39; are not available for package &#39;rvest&#39;
* removing &#39;/usr/local/lib/R/site-library/rvest&#39;
ERROR: dependencies &#39;gargle&#39;, &#39;httr&#39; are not available for package &#39;googledrive&#39;
* removing &#39;/usr/local/lib/R/site-library/googledrive&#39;
ERROR: dependencies &#39;curl&#39;, &#39;gargle&#39;, &#39;googledrive&#39;, &#39;httr&#39;, &#39;ids&#39; are not available for package &#39;googlesheets4&#39;
* removing &#39;/usr/local/lib/R/site-library/googlesheets4&#39;
ERROR: dependencies &#39;googledrive&#39;, &#39;googlesheets4&#39;, &#39;httr&#39;, &#39;ragg&#39;, &#39;rvest&#39;, &#39;xml2&#39; are not available for package &#39;tidyverse&#39;
* removing &#39;/usr/local/lib/R/site-library/tidyverse&#39;

The downloaded source packages are in
	&#39;/tmp/RtmpUztchF/downloaded_packages&#39;
There were 13 warnings (use warnings() to see them)

In short, you will have to install some extra system libraries such as libcurl4-openssl-dev, libssl-dev, libfontconfig1-dev, libxml2-dev

or

install it from the Ubuntu repository using

apt-get install -y r-cran-tidyverse

For the latter option inspect that the version you get is the one you are after (if you care about such things).

root@aee05f0e225e:/app# apt-get install -y r-cran-tidyverse

...

Processing triggers for libc-bin (2.35-0ubuntu3.1) ...
Processing triggers for fontconfig (2.13.1-4.2ubuntu5) ...
root@aee05f0e225e:/app# R

&gt; library(tidyverse)
-- Attaching packages --------------------------------------------------------------------------------------------------------------------------------- tidyverse 1.3.1 --
v ggplot2 3.4.2     v purrr   1.0.1
v tibble  3.2.1     v dplyr   1.1.2
v tidyr   1.3.0     v stringr 1.5.0
v readr   2.1.4     v forcats 1.0.0
-- Conflicts ------------------------------------------------------------------------------------------------------------------------------------ tidyverse_conflicts() --
x dplyr::filter() masks stats::filter()
x dplyr::lag()    masks stats::lag()

huangapple
  • 本文由 发表于 2023年5月7日 13:18:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76192294.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定