Mapping with R on Mac: Installation

I’m learning geocomputation with R by following the awesome content in this book:

It’s well written and easy to follow. But I bumped into a couple issues when I was trying to install the required packages, which were not mentioned in the book. In case anyone else face the same problem, here are the steps I took to make it work.

Step 1: Install Homebrew if you haven’t yet

/usr/bin/ruby -e "$(curl -fsSL"

Step 2: On Mac, a few requirements must be met to install sf package in R. More information can be found here at

If you have never installed GDAL before, install it with Homebrew first

brew install gdal

Then, upgrade to gdal 2 so that you can install the sf package later. To do that, nlink gdal 1.x

brew unlink gdal

Then, tap into osgeo4mac

brew tap osgeo/osgeo4mac && brew tap --repair

Then, install GDAL 2.X

brew install proj
brew install geos
brew install udunits
brew install gdal2 --with-armadillo --with-complete --with-libkml --with-unsupported

Finally, link GDAL 2.X

brew link --force gdal2

If you want to verify your installation (version number may vary):

$ gdal-config --version
$ gdal-config --libs
-L/usr/local/Cellar/gdal2/2.3.1/lib -lgdal
$ gdal-config --cflags

Step 3: Install packages

When I was trying to install sf in R, it threw me an error saying “tar: Failed to set default locale”. I managed to solve this issue by running system('defaults write org.R-project.R force.LANG en_US.UTF-8')in R console, then restart your R.

Then install all the follow packages. Worked like a charm.

install.packages("spDataLarge", repos = "",type = "source")

Now if you run the following the code in R, there shouldn’t be any error.