Installing R and RStudio for M1 and M2 Mac systems is a bit complicated because Apple does not provide developer tools, like libcurl and openssl, by default. You can use homebrew to install these libraries but getting R to recognize these libraries is difficult.
A nice alternative is to first install Anaconda and then R and RStudio. Anaconda is an open-source distribution for data science in Python and R. It simplifies package management and deployment by bundling a comprehensive suite of tools, including the Conda package manager, Jupyter Notebook, and over 1,500 pre-installed libraries like NumPy, Pandas, and TensorFlow. It also has support for R which makes it an ideal solution to get your R environment ready for use by R Studio.
In this tutorial I will explain how to install Anaconda, then R and R Studio so you can quickly run R applications and scripts.
1. Install Anaconda
First, go to https://www.anaconda.com/download. Here you can find the right distribution of Anaconda for your platform. In this tutorial you need to download the Mac version for M1 and M2 architectures (ARM64). If you have an older Mac with Intel silicon, you need to download the distribution for that platform. For Windows and Linux platforms you don’t need to install Anaconda to get R working, but you can.
After downloading the ARM64 distribution of Anaconda, double-click the installer file (.pkg) and follow the instructions.
When installation has completed successfully, you should open a terminal window and test if Anaconda is working. To do this, open the Terminal.app by simultaneously pressing Cmd + space bar (this opens the search box) and typing terminal. When the search box finds the Terminal.app, press Enter to run it.
When you start the terminal window, after installing Anaconda, you will see a command prompt starting with (base). This means that the Anaconda environment has been successfully loaded and is available in the terminal window.
2. Install R through Anaconda
While still in the terminal window (step 1), create a new R environment by typing the following command: conda create -n r_env conda-forge::r-base conda-forge::r-essentials, and pressing Enter. If all goes well, a new R environment called r_env will have been created.
Next, in the same terminal window, activate the R environment you just created by typing conda activate r_env. Then type conda install -c conda-forge libcurl openssl. to install the required libcurl and openssl packages. You need these to be able to install any other package in R/RStudio.
3. Install RStudio
Go to the website https://posit.co/download/rstudio-desktop/ and select 2: Install RStudio. You can skip 1: Install R because you just did that through Anaconda. Follow the instructions to install RStudio.
After you installed RStudio, go back to the terminal window. If you left it open from the previous step, you can type open -a RStudio right away and start RStudio. If you closed the terminal window, you need to reopen it, activate the R environment like described before (conda activate r_env) and then type open -a RStudio to start RStudio. Unfortunately, you cannot start RStudio directly from the /Applications folder on your Mac because starting it this way does not correctly detect the R interpreter you installed through your Anaconda environment. You have to start it from within the terminal window after the Anaconda R environment has been activated. This is a bit annoying but, until now, it seems the only way to get R working on a Mac with a M1/M2 chipset.
4. Check correct R environment in RStudio
To check that RStudio indeed uses the correct R environment, go to the R console in RStudio (usually on the bottom left of the screen) and type R.home() and press Enter This should output the location of the R interpreter. If all went well, it should point to /opt/anaconda3/envs/r_env/bin/R.