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

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.arm64.R.GUI.pkg## org.r-project.arm64.tcltk## org.R-project.arm64.R.fw.pkg## org.r-project.arm64.texinfo

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 installedls -lh /Library/Frameworks/R.framework/Versions
## total 0## drwxrwxr-x 6 root admin 192B Nov 2 2023 4.0## drwxrwxr-x 6 root admin 192B Jul 31 2023 4.2-arm64## drwxrwxr-x 7 root admin 224B May 20 13:06 4.3-arm64## drwxrwxr-x 6 root admin 192B May 20 13:08 4.4-arm64## lrwxr-xr-x 1 mthieu admin 50B May 20 13:14 Current -> /Library/Frameworks/R.framework/Versions/4.3-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 destinationln -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.)

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

References

Top Articles
Latest Posts
Recommended Articles
Article information

Author: Fr. Dewey Fisher

Last Updated:

Views: 6173

Rating: 4.1 / 5 (42 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Fr. Dewey Fisher

Birthday: 1993-03-26

Address: 917 Hyun Views, Rogahnmouth, KY 91013-8827

Phone: +5938540192553

Job: Administration Developer

Hobby: Embroidery, Horseback riding, Juggling, Urban exploration, Skiing, Cycling, Handball

Introduction: My name is Fr. Dewey Fisher, I am a powerful, open, faithful, combative, spotless, faithful, fair person who loves writing and wants to share my knowledge and understanding with you.