Skip to content

Instantly share code, notes, and snippets.

@sueszli
Last active November 28, 2024 22:55
Show Gist options
  • Save sueszli/3e2b29b1b7b80471e1bcce6055f1c9ba to your computer and use it in GitHub Desktop.
Save sueszli/3e2b29b1b7b80471e1bcce6055f1c9ba to your computer and use it in GitHub Desktop.
gfortran for R packages on ARM based machines
# this container just serves to install the `klaR` package
#
# usage:
#
# $ docker compose up -d
# $ docker compose exec main Rscript -e "rmarkdown::render('ex7.rmd', output_format = 'pdf_document')"
services:
main:
container_name: main
volumes:
- type: bind
source: .
target: /workspace
working_dir: /workspace
ports:
- '8888:8888'
build:
context: .
dockerfile_inline: |
FROM --platform=linux/amd64 ubuntu:20.04
ARG DEBIAN_FRONTEND=noninteractive
RUN apt-get clean && rm -rf /var/lib/apt/lists/* && apt-get update
RUN apt-get install -y ca-certificates
RUN update-ca-certificates
# pandoc
RUN apt-get install -y --no-install-recommends pandoc texlive-latex-base texlive-fonts-recommended
RUN apt-get install -y --no-install-recommends texlive-latex-extra texlive-xetex texlive-lang-japanese
RUN apt-get install -y --no-install-recommends texlive-fonts-extra texlive-lang-cjk texlive-latex-recommended texlive-bibtex-extra
RUN apt-get install -y --no-install-recommends texlive-science texlive-pictures biber libfreetype6-dev libfontconfig1-dev
# fortran 9
RUN apt-get install -y gfortran-9
RUN update-alternatives --install /usr/bin/gfortran gfortran /usr/bin/gfortran-9 100
# r
RUN apt-get clean
RUN apt-get autoremove
RUN apt-get update
RUN apt-get install -y --no-install-recommends r-base r-base-dev
RUN apt-get install -y automake build-essential zlib1g-dev r-cran-httpuv
RUN Rscript -e 'for(p in c("rmarkdown", "ISLR", "IRkernel")) install.packages(p, repos = "https://cran.rstudio.com")'
# klar package
RUN Rscript -e 'install.packages("klaR", repos = "https://cran.rstudio.com")'
CMD ["tail", "-f", "/dev/null"]
@sueszli
Copy link
Author

sueszli commented Nov 28, 2024

In short: the klaR package requires you to have a gfortran compiler installed.

This can be done either through the Dockerfile above or – if you're using an ARM based MacOS system, simply by installing gcc first.

brew install gcc
mkdir -p ~/.R
echo "FC = /opt/homebrew/bin/gfortran # find path via `which gfortran`
F77 = /opt/homebrew/bin/gfortran
FLIBS = -L/opt/homebrew/lib" > ~/.R/Makevars

Verify the successful installation of this package with the following command:

Sys.setenv(PATH=paste("/opt/homebrew/bin", Sys.getenv("PATH"), sep=":"))
install.packages(c("classInt", "questionr", "klaR"), type="source")

if (!requireNamespace("klaR", quietly = TRUE)) {
    stop("package klaR not found")
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment