Fully Native M1/Apple Silicon R Setup

Presented without much commentary since I stopped once {ggrepel} and {graphlayouts} failed (RStudio doesn’t support it yet, either, which I knew).

The following steps will get you a fully working and STUPID FAST fully native ARM64 M1/Apple Silicon R setup with {tidyverse} and {rJava}.

Just remember, that if you need RStudio (or anything that links against the x86_64 R dylib) you’re going to be reverting this to get stuff done.

# Setting up ARM64 R on Apple Silicon/M1

# I'd run each section by hand, but feel free to live dangerously

# save off what you have installed in homebrew
brew list > ~/Documents/currently-installed-homebrew-formulas.txt

# uninstall x86_64 homebrew
# NOTE that in theory x86_64 and arm64 homebrew can live happily together but
# I went cheap on the SSD in the M1 Mini and wld like the space back
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall.sh)"

# make sure you unalias "brew" if you aliased it with "arch"
# need to do this in whatever shell startup script(s) you use, too
unalias brew

# install arm64 homebrew
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

# do what it says re: paths

# install wget (to make sure stuff is working)
brew install wget 

# place for r-libs; ty R Core & Prof Ripley!
mkdir ~/Downloads/libs-arm64/

# go there
cd ~/Downloads/libs-arm64

# grab'em from https://mac.r-project.org/libs-arm64/
for dl in $(curl -sS "https://mac.r-project.org/libs-arm64/" \
  | xmllint --html --xpath '//td/a[contains(@href, 'tar.gz')]/@href' 2>/dev/null - \
  | sed -e 's/ href="//g' -e 's/"/\n/g'); do wget "https://mac.r-project.org/libs-arm64/${dl}" ; done

# prime sudo (not rly necessary but I dislike having to enter sudo passwords in a for loop)
sudo ls ~/Downloads/libs-arm64

# extract'em
for gz in $(ls ~/Downloads/libs-arm64/*gz); do  
  sudo tar fvxz ${gz} -C /
done

# grab r-devel
cd ~/Downloads
wget https://mac.r-project.org/big-sur/R-devel/arm64/R-devel.tar.gz

# extract it
tar fvxz R*.tar.gz -C /

# install libxml2 and more (to prime libraries)
brew install libxml2 ccache libgit2 unixodbc poppler coreutils

# review ~/Documents/currently-installed-homebrew-formulas.txt and add what you need from there

# put this in your shell startup (macOS folks shld get used to zsh, so ~/.zshrc is a good place to stick it at the end)
export PATH=/opt/R/arm64/bin:$PATH

# and also run it at the command prompt
export PATH=/opt/R/arm64/bin:$PATH

# go for broke!
Rscript -e "install.packages('tidyverse')"

# throw caution to the wind!
Rscript -e "install.packages('devtools')"

# shoot for the moon!
Rscript -e "install.packages(c('DBI', 'odbc'))"

# it's crazytown
Rscript -e "install.packages('ggraph')"

# ARGH! {ggrepel} and {graphlayouts} fail (RStudio won't work anyway so this whole thing was just an exercise)

# setup java; open and run the pkg installer; they have a tar.gz as well 
wget https://cdn.azul.com/zulu/bin/zulu11.45.27-ca-jdk11.0.10-macosx_aarch64.dmg

# tell R about it
R CMD javareconf

# setup JAVA_HOME like you would in your shell

# this is necessary as the Java framework is gone from macOS in Big Sur
Rscript -e 'install.packages("rJava",,"http://rforge.net")'

##################################
# TO ***UNDO*** ALL OF THE ABOVE #
##################################

# uninstall amd64 homebrew
# NOTE that in theory x86_64 and arm64 homebrew can live happily together but
# I went cheap on the SSD in the M1 Mini and wld like the space back
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall.sh)"

# make sure you re-alias "brew" 
# need to do this in whatever shell startup script(s) you use, too
alias brew='arch --x86_64 brew'

# install x86_64 homebrew
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

# install libxml2 and more (to prime libraries)
brew install libxml2 ccache libgit2 unixodbc poppler coreutils

# review ~/Documents/currently-installed-homebrew-formulas.txt and add what you need from there

# remove the following from your shell startup script(s)
export PATH=/opt/R/arm64/bin:$PATH

# re-setup java for x86_64; open and run the pkg installer; they have a tar.gz as well 
wget "https://cdn.azul.com/zulu/bin/zulu11.45.27-ca-jdk11.0.10-macosx_x64.dmg" 

# re-do the parts of the rest of the setup that you need to

Until CRAN has {sf} binaries, use this recipe to build it from source.

Cover image from Data-Driven Security
Amazon Author Page

9 Comments Fully Native M1/Apple Silicon R Setup

  1. Pingback: Fully Native M1/Apple Silicon R Setup – Data Science Austria

  2. Pingback: Fully Native M1/Apple Silicon R Setup | R-bloggers

  3. Pingback: Fully Native M1/Apple Silicon R Setup - Security Boulevard

  4. Pingback: Fully Native M1/Apple Silicon R Setup | rud.is - Apple News

  5. Matt

    A somewhat related question. Did you by any chance ever manage to get OpenMP working in R with the m1?

    Reply
  6. Nathan

    Is there any way of having both setups available at the same time? From the way you have presented it, I am guessing not

    Reply
  7. Keith

    Thanks so much for this. I threw caution to the wind and tried to install ‘devtools’ but that is failing with the installation of package processx. Any thoughts about how I can overcome this?

    installing source package ‘processx’ …
    ** package ‘processx’ successfully unpacked and MD5 sums checked
    ** using staged installation
    ** libs
    /opt/homebrew/opt/llvm/bin/clang -fopenmp -g -O3 -Wall -pedantic -std=gnu99 -mtune=native -pipe -Wall tools/px.c -o tools/px
    ld: library not found for -lomp
    clang-11: error: linker command failed with exit code 1 (use -v to see invocation)
    make: *** [tools/px] Error 1
    ERROR: compilation failed for package ‘processx’

    Reply
    1. hrbrmstr

      Ah, yeah, anything with openmp is going to be a trip into Hades on macOS since Apple refuses to provide native support for it.

      I’m running 4.1.x (r-devel) on my M1 Mini since it’s a native M1 build of R. CRAN managed to get it to compile since there’s a processx R 4.1.x binary here: https://cran.r-project.org/bin/macosx/big-sur-arm64/contrib/4.1

      Give this — https://mac.r-project.org/openmp/ — a go and I’ll see if I can use RSwitch to try this on R 4.0.x later this weekend.

      Reply

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.