Installing R with Homebrew

Installing R with Homebrew

As of 2017 / Brew 1.3.2 @ macOS Sierra 10.12.6 all you have to do is:

$ brew install r

You don't even need to tap homebrew/science since r is now a part of core formulae for the Homebrew (homebrew-core).

It will also install all dependencies automatically:

==> Installing dependencies for r: gmp, mpfr, libmpc, isl, gcc

There are two additional options you might want to know:

--with-java
Build with java support
--with-openblas
Build with openblas support

Installing R on OSX Big Sur (EDIT: and Apple M1) for use with Rcpp and openMP

Eventually, I found a process that works on a M1 mac with Big Sur.

  • Head over to https://mac.r-project.org/, it contains most things you will need
  • Download and install R via R-4.1-branch.pkg. The CRAN version might also work, but I used the installer from mac.r-project.org, which required opening the osx security settings to allow the installation.
  • Install RStudio, start it, and let it install the developer tools. Alternatively, run sudo xcode-select --install in Terminal.
  • Head to https://mac.r-project.org/openmp/. Download openmp-11.0.1-darwin20-Release.tar.gz and install it (see Terminal commands below).
curl -O https://mac.r-project.org/openmp/openmp-11.0.1-darwin20-Release.tar.gz
sudo tar fvx openmp-11.0.1-darwin20-Release.tar.gz -C /
  • Now we need to add compiler flags so that clan uses openMP. In Terminal, create the Makevars file.
cd ~
mkdir .R
nano .R/Makevars

in nano, paste these additional compiler flags into the Makevars file:

CPPFLAGS += -Xclang -fopenmp
LDFLAGS += -lomp

Hit Control+O, Control+X to save and close

  • Head over to the gfortran page: https://github.com/fxcoudert/gfortran-for-macOS/releases
  • Use the installer gfortran-ARM-11.0-BigSur.pkg to install gfortran.
  • For some reason it appears to install in /usr/local/gfortran, but R expects it in /opt. The mac-R team likes to separate arm64 and intel related files. We could go and fix paths, or simply also install gfortran under /opt. Download the tar file gfortran-ARM-11.0-BigSur.tar.xz. You can use curl, or just download it and point tar in the command line to it.
cd /opt/R/arm64/
sudo mkdir gfortran
sudo tar -xzyf gfortran-ARM-11.0-BigSur.tar.xz -C /opt/R/arm64/

(replace gfortran-ARM-11.0-BigSur.tar.xz with /users/YOURUSERNAME/downloads/gfortran-ARM-11.0-BigSur.tar.xz)

Now it should work.

Not an expert in OSX, but doing this so others can figure out how to use my R package. I'd like to streamline the process some more, but wiping the mac, reinstalling osx and testing it takes so much time.

Can homebrew R and standard R for MacOS from CRAN coexist?

Yes, installing from homebrew is a recipe for pain. It's specifically recommended against by the official CRAN binary maintainer see his remarks from March 2016 on r-sig-mac.

Regarding your questions, this can be summarized as:

  1. What is the advantage of a homebrew version of R for MacOSX over the "regular" version installed from CRAN?

Positives: Select your own BLAS and easily work with geospatial tools.

Downsides: Always needing to compile each R package.


  1. Can the two versions coexist?

Yes. The homebrew version installs into a different directory. But, watch out for library collision (see next question). However, you will have to deal with symbolic linking regarding what version of R is accessible from the console and you will also need to look into using RSwitch to switch between R versions.


  1. Is the homebrew version going to affect the regular one?
  • Yes, if the library paths overlap. There will be problems regarding package installation and loading. Make sure to setup different library paths. To do so, please look at the .libPaths() documentation.

  1. Finally: is homebrew going to help or will it simply open a whole new can of worms?

Yes and no. Unless you know what you're doing, opt for the CRAN version of R and its assorted goodies.

Install specific version of R via homebrew

Disclaimer: I don't have a Mac available for testing.

Here you can find a quite useful tutorial on how to install old versions via homebrew, here is another one.

For R this translates to:

git clone https://github.com/Homebrew/homebrew-core.git
cd homebrew-core/
git log master -- Formula/r.rb

which yields the following commits for R 3.5.2:

commit e66b3a288ee5e68c3c04e95705b7311c9f76bdf2
Author: ...
Date: Sun Jan 13 10:59:16 2019 +0000

r: update 3.5.2_2 bottle.

commit a2ced14fabb30732b2b6f22919105ac66085e78c
Author: ...
Date: Tue Jan 8 10:36:41 2019 +0100

r: revision for readline

commit 61c60b0c4c237a0ada3ab8b83b934566b101ee67
Author: ...
Date: Sat Jan 5 04:06:37 2019 +0000

r: update 3.5.2_1 bottle.

commit f9325e5e63ba812d1a2d5fd405811e313f782ff1
Author: ...
Date: Fri Jan 4 17:36:34 2019 +0100

r: build with openblas

commit 70aca0ac8f83459ff1ce18c1ea3f462b49434eb2
Author: ...
Date: Fri Dec 21 06:48:02 2018 +0000

r: update 3.5.2 bottle.

commit d344d5b57d0c8b0bf0ef6b3052e6bed551b37b47
Author: ...
Date: Fri Dec 21 14:31:14 2018 +0800

r 3.5.2

Closes #35321.

As an alternative you can search for commits via URL (Please see this comment):

https://github.com/Homebrew/homebrew-core/search?q=r%203.5.2&type=commits


Accordingly you should be able to install R 3.5.2 via (Latests R 3.5.2 commit):

brew install https://raw.githubusercontent.com/Homebrew/homebrew-core/e66b3a288ee5e68c3c04e95705b7311c9f76bdf2/Formula/r.rb

or:

brew install https://github.com/Homebrew/homebrew-core/raw/e66b3a2/Formula/r.rb

brew switch and brew pin as mentioned in this article might also be of interest.

Cannot install packages to R after brew install

My problem was that I had a few stray python3 config files floating around after a non-brew install of python3. I removed python3 and reinstalled with brew.

My recommendation to users having the same problem is to run brew doctor. If you are having the same problem I was, your python3 config files will be displayed as issues, and you can uninstall python3 and reinstall it with brew install python3.

I then had a few dependencies I had to work through, some in R, and some in brew.

@comfort eagle 's suggestion of using install_github() after installing and importing devtools was also helpful.

How to install R on Mac OS X 10.10.2 using Homebrew?

You should run:

brew tap homebrew/science
brew install r

since R is maintained in the homebrew/science tap.



Related Topics



Leave a reply



Submit