Ubuntu 22.04 Docker环境安装最新R版本失败求助
Ubuntu 22.04 Docker容器安装最新版R的依赖问题
错误现象
执行apt-get install -y r-base时出现如下错误:
$ apt-get install -y r-base Reading package lists... Done Building dependency tree... Done Reading state information... Done 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-base : Depends: r-base-core (>= 4.3.3-1.2204.0) but it is not going to be installed Depends: r-recommended (= 4.3.3-1.2204.0) but it is not going to be installed Recommends: r-base-html but it is not going to be installed Recommends: r-doc-html but it is not going to be installed E: Unable to correct problems, you have held broken packages.
原Dockerfile
此前用于安装旧版R的Dockerfile:
FROM ubuntu:22.04 USER root ENV DEBIAN_FRONTEND noninteractive ## INSTALL R RUN apt update -y \ && apt upgrade -y \ && apt install -y \ software-properties-common \ dirmngr \ gnupg2 \ wget RUN 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 -y "deb https://cloud.r-project.org/bin/linux/ubuntu $(lsb_release -cs)-cran40/" \ && apt-get update -y \ && apt-get install -y r-base
排查信息
移除apt-get install -y r-base步骤后,在容器内查看包信息:
r-base-core的apt policy
$ apt policy r-base-core r-base-core: Installed: (none) Candidate: 4.1.2-1ubuntu2 Version table: 4.1.2-1ubuntu2 500 500 http://ports.ubuntu.com/ubuntu-ports jammy/universe arm64 Packages
r-base的apt policy
$ apt policy r-base r-base: Installed: (none) Candidate: 4.3.3-1.2204.0 Version table: 4.3.3-1.2204.0 500 500 https://cloud.r-project.org/bin/linux/ubuntu jammy-cran40/ Packages 4.3.2-1.2204.0 500 500 https://cloud.r-project.org/bin/linux/ubuntu jammy-cran40/ Packages 4.3.1-4.2204.0 500 500 https://cloud.r-project.org/bin/linux/ubuntu jammy-cran40/ Packages 4.3.1-3.2204.0 500 500 https://cloud.r-project.org/bin/linux/ubuntu jammy-cran40/ Packages 4.3.1-2.2204.0 500 500 https://cloud.r-project.org/bin/linux/ubuntu jammy-cran40/ Packages 4.3.1-1.2204.0 500 500 https://cloud.r-project.org/bin/linux/ubuntu jammy-cran40/ Packages 4.3.0-1.2204.0 500 500 https://cloud.r-project.org/bin/linux/ubuntu jammy-cran40/ Packages 4.2.3-1.2204.0 500 500 https://cloud.r-project.org/bin/linux/ubuntu jammy-cran40/ Packages 4.2.2.20221110-1.2204.0 500 500 https://cloud.r-project.org/bin/linux/ubuntu jammy-cran40/ Packages 4.2.2-1.2204.0 500 500 https://cloud.r-project.org/bin/linux/ubuntu jammy-cran40/ Packages 4.2.1-3.2204.0 500 500 https://cloud.r-project.org/bin/linux/ubuntu jammy-cran40/ Packages 4.2.1-2.2204.0 500 500 https://cloud.r-project.org/bin/linux/ubuntu jammy-cran40/ Packages 4.2.1-1.2204.0 500 500 https://cloud.r-project.org/bin/linux/ubuntu jammy-cran40/ Packages 4.2.0-1.2204.0 500 500 https://cloud.r-project.org/bin/linux/ubuntu jammy-cran40/ Packages 4.1.2-1ubuntu2 500 500 http://ports.ubuntu.com/ubuntu-ports jammy/universe arm64 Packages
问题原因
核心是架构不匹配:从apt policy输出能看到,你用的是arm64架构的Ubuntu镜像,但CRAN默认的jammy-cran40源没有提供arm64版本的r-base-core、r-recommended等依赖包,只有x86_64架构的包。apt能识别到CRAN源的r-base元包(元包是架构无关的),但找不到对应的架构匹配的依赖包,所以抛出依赖未满足的错误。
解决方法
1. 指定x86_64架构构建镜像
如果你的Docker环境支持多架构(比如Docker Desktop开启了Buildx),可以强制用x86_64架构构建:
- 构建命令加参数:
docker build --platform linux/amd64 -t r-jammy .
- 或者在Dockerfile开头直接指定架构:
FROM --platform=linux/amd64 ubuntu:22.04
2. 使用CRAN的arm64专用源
CRAN为arm64单独提供了源,把Dockerfile中的源添加命令替换成:
add-apt-repository -y "deb https://cloud.r-project.org/bin/linux/ubuntu jammy-cran40-arm64/"
这里直接写jammy而非$(lsb_release -cs),避免变量解析问题,确保源路径正确。
3. 改用官方R镜像
直接基于rocker项目的官方R镜像构建,省去手动配置源的麻烦:
FROM rocker/r-ver:4.3.3 ENV DEBIAN_FRONTEND noninteractive # 这里添加你的项目依赖安装步骤
内容的提问来源于stack exchange,提问作者Marco Blanchette
相关产品推荐
相关产品推荐

