How to Install the Tidyverse with R via Homebrew macOS

Kade Killary · 2019.03.12 · 2 minutes until it's over


Installing R is fairly painless via homebrew these days.

brew install r

Unfortunately, installing the Tidyverse isn’t quite as easy with the latest version of macOS. A simple install.packages("tidyverse") often ends in the following message:

* installing *source* package ‘xml2’ ...
** package ‘xml2’ successfully unpacked and MD5 sums checked
Found pkg-config cflags and libs!
Using PKG_CFLAGS=-I/usr/include/libxml2
Using PKG_LIBS=-L/usr/lib -lxml2 -lz -lpthread -licucore -lm
------------------------- ANTICONF ERROR ---------------------------
Configuration failed because libxml-2.0 was not found. Try installing:
 * deb: libxml2-dev (Debian, Ubuntu, etc)
 * rpm: libxml2-devel (Fedora, CentOS, RHEL)
 * csw: libxml2_dev (Solaris)
If libxml-2.0 is already installed, check that 'pkg-config' is in your
PATH and PKG_CONFIG_PATH contains a libxml-2.0.pc file. If pkg-config
is unavailable you can set INCLUDE_DIR and LIB_DIR manually via:
R CMD INSTALL --configure-vars='INCLUDE_DIR=... LIB_DIR=...'
ERROR: configuration failed for package ‘xml2’
* removing ‘/usr/local/lib/R/3.5/site-library/xml2’
Warning in install.packages :
  installation of package ‘xml2’ had non-zero exit status
ERROR: dependency ‘xml2’ is not available for package ‘rvest’
* removing ‘/usr/local/lib/R/3.5/site-library/rvest’

This wall of text then leads to endless attempts at getting libxml2 to be recognized. Yet, the pursuit remains elusive.

Luckily, the solution is rather simple. Navigate to your terminal and copy/paste the following command:

sudo installer -pkg /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg -target /

Apparently, as of macOS 10.14, these headers are missing in the base system under /usr/include. However, Apple did include a package, the one you see above, to address this issue. Although, in future releases this package will no longer be made available, so the solution is temporary.

You can read more here, specifically under Command Line Tools - New Features.