r-lib / pak

A fresh approach to package installation
https://pak.r-lib.org
682 stars 62 forks source link

`pak::pkg_install()` In Dockerfile Attempts Wrong Package Version #695

Closed z02dpj closed 1 month ago

z02dpj commented 1 month ago

I have a Quarto website I have built and want to host after it has been containerized in Docker. I can build the Docker image locally, but I am having issues trying to build it using an Azure DevOps Pipeline. The issue seems to stem from when I try to install specific versions of packages I want using pak.

The overview of the Dockerfile's steps:

# Base R image
FROM rocker/r-ver:4.1.2 AS builder

# Install Linux libraries required by R and Python package stored in the
# `renv.lock` and `requirements.txt` files, respectively. 
RUN apt-get update \
    && apt-get install -y --no-install-recommends \
    libcurl4-openssl-dev \
    libssl-dev \
    curl \
    && rm -rf /var/lib/apt/lists/*

# Install Quarto
# https://docs.posit.co/resources/install-quarto.html
#RUN export QUARTO_VERSION="1.4.555"
RUN mkdir -p /opt/quarto/1.4.555
RUN curl -o quarto.tar.gz -L \
    "https://github.com/quarto-dev/quarto-cli/releases/download/v1.4.555/quarto-1.4.555-linux-amd64.tar.gz"
RUN tar -zxvf quarto.tar.gz \
    -C "/opt/quarto/1.4.555" \
    --strip-components=1
RUN rm quarto.tar.gz

# Install Python from source.
# https://docs.posit.co/resources/install-python-source.html
#RUN export PYTHON_VERSION="3.10.9"
RUN apt-get update \
    && apt-get install -y --no-install-recommends \
    gcc \
    libbz2-dev \
    libev-dev \
    libffi-dev \
    libgdbm-dev \
    liblzma-dev \
    libncurses-dev \
    libreadline-dev \
    libsqlite3-dev \
    make \
    tk-dev \
    wget \
    zlib1g-dev

RUN curl -O https://www.python.org/ftp/python/3.10.9/Python-3.10.9.tgz
RUN tar -xvzf Python-3.10.9.tgz
WORKDIR Python-3.10.9
RUN ./configure \
    --prefix=/opt/python/3.10.9 \
    --enable-shared \
    --enable-optimizations \
    --enable-ipv6 \
    LDFLAGS=-Wl,-rpath=/opt/python/3.10.9/lib,--disable-new-dtags
RUN make
RUN make install

# Install pip.
# https://docs.posit.co/resources/install-python-source.html#install-pip
RUN curl -O https://bootstrap.pypa.io/get-pip.py
RUN /opt/python/3.10.9/bin/python3.10 get-pip.py

# Make a directory in the container
WORKDIR /
RUN mkdir /app
RUN chown 1000 /app

# Change working directory to `/app` to replicate how users interact
# with the repository.
WORKDIR /app

# Install `pak`
# Switch the default CRAN mirror
# https://rocker-project.org/images/versioned/r-ver.html#switch-the-default-cran-mirror
RUN R -e "options(repos = c(CRAN = 'https://cloud.r-project.org', P3M = 'https://packagemanager.posit.co/cran/__linux__/jammy/latest'))"
RUN R -e "install.packages('https://cloud.r-project.org/src/contrib/Archive/pak/pak_0.7.2.tar.gz')"

# Install `renv` and `reticulate`.
RUN R -e "pak::pkg_install(pkg = 'renv@0.15.5')"

The pak::pkg_install(pkg = 'renv@0.15.5') install returns an error that I cannot understand why it is happening. My interpretation is that it downloads renv version 0.15.4 instead of 0.15.5 that I specified. I believe it may have to do with the fact that my options() call is not being carried forward when I call pak, as I see pak says it Failed to download renv from 'https://packagemanager.posit.co/cran/__linux__/focal/2022-03-09/src/contrib/renv_0.15.4.tar.gz' and 'https://packagemanager.posit.co/cran/__linux__/focal/2022-03-09/src/contrib/Archive/renv/renv_0.15.4.tar.gz'.

2024-09-25T15:40:00.3152745Z #30 [builder 21/38] RUN R -e "pak::pkg_install(pkg = 'renv@0.15.5')"
2024-09-25T15:40:00.4885244Z #30 0.324 
2024-09-25T15:40:00.4885888Z #30 0.324 R version 4.1.2 (2021-11-01) -- "Bird Hippie"
2024-09-25T15:40:00.4886280Z #30 0.324 Copyright (C) 2021 The R Foundation for Statistical Computing
2024-09-25T15:40:00.4886703Z #30 0.324 Platform: x86_64-pc-linux-gnu (64-bit)
2024-09-25T15:40:00.4886967Z #30 0.324 
2024-09-25T15:40:00.6008333Z #30 0.333 R is free software and comes with ABSOLUTELY NO WARRANTY.
2024-09-25T15:40:00.6008745Z #30 0.333 You are welcome to redistribute it under certain conditions.
2024-09-25T15:40:00.6009354Z #30 0.333 Type 'license()' or 'licence()' for distribution details.
2024-09-25T15:40:00.6009574Z #30 0.333 
2024-09-25T15:40:00.6009812Z #30 0.333   Natural language support but running in an English locale
2024-09-25T15:40:00.6027258Z #30 0.333 
2024-09-25T15:40:00.6027581Z #30 0.333 R is a collaborative project with many contributors.
2024-09-25T15:40:00.6029040Z #30 0.333 Type 'contributors()' for more information and
2024-09-25T15:40:00.6029408Z #30 0.333 'citation()' on how to cite R or R packages in publications.
2024-09-25T15:40:00.6029638Z #30 0.333 
2024-09-25T15:40:00.6030348Z #30 0.333 Type 'demo()' for some demos, 'help()' for on-line help, or
2024-09-25T15:40:00.6030698Z #30 0.333 'help.start()' for an HTML browser interface to help.
2024-09-25T15:40:00.6031085Z #30 0.333 Type 'q()' to quit R.
2024-09-25T15:40:00.6031276Z #30 0.333 
2024-09-25T15:40:00.6031578Z #30 0.437 > pak::pkg_install(pkg = 'renv@0.15.5')
2024-09-25T15:40:02.2917103Z #30 2.127 
2024-09-25T15:40:03.2869214Z #30 3.123 ✔ Updated metadata database: 3.40 MB in 9 files.
2024-09-25T15:40:03.4391529Z #30 3.124 
2024-09-25T15:40:03.4392296Z #30 3.125 ℹ Updating metadata database
2024-09-25T15:40:07.4550897Z #30 7.291 ✔ Updating metadata database ... done
2024-09-25T15:40:07.4551197Z #30 7.291 
2024-09-25T15:40:07.6287043Z #30 7.465  
2024-09-25T15:40:07.8371702Z #30 7.471 → Will install 1 package.
2024-09-25T15:40:07.8372266Z #30 7.512 → Will download 1 package with unknown size.
2024-09-25T15:40:07.8372509Z #30 7.517 + renv   0.15.4 [dl]
2024-09-25T15:40:07.8372709Z #30 7.519   
2024-09-25T15:40:07.9319230Z #30 7.768 ℹ Getting 1 pkg with unknown size
2024-09-25T15:40:08.8747705Z #30 8.711 ✖ Failed to download renv 0.15.4 (x86_64-pc-linux-gnu-ubuntu-20.04)
2024-09-25T15:40:11.3361208Z #30 11.17 Error: 
2024-09-25T15:40:11.3361635Z #30 11.17 ! error in pak subprocess
2024-09-25T15:40:11.3361852Z #30 11.17 Caused by error: 
2024-09-25T15:40:11.3362672Z #30 11.17 ! Failed to download renv from `https://packagemanager.posit.co/cran/__linux__/focal/2022-03-09/src/contrib/renv_0.15.4.tar.gz` and `https://packagemanager.posit.co/cran/__linux__/focal/2022-03-09/src/contrib/Archive/renv/renv_0.15.4.tar.gz`.
2024-09-25T15:40:11.3363266Z #30 11.17 ---
2024-09-25T15:40:11.3363415Z #30 11.17 Backtrace:
2024-09-25T15:40:11.3363659Z #30 11.17 1. pak::pkg_install(pkg = "renv@0.15.5")
2024-09-25T15:40:11.3364008Z #30 11.17 2. pak:::remote(function(...) get("pkg_install_do_plan", asNamespace("pak"))(...), …
2024-09-25T15:40:11.3364274Z #30 11.17 3. err$throw(res$error)
2024-09-25T15:40:11.3364471Z #30 11.17 ---
2024-09-25T15:40:11.3364667Z #30 11.17 Subprocess backtrace:
2024-09-25T15:40:11.3364932Z #30 11.17 1. base::withCallingHandlers(cli_message = function(msg) { …
2024-09-25T15:40:11.3365236Z #30 11.17 2. get("pkg_install_do_plan", asNamespace("pak"))(...)
2024-09-25T15:40:11.3365482Z #30 11.17 3. proposal$stop_for_download_error()
2024-09-25T15:40:11.3365734Z #30 11.17 4. private$plan$stop_for_solution_download_error()
2024-09-25T15:40:11.3366022Z #30 11.17 5. pkgdepends:::pkgplan_stop_for_solution_download_error(self, private)
2024-09-25T15:40:11.3366286Z #30 11.17 6. pkgdepends:::stop(err)
2024-09-25T15:40:11.3366634Z #30 11.17 7. | base::throw(add_class(args[[1]], c("rlib_error_3_0", "rlib_error"), …
2024-09-25T15:40:11.3366936Z #30 11.17 8. | base::signalCondition(cond)
2024-09-25T15:40:11.3367588Z #30 11.17 9. global (function (e) …
2024-09-25T15:40:11.4545311Z #30 11.17 Execution halted
2024-09-25T15:40:11.4546165Z #30 ERROR: process "/bin/sh -c R -e \"pak::pkg_install(pkg = 'renv@0.15.5')\"" did not complete successfully: exit code: 1
2024-09-25T15:40:11.4793676Z ------
2024-09-25T15:40:11.4794177Z  > [builder 21/38] RUN R -e "pak::pkg_install(pkg = 'renv@0.15.5')":
2024-09-25T15:40:11.4794553Z 11.17 1. base::withCallingHandlers(cli_message = function(msg) { …
2024-09-25T15:40:11.4794826Z 11.17 2. get("pkg_install_do_plan", asNamespace("pak"))(...)
2024-09-25T15:40:11.4795164Z 11.17 3. proposal$stop_for_download_error()
2024-09-25T15:40:11.4795417Z 11.17 4. private$plan$stop_for_solution_download_error()
2024-09-25T15:40:11.4795855Z 11.17 5. pkgdepends:::pkgplan_stop_for_solution_download_error(self, private)
2024-09-25T15:40:11.4796128Z 11.17 6. pkgdepends:::stop(err)
2024-09-25T15:40:11.4796393Z 11.17 7. | base::throw(add_class(args[[1]], c("rlib_error_3_0", "rlib_error"), …
2024-09-25T15:40:11.4797040Z 11.17 8. | base::signalCondition(cond)
2024-09-25T15:40:11.4797339Z 11.17 9. global (function (e) …
2024-09-25T15:40:11.4797562Z 11.17 Execution halted
2024-09-25T15:40:11.4797795Z ------
2024-09-25T15:40:11.4818128Z 
2024-09-25T15:40:11.4819050Z  4 warnings found (use docker --debug to expand):
2024-09-25T15:40:11.4819563Z  - WorkdirRelativePath: Relative workdir "Python-3.10.9" can have unexpected results if the base image changes (line 45)
2024-09-25T15:40:11.4820049Z  - LegacyKeyValueFormat: "ENV key=value" should be used instead of legacy "ENV key value" format (line 87)
2024-09-25T15:40:11.4820934Z  - LegacyKeyValueFormat: "ENV key=value" should be used instead of legacy "ENV key value" format (line 98)
2024-09-25T15:40:11.4826757Z  - JSONArgsRecommended: JSON arguments recommended for CMD to prevent unintended behavior related to OS signals (line 112)
2024-09-25T15:40:11.4827087Z Dockerfile:76
2024-09-25T15:40:11.4827329Z --------------------
2024-09-25T15:40:11.4827557Z   74 |     
2024-09-25T15:40:11.4827839Z   75 |     # Install `renv` and `reticulate`.
2024-09-25T15:40:11.4828159Z   76 | >>> RUN R -e "pak::pkg_install(pkg = 'renv@0.15.5')"
2024-09-25T15:40:11.4828503Z   77 |     RUN R -e "pak::pkg_install(pkg = 'reticulate@1.37.0')"
2024-09-25T15:40:11.4828741Z   78 |     
2024-09-25T15:40:11.4829066Z --------------------
2024-09-25T15:40:11.4829522Z ERROR: failed to solve: process "/bin/sh -c R -e \"pak::pkg_install(pkg = 'renv@0.15.5')\"" did not complete successfully: exit code: 1
gaborcsardi commented 1 month ago

The 2022-03-09 snapshot you are using does not have renv 0.15.5, only 0.15.4.

z02dpj commented 1 month ago

How do I specify the snapshot? I assumed the Rocker documentation meant that pak would utilize the CRAN/P3M mirror I declared in the prior step.

z02dpj commented 1 month ago

This problem was due to my own user error. Following a different tip from the same section resulted in a successful build, as pak was able to source the packages from https://cloud.r-project.org.

# Install `pak`
# Switch the default CRAN mirror
# https://rocker-project.org/images/versioned/r-ver.html#switch-the-default-cran-mirror
RUN /rocker_scripts/setup_R.sh https://cloud.r-project.org
RUN R -e "install.packages('https://cloud.r-project.org/src/contrib/Archive/pak/pak_0.7.2.tar.gz')"

# Install `renv` and `reticulate`.
RUN R -e "pak::pkg_install(pkg = 'renv@0.15.5')"