Install R and Python via Homebrew

R
Python
OS X
Install R and more tools using Homebrew, and save yourself a headache and valuable time when you need newer software versions.
Author

Mauricio “Pachá” Vargas S.

Published

January 8, 2017

Updated 2022-05-28: Updated links.

Updated 2018-02-12: Many thanks to Angelo Zehr (@angelozehr) who reported the Brew formula changed.

Why should you do this?

With a single brew upgrade you can save yourself a headache and valuable time when you need newer software versions.

Besides that, R binaries from CRAN and OS X stock Python can give problems with different libraries.

Here are some examples of unexpected situations of the mentioned versions:

  1. I built my blog by using Pelican. To use Pelican you need to run pip install pelican but that library figured as “not installed” after running that command on a fresh OS X installation.

  2. Some R packages will not work properly. For example, before data.table Mac binary came with multithread support R returned this message after running install.packages("data.table"):

This installation of data.table has not detected OpenMP support. 
It will still work but in single-threaded mode. 
If this is a Mac and you obtained the Mac binary of data.table from CRAN, 
CRAN's Mac does not support OpenMP.

Compiling R allows the user to use libraries such as OpenBLAS and that can enhance performance for some operations.

What do you need to do this?

You’ll need Homebrew and XCode installed. How to do that? check these links

Its possible to install just Command Line Tools for XCode from Apple Developers instead of full XCode. The script below contains a line to install that.

How do you do this?

This script will install R and Python. R will be installed with OpenBLAS, OpenMP and rJava enabled.

# See http://pacha.hk/2017-07-12_r_and_python_via_homebrew.html
# XCode CLT
xcode-select --install
# Update Homebrew and add formulae
brew update
# Check for broken dependencies and/or outdated packages
brew doctor
brew prune
# Brew PATH
echo "export LC_ALL=en_US.UTF-8" >> ~/.bash_profile
echo "export LANG=en_US.UTF-8" >> ~/.bash_profile
echo "export PATH=/usr/local/bin:$PATH" >> ~/.bash_profile && source ~/.bash_profile
# Python
brew install python
pip install pelican==3.6.3
pip install markdown rpy2
# R
brew install openblas
brew install r --with-openblas
echo 'Sys.setlocale(category="LC_ALL", locale = "en_US.UTF-8")' >> ~/.bash_profile
# data.table
R --vanilla << EOF
install.packages('data.table', repos='http://cran.us.r-project.org')
q()
EOF
# knitr
R --vanilla << EOF
install.packages('knitr', repos='http://cran.us.r-project.org')
q()
EOF
# rmarkdown
R --vanilla << EOF
install.packages('rmarkdown', repos='http://cran.us.r-project.org')
q()
EOF
# rJava
brew cask install java
sudo ln -f -s $(/usr/libexec/java_home)/jre/lib/server/libjvm.dylib /usr/local/lib
sudo Rscript -e 'install.packages("rJava", repos="http://rforge.net", type="source")';
# devtools
brew install openssl openssl@1.1 libssh2
sudo Rscript -e 'install.packages("devtools")';
# tidyverse tools
sudo Rscript -e 'install.packages("tidyverse"))';
# pdf extraction tools
brew install poppler
sudo Rscript -e 'install.packages("pdftools")';
sudo Rscript -e 'library(devtools); install_github("ropensci/tabulizer")';