How to install and switch between multiple R versions on a Mac

hacks
r
It’s really quite smooth… once you have it set up.
Published

May 21, 2024

Every time I start a new analysis project, I like to try to start it on the newest available version of R so I can benefit from all those nice updates. (Additionally, some packages update their minimum compatible R version so not only do I get the newest version of R, I also get it to play most nicely with the newest version of packages!)

However, R’s default installation behavior when you use a .pkg installer on Mac is to uninstall all other installed versions of R. Which is quite bad for reproducibility! If my analysis code pipelines are basically independent from one another, I ought to be able to have multiple versions of R installed at the same time and associate different versions of R with different project folders.

Turns out, the framework (or the Framework… you’ll see) is already there! With a few additional steps, you too can become the master of versions. (And then you’ll have no excuse for starting your new R projects using that old, dusty R version…)

“Conda is better because it already lets you install a different python version for each environment!” It definitely does. Conda does this by installing a separate copy of the Python source code (and indeed all packages) in each environment. Python’s code base isn’t massive, so thankfully this doesn’t take up crazy amounts of storage. However, you can see that there are pros and cons to maintaining a machine-wide copy of software (as base R does) and prioritizing storage efficiency vs. multiple local copies and prioritizing flexibility (as conda does).

It’s pretty easy on Linux, actually

If you’re running on Linux, the default R installer behavior is to install R without uninstalling existing versions. How nice! As such, you should be able to install whichever additional versions you want without following any special instructions. Then follow the Linux section of these official Posit instructions to change which version RStudio attempts to open.

It’s also pretty easy on Windows

If you’re running on Windows, old forum posts suggest that the default R installer behavior is also to install R without uninstalling existing versions. (Why does it only do it for Mac, then…?) According to the official Posit instructions linked above, you can hold down the Ctrl key when clicking the RStudio icon and a dialog box will appear asking you to choose which version of R to open RStudio with.

Meanwhile, for Macs…

Below, see the steps for maintaining and switching between simultaneously installed versions of R on a Mac.

These instructions were inspired by Jacob Price’s blog post, but updated given R/Mac OS changes over time.

Make your Mac “forget” that R is already installed

As I mentioned before, the evil Mac .pkg installer for R will by default uninstall any other installed versions of R before installing the new one. It does this by checking through the Mac’s list of installed application packages and removing all folders listed as being installed with R.

However, when the installer installs the files, it actually by default installs R into a MAJOR.MINOR version-specific subfolder of the R install folder. That means that, for example, R 4.3.2 is installed into a folder for R 4.3. R 4.4.0 would get installed into a folder for R 4.4, which does not require overwriting R 4.3.2.

Note: You cannot have multiple simultaneous versions of R with the same major and minor version but different patch numbers. For example, R 4.3.2 and R 4.0.0 simultaneously are okay, but R 4.3.2 and R 4.3.3 will overwrite each other’s files.

Thus, the only thing you need to do to trick your Mac into not deleting other R versions is to remove those R application files from the list of installed packages, without actually deleting the application files themselves.

This way, when the new R installer runs, it doesn’t think there is any old version of R to uninstall.

First, in a terminal, use the pkgutil command to list all of your Mac’s “known” app packages associated with R.

pkgutil --pkgs='org.?-project*'
org.R-project.R.GUI.pkg
org.r-project.x86_64.texinfo
org.R-project.R.fw.pkg
org.r-project.x86_64.tcltk

The --pkgs flag can take a regex string as shown above. This regex will find all packages that start with org.r-project or org.R-project. Yeah, the capital/lowercase R thing is annoying. Be careful!

You’ll probably get the same package list as I did, but defer to what shows up on your own terminal (for example, if you’re running an Intel Mac instead of an Apple chip Mac.)

Each of these files pertains to a different component of R’s underlying application package source, including the GUI, the source code, and associated LaTeX info. Now, once for each of the file names you see, run, for example:

sudo pkgutil --forget org.R-project.arm64.R.GUI.pkg

You need to do this once for each of the files that are listed when you run pkgutil --pkgs='org.?-project*'. You can make sure you’ve gotten them all by checking that no packages show up when you run that command.

Now, your Mac thinks R is no longer installed. But when you run:

# These are the R versions that I have installed
ls -lh /Library/Frameworks/R.framework/Versions
total 0
drwxrwxr-x  6 root    admin   192B Sep 13 16:35 4.0
drwxrwxr-x  6 root    admin   192B Jul 31  2023 4.2-arm64
drwxrwxr-x  7 root    admin   224B May 20  2024 4.3-arm64
drwxrwxr-x  6 root    admin   192B May 20  2024 4.4-arm64
lrwxr-xr-x  1 mthieu  admin    50B Dec 28 22:10 Current -> /Library/Frameworks/R.framework/Versions/4.4-arm64

You should see that the folder for your existing R install is still there!

You can also see when you ls -l the content of /Library/Frameworks/R.framework/Versions that the “Current” folder, which is what RStudio calls by default, is merely symlinked to a specific R version (see that arrow pointing to one of the version-specific R folders), as opposed to there being only one “Current” folder that is fully overwritten every time you install R. If you change which R version “Current” points to, you could change which version RStudio runs with!

Install the new version of R as usual

Now that you’ve made your Mac forget that R was ever there, you can run the new .pkg R installer and install freely.

If you’re paying attention on the second screen of the .pkg installer, you will notice that it gives you instructions about how to “forget” the old R install if you want to stop the force-uninstall behavior. However, they don’t tell you every single pkgutil-listed package you need to pkgutil --forget. If you don’t forget all of the R-associated packages, the installer will overwrite some of the existing R application files, which renders the previous R version “incomplete” and unusable. That’s why you do need to check for every possible R-related package file using pkgutil --pkgs.

Switch the active version

I prefer the RSwitch menu bar GUI utility for switching my active R version.

If you want to be hardcore and do it without installing any extra software, the official Posit instructions also tell you that you can manually symlink the “Current” folder to the R version you want to use. For example, this will set me up to use R 4.3:

# remember, ln syntax puts source first, then link destination
ln -s /Library/Frameworks/R.framework/Versions/4.3-arm64 /Library/Frameworks/R.framework/Versions/Current

However, the Posit instructions also mention that you can use RSwitch, so no need to be a hero and use command line to switch every time 😜

Accessing R packages

Each R version has a separate store for packages. (This makes total sense.) Once you’ve switched versions, how do you get the packages you need for the R version you’re working on? I strongly recommend using the renv package for managing R package environments. While renv does not handle R version switching for you (hence this blog post), its system for managing package stores already smoothly handles packages for different R versions. You can use renv as usual and you should notice no differences as long as you switch to the correct R version before opening your R project. (Conveniently, renv will throw a warning upon startup if the version of R detected is different than the version recorded in the lockfile. If you forget to switch R versions, you can close RStudio, switch the R version, and reopen it.)