You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

构建r-text环境的Singularity容器时遭遇依赖问题求助

解决Singularity构建r-text环境的R依赖问题

问题场景

在Ubuntu系统下使用Singularity构建r-text环境时,执行R包安装步骤出现未满足依赖错误,导致构建中断。

错误输出

+ apt-get install -y --no-install-recommends r-base=4.1.3* r-base-core=4.1.3* r-base-dev=4.1.3* r-recommended=4.1.3* r-base-html=4.1.3* r-doc-html=4.1.3* libcurl4-openssl-dev libssl-dev libxml2-dev libcairo2-dev libxt-dev libopenblas-dev
Reading package lists... Done
Building dependency tree
Reading state information... Done
Selected version '4.1.3-1.2004.0' (CRAN:20.04/focal-cran40 [all]) for 'r-base'
Selected version '4.1.3-1.2004.0' (CRAN:20.04/focal-cran40 [amd64]) for 'r-base-core'
Selected version '4.1.3-1.2004.0' (CRAN:20.04/focal-cran40 [all]) for 'r-base-dev'
Selected version '4.1.3-1.2004.0' (CRAN:20.04/focal-cran40 [all]) for 'r-recommended'
Selected version '4.1.3-1.2004.0' (CRAN:20.04/focal-cran40 [all]) for 'r-base-html'
Selected version '4.1.3-1.2004.0' (CRAN:20.04/focal-cran40 [all]) for 'r-doc-html'
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:

The following packages have unmet dependencies:
 r-recommended : Depends: r-cran-cluster (>= 1.9.6-2) but it is not going to be installed
                 Depends: r-cran-lattice (>= 0.10.11) but it is not going to be installed
                 Depends: r-cran-mgcv (>= 1.1.5) but it is not going to be installed
                 Depends: r-cran-nlme (>= 3.1.52) but it is not going to be installed
                 Depends: r-cran-rpart (>= 3.1.20) but it is not going to be installed
                 Depends: r-cran-survival (>= 2.13.2-1) but it is not going to be installed
                 Depends: r-cran-mass but it is not going to be installed
                 Depends: r-cran-class but it is not going to be installed
                 Depends: r-cran-nnet but it is not going to be installed
                 Depends: r-cran-matrix but it is not going to be installed
E: Unable to correct problems, you have held broken packages.
FATAL:   While performing build: while running engine: exit status 100

使用的Singularity Def文件

Bootstrap: docker
From: ubuntu:20.04

%environment
export LANG=C.UTF-8 LC_ALL=C.UTF-8
export XDG_RUNTIME_DIR=/tmp/.run_$(uuidgen)

%post
# Install
apt-get -y update

export R_VERSION=4.1.3
echo "export R_VERSION=${R_VERSION}" >> $SINGULARITY_ENVIRONMENT

# Install R
apt-get update
apt-get install -y --no-install-recommends software-properties-common dirmngr  wget uuid-runtime
wget -qO- https://cloud.r-project.org/bin/linux/ubuntu/marutter_pubkey.asc | \
tee -a /etc/apt/trusted.gpg.d/cran_ubuntu_key.asc
add-apt-repository \
"deb https://cloud.r-project.org/bin/linux/ubuntu $(lsb_release -cs)-cran40/"
apt-get install -y --no-install-recommends \
r-base=${R_VERSION}* \
r-base-core=${R_VERSION}* \
r-base-dev=${R_VERSION}* \
r-recommended=${R_VERSION}* \
r-base-html=${R_VERSION}* \
r-doc-html=${R_VERSION}* \
libcurl4-openssl-dev \
libssl-dev \
libxml2-dev \
libcairo2-dev \
libxt-dev \
libopenblas-dev

# Add a default CRAN mirror
echo "options(repos = c(CRAN = 'https://cran.rstudio.com/'), download.file.method = 'libcurl')" >> /usr/lib/R/etc/Rprofile.site

# Fix R package libpaths (helps RStudio Server find the right directories)
mkdir -p /usr/lib64/R/etc
echo "R_LIBS_USER='/usr/lib64/R/library'" >> /usr/lib64/R/etc/Renviron
echo "R_LIBS_SITE='${R_PACKAGE_DIR}'" >> /usr/lib64/R/etc/Renviron
# Clean up
rm -rf /var/lib/apt/lists/*
  
  # Install python3
  apt-get -y install python3 wget
apt-get -y clean

# Install Miniconda
cd /
  wget --quiet https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
bash Miniconda3-latest-Linux-x86_64.sh -b -p /miniconda

/bin/bash <<EOF
rm Miniconda3-latest-Linux-x86_64.sh
source /miniconda/etc/profile.d/conda.sh
conda update -y conda
# Install reticulate and text
Rscript -e 'install.packages("reticulate")'
Rscript -e 'install.packages("devtools")'
Rscript -e 'install.packages("dplyr")'
Rscript -e 'install.packages("glmnet")'
Rscript -e 'devtools::install_github("oscarkjell/text")'
# Create the Conda environment at a system folder
Rscript -e 'text::textrpp_install(prompt = FALSE)'
Rscript -e 'text::textrpp_initialize(save_profile = TRUE, prompt = FALSE, textEmbed_test = TRUE)'
EOF

解决方案

1. 拆分R包安装步骤,自动处理依赖

将原有的R包安装命令拆分为两步,让APT自动处理r-recommended的依赖匹配:

# 先安装核心R包,指定版本
apt-get install -y --no-install-recommends --fix-broken \
r-base=${R_VERSION}* \
r-base-core=${R_VERSION}* \
r-base-dev=${R_VERSION}* \
libcurl4-openssl-dev \
libssl-dev \
libxml2-dev \
libcairo2-dev \
libxt-dev \
libopenblas-dev

# 单独安装推荐包及文档,不强制版本,让APT自动拉取兼容版本
apt-get install -y --no-install-recommends r-recommended r-base-html r-doc-html

2. 配置CRAN源优先级

添加CRAN源后,创建优先级配置文件,确保CRAN源的包优先级高于系统默认源,避免版本冲突:

# 在add-apt-repository之后添加以下内容
echo "Package: *
Pin: release o=CRAN
Pin-Priority: 700" > /etc/apt/preferences.d/cran

3. 修复后续APT命令的缓存问题

Def文件中清理APT缓存后直接安装python3会失败,需先重新更新缓存:

# Clean up
rm -rf /var/lib/apt/lists/*
  
# Install python3
apt-get -y update
apt-get -y install python3 wget
apt-get -y clean

完整修改后的Def文件片段

# Install R
apt-get update
apt-get install -y --no-install-recommends software-properties-common dirmngr  wget uuid-runtime
wget -qO- https://cloud.r-project.org/bin/linux/ubuntu/marutter_pubkey.asc | \
tee -a /etc/apt/trusted.gpg.d/cran_ubuntu_key.asc
add-apt-repository \
"deb https://cloud.r-project.org/bin/linux/ubuntu $(lsb_release -cs)-cran40/"

# 添加CRAN源优先级配置
echo "Package: *
Pin: release o=CRAN
Pin-Priority: 700" > /etc/apt/preferences.d/cran

apt-get update

# 分步安装R包
apt-get install -y --no-install-recommends --fix-broken \
r-base=${R_VERSION}* \
r-base-core=${R_VERSION}* \
r-base-dev=${R_VERSION}* \
libcurl4-openssl-dev \
libssl-dev \
libxml2-dev \
libcairo2-dev \
libxt-dev \
libopenblas-dev

apt-get install -y --no-install-recommends r-recommended r-base-html r-doc-html

# ... 后续原有内容 ...

# Clean up
rm -rf /var/lib/apt/lists/*
  
# Install python3
apt-get -y update
apt-get -y install python3 wget
apt-get -y clean

内容的提问来源于stack exchange,提问作者AlexGu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 02:10:39