I have seen many guides on Stack Overflow and elsewhere for building R packages from sources on macOS.
But what steps are required to build R itself from sources on macOS? And how do these differ between Intel- and ARM-based Macs, i.e., how is the process different on Apple silicon (M1, M2, ...)?
For simplicity, I would like to restrict attention to macOS 11 (Big Sur) and newer.
I am running Big Sur on an M1 Mac. I obtained a daily snapshot of R's sources here and have installed the mandatory tools listed here, namely Xcode and GNU Fortran.
So far I've tried
$ curl -LO https://stat.ethz.ch/R/daily/R-patched.tar.gz
$ tar -xvf R-patched.tar.gz
$ cd R-patched
$ ./configure
with the error
configure: error: No Fortran compiler found
so I guess that additional setup is required ...
[Grateful for comments/testing ...]
The authoritative sources of information on this topic are the R Installation and Admistration manual ("R-admin") and the R for macOS Developers web page. This answer is an unofficial summary, parts of which are liable to become out of date eventually as macOS and CRAN's build system evolve. As usual, sudo
at your own risk.
As part of the GNU project, R uses the Autotools build system, which should be familiar to anyone who has developed software for Linux. On most Linux systems, installing R's dependencies is easy enough and the build "just works". Unfortunately, that is not the case at all on macOS, where installing the prerequisites and configuring the build correctly can require a careful reading of the documentation. That is a barrier for macOS users wanting to (for example) create and test patches for bugs in R.
This guide tries to present the necessary steps for a CRAN-like build as compactly as possible, without getting into details (for better or worse). My hope is that a compact guide will help make the R-admin manual a bit less intimidating, especially for people who have never interacted with Autotools.
A copy of R's sources. If you have Subversion installed
on your system (try, e.g., svn --version
), then you can
check out the latest R-devel from R's Subversion repository
with
$ svn checkout https://svn.r-project.org/R/trunk/ R-devel
or the latest R-patched with
$ svn checkout https://svn.r-project.org/R/branches/R-x-y-branch/ R-patched
replacing x-y
with 4-4
for R version 4.4, 4-3
for
R version 4.3, and so on. Otherwise, you can download and
unpack one of the daily snapshots available
here.
For example:
$ curl -LO https://stat.ethz.ch/R/daily/R-patched.tar.gz
$ tar -xvf R-patched.tar.gz
If you intend to build the recommended packages (e.g., Matrix) along with R, then you will need to download the corresponding sources. R has a script for that:
$ cd R-devel # or R-patched
$ ./tools/rsync-recommended
If you downloaded one of the daily snapshots, then the script may do nothing, since tarballs containing the package sources are typically already included in the snapshots.
To obtain Subversion, you can use one of the binaries provided here, choosing the appropriate architecture-specific repository:
$ curl -LO https://mac.r-project.org/bin/darwin20/$(uname -m)/subversion-1.14.3-darwin.20-$(uname -m).tar.xz
$ sudo tar -xvf subversion-1.14.3-darwin.20-$(uname -m).tar.xz -C /
$ export PATH=/opt/R/$(uname -m)/bin:${PATH}
Command Line Tools for Xcode. These include Apple Clang and other tools needed to build libraries containing C and C++ code. You can install the latest version available for your version of macOS with:
$ sudo xcode-select --install
You can obtain older versions from your browser here. CRAN builds R 4.4.x for macOS 11.0 and newer using Xcode 14.2.
GNU Fortran. To avoid binary incompatibilities,
do not rely on Homebrew's gfortran
. Download and
unpack the binary used by CRAN, which is available
here:
$ curl -LO https://github.com/R-macos/gcc-12-branch/releases/download/12.2-darwin-r0/gfortran-12.2-darwin20-r0-universal.tar.xz
$ sudo tar -xvf gfortran-12.2-darwin20-r0-universal.tar.xz -C /
$ sudo ln -sfn $(xcrun --show-sdk-path) /opt/gfortran/SDK
MacTeX. A LaTeX distribution is used to build PDF versions of the R manuals and of R package help pages and vignettes. You can download the MacTeX distribution from CTAN here if you can spare 5-10 GiB of disk space:
$ curl -LO https://mirror.ctan.org/systems/mac/mactex/MacTeX.pkg
$ sudo installer -pkg MacTeX.pkg -target /
If you prefer the much smaller BasicTeX, then you will have to install additional TeX packages, but that is beyond the scope of this question.
XQuartz. Though not mandatory, XQuartz provides
an implementation of the X Window System, which is
needed for the X11()
graphics device, for View()
-ing
and edit()
-ing data frames, and for other graphical
functionality. Download and unpack a recent binary
from the XQuartz web page
here:
$ curl -LO https://github.com/XQuartz/XQuartz/releases/download/XQuartz-2.8.5/XQuartz-2.8.5.pkg
$ sudo installer -pkg XQuartz-2.8.5.pkg -target /
Additional external libraries, headers, and programs.
Some of these are mandatory
(libz
, libbz2
, liblzma
, libpcre2-8
, libcurl
).
Others are optional but desirable enough that CRAN also
installs them on its build system
(libjpeg
, libpng
, libtiff
, libcairo
, pkg-config
).
GNU Texinfo is required (in addition to MacTeX) for building
the PDF and Info versions of the R manuals whose sources are
written in the Texinfo format.
The Tcl and Tk headers and libraries are required to use
base package tcltk.
Simon Urbanek's recipes
build system provides a convenient mechanism for installing
those components that are not already part of macOS.
$ git clone https://github.com/R-macos/recipes.git
$ cd recipes
$ export PREFIX=/opt/R/$(uname -m)
$ sudo -E bash ./build.sh r-base-dev
$ cd other/tcltk
$ sh ./build-tcl.sh
$ sh ./build-tk.sh
$ sudo tar -xvf tcl*-$(uname -m).tar.gz -C /
$ sudo tar -xvf tk*-$(uname -m).tar.gz -C /
That will install the components in the r-base-dev
bundle,
as well as Tcl and Tk, under /opt/R/x86_64
on Intel-based Macs
and /opt/R/arm64
on on ARM-based Macs. Tools must be found on
the search path in the next step, so at this point you will want
to do (if you haven't already):
$ export PATH=/opt/R/$(uname -m)/bin:${PATH}
In the top level directory of R's sources, you'll find a script named configure
and a file named config.site
. Create and change to a build directory and copy config.site
there:
$ mkdir _build
$ cd _build
$ cp /path/to/config.site .
Edit your copy of config.site
so that it defines shell variables
as below, setting:
ARCH
to the value of uname -m
, namely x86_64
on Intel-based
Macs and arm64
on ARM-based Macs.OSVER
to 11.0
on Big Sur, 12.0
on Monterey, 13.0
on
Ventura, 14.0
on Sonoma, and 15.0
on Sequoia (i.e., the floor
of sw_vers -productVersion
). CRAN will use 11.0
as long as
it continues to support Big Sur.CC="clang"
CFLAGS="-Wall -g -O2 -pedantic -mmacosx-version-min=${OSVER} -falign-functions=8 -Wno-error=implicit-function-declaration"
CXX="clang++"
CXXFLAGS="-Wall -g -O2 -pedantic -mmacosx-version-min=${OSVER} -falign-functions=8"
FC="/opt/gfortran/bin/gfortran"
FCFLAGS="-Wall -g -O2 -pedantic -mmacosx-version-min=${OSVER} -mtune=native"
CPPFLAGS="-I/opt/R/${ARCH}/include"
LDFLAGS="-L/opt/R/${ARCH}/lib"
PKG_CONFIG="/opt/R/arm64/bin/pkg-config"
PKG_CONFIG_PATH="/opt/R/${ARCH}/lib/pkgconfig:/opt/R/${ARCH}/share/pkgconfig:/usr/lib/pkgconfig:/opt/X11/lib/pkgconfig:/opt/X11/share/pkgconfig"
Then run configure
in your build directory with appropriate flags. To emulate CRAN, your command line could look like this:
$ /path/to/configure --enable-R-framework --enable-memory-profiling --with-tcl-config=/opt/R/$(uname -m)/lib/tclConfig.sh --with-tk-config=/opt/R/$(uname -m)/lib/tkConfig.sh --x-includes=/opt/X11/include --x-libraries=/opt/X11/lib
Note that you must add the flag --without-recommended-packages
to the command line if you decided earlier to not run the rsync-recommended
script. In any case, if things are working, then you should see output ending with a summary similar to this one:
R is now configured for aarch64-apple-darwin23.6.0
Source directory: ../R-devel
Installation directory: /Library/Frameworks
C compiler: clang -Wall -g -O2 -pedantic -mmacosx-version-min=14.0 -falign-functions=8 -Wno-error=implicit-function-declaration
Fortran fixed-form compiler: /opt/gfortran/bin/gfortran -Wall -g -O2 -pedantic -mmacosx-version-min=14.0 -mtune=native
Default C++ compiler: clang++ -std=gnu++17 -Wall -g -O2 -pedantic -mmacosx-version-min=14.0 -falign-functions=8
C++11 compiler: clang++ -std=gnu++11 -Wall -g -O2 -pedantic -mmacosx-version-min=14.0 -falign-functions=8
C++14 compiler: clang++ -std=gnu++14 -Wall -g -O2 -pedantic -mmacosx-version-min=14.0 -falign-functions=8
C++17 compiler: clang++ -std=gnu++17 -Wall -g -O2 -pedantic -mmacosx-version-min=14.0 -falign-functions=8
C++20 compiler: clang++ -std=gnu++20 -Wall -g -O2 -pedantic -mmacosx-version-min=14.0 -falign-functions=8
C++23 compiler: clang++ -std=gnu++2b -Wall -g -O2 -pedantic -mmacosx-version-min=14.0 -falign-functions=8
Fortran free-form compiler: /opt/gfortran/bin/gfortran -Wall -g -O2 -pedantic -mmacosx-version-min=14.0 -mtune=native
Obj-C compiler: gcc -g -O2 -fobjc-exceptions
Interfaces supported: X11, aqua, tcltk
External libraries: pcre2, readline, curl, libdeflate
Additional capabilities: PNG, JPEG, TIFF, NLS, cairo, ICU
Options enabled: framework, shared BLAS, R profiling, memory profiling, libdeflate for lazyload
Capabilities skipped:
Options not enabled:
Recommended packages: yes
You may decide to modify the flags passed to configure
after running /path/to/configure --help
and consulting the documentation there, as well as the more detailed documentation in R-admin. Notably, --enable-R-framework
can be replaced by --enable-R-shlib
if you do not intend to run R inside of a GUI like R.app or RStudio.app. In that case, R is installed under /usr/local
unless you indicate otherwise by setting --prefix=
on the command line.
If you have configured and would like to reconfigure with different flags, then do start by cleaning the build directory:
$ make distclean
If necessary, you can always remove the build directory and start anew.
Just run:
$ make
At this point you should be able to run R directly from the build directory.
$ bin/R --version
Before installing R—and certainly before submitting patches to R—verify that your build passes all of the relevant tests:
$ make check-devel
If a test fails, then you can diagnose the error by inspecting any .fail
files in the tests
subdirectory.
It is not at all necessary to install R. For example, you might prefer to only install binaries built by CRAN. But if you wanted to, then you would run:
$ sudo make install
And if you configured using --enable-R-framework
, then you would add /Library/Frameworks/R.framework/Resources/bin
to your PATH
, allowing you to run R
in your shell without specifying its location.
You can always uninstall with:
$ sudo make uninstall
assuming that you have not destroyed the build directory.