rguides

Installing R and RStudio on Windows, macOS, and Linux

R is a powerful programming language for statistical computing and data analysis. RStudio is an integrated development environment (IDE) that makes working with R much more enjoyable and productive. Together, they form the foundation for data science work in R.

Installing R and RStudio is the first step in setting up a data analysis environment. This tutorial walks you through installing both R and RStudio on your computer, covering Windows, macOS, and Linux. By the end, you will have a working R environment and know how to verify it, install packages, and navigate the RStudio interface.

Installing R

R is available for all major operating systems. The installation process varies slightly depending on your system.

Windows

  1. Visit the Comprehensive R Archive Network (CRAN) at cran.r-project.org
  2. Click on Download R for Windows
  3. Click on base (or click on install R for the first time)
  4. Download the latest version of R (currently R-4.4.x)
  5. Run the downloaded .exe file and follow the installation wizard
  6. Accept the default settings unless you have a specific reason to change them

macOS

  1. Visit CRAN at cran.r-project.org
  2. Click on Download R for macOS
  3. Download the appropriate package for your Apple Silicon (M1/M2/M3) or Intel Mac:
    • Apple Silicon (M1/M2/M3): Download the arm64.pkg file
    • Intel Mac: Download the x86_64.pkg file
  4. Open the downloaded file and follow the installation instructions

Linux

On Debian-based distributions (Ubuntu, Debian), you can install R using apt. The r-base package provides the core R runtime and the r-base-dev package adds the development headers needed for compiling R packages from source:

sudo apt update
sudo apt install r-base r-base-dev

On Fedora, RHEL, or CentOS, use the dnf package manager. The single R package pulls in the runtime and common dependencies. If you plan to install packages that require compilation, also install the R development package:

sudo dnf install R

On Arch Linux, the pacman package manager installs R from the community repository. Arch keeps R versions close to upstream, so you typically get the latest release shortly after it is published:

sudo pacman -S R

Installing RStudio

RStudio is the IDE that will transform your R experience. It provides a graphical interface with a code editor, console, plots pane, and many other useful features.

All operating systems

  1. Visit the Posit website (formerly RStudio) at posit.co/downloads
  2. Click on Download RStudio Desktop
  3. Choose the free RStudio Desktop option
  4. Download the installer for your operating system
  5. Run the installer and follow the instructions

For Windows, download the .exe installer. For macOS, download the .dmg file and drag RStudio to your Applications folder. For Linux, download the appropriate .deb or .rpm package.

Verifying your installation

After installation, let’s make sure everything is working correctly.

Opening RStudio

Launch RStudio from your applications menu or desktop shortcut. You should see a window divided into several panes:

  • Source Editor (top-left): Where you write and edit R scripts
  • Console (bottom-left): Where R code is executed
  • Environment/History (top-right): Shows your variables and command history
  • Files/Plots/Packages/Help (bottom-right): File browser, plots, package management, and help

Running your first R code

In the console pane, type the following and press Enter. R’s print() function displays values to the console — the [1] at the start of each line is R’s way of showing you the first element index in the output:

# Print a simple message
print("Hello, R world!")

The console should respond with the greeting, confirming that R can execute code and display results. Every line after this follows the same pattern: you type a command, R evaluates it, and the result appears in the console:

[1] "Hello, R world!"

Now try a simple calculation. R works as a calculator for arithmetic, and each expression is evaluated immediately when you press Enter:

# Basic arithmetic
2 + 2

You should see the result appear directly below. R evaluates arithmetic expressions and returns the numeric result without needing a print() wrapper:

[1] 4

Next, create a variable and compute a summary statistic. Variables store values for reuse, and vectorised functions like mean() operate on every element at once. Assign the numbers to a variable with c() (the combine function), then pass that variable to mean():

# Create a numeric vector
numbers <- c(1, 2, 3, 4, 5)

# Calculate the mean
mean(numbers)

The mean of the five values 1 through 5 is 3 — the output confirms that R correctly computed the arithmetic average of the vector:

[1] 3

Checking your R version

To verify which version of R you have installed, run:

R.version

This displays detailed information about your R installation, including the version number, platform, and operating system.

Installing packages

One of R’s greatest strengths is its vast ecosystem of packages—collections of functions and data that extend R’s capabilities.

Installing from CRAN

The Comprehensive R Archive Network (CRAN) hosts thousands of packages. Install the popular tidyverse package:

install.packages("tidyverse")

This downloads and installs tidyverse along with all its dependencies. You only need to install a package once (unless you want to update it).

Loading a package

Before using a package in your code, you must load it with the library() function:

library(tidyverse)

You should see a message displaying the loaded packages and any conflicts.

Understanding the RStudio interface

Take a few minutes to explore RStudio’s interface:

The console

The console is where your R code runs. You can type code directly here and see results immediately. The > symbol is the prompt, indicating R is ready for input.

The source editor

Create a new R script by clicking File > New File > R Script or pressing Ctrl+Shift+N (Cmd+Shift+N on Mac). Write your code here and run it using:

  • Ctrl+Enter (Cmd+Enter on Mac): Run the current line or selected code
  • Ctrl+Shift+Enter (Cmd+Shift+Enter on Mac): Run the entire script

The environment pane

This pane shows all variables and data frames currently in memory. Click on a variable to inspect its contents.

The files pane

Navigate your project directory, view plots, manage packages, and access help documentation.

Troubleshooting common issues

RStudio won’t start

If RStudio fails to launch, try these steps:

  1. Ensure R is properly installed by opening R directly
  2. Check for error messages in the startup log
  3. Try reinstalling both R and RStudio

Package installation fails

Some packages require system dependencies. On Linux, you may need to install additional libraries:

# Ubuntu/Debian
sudo apt install libcurl4-openssl-dev libxml2-dev libssl-dev

Slow performance

If R runs slowly, ensure you have enough RAM and consider increasing R’s memory limit with:

memory.limit(size = 4000)  # Windows: increase to 4GB

R vs RStudio

R is the programming language and runtime, the engine. RStudio (now called Posit Workbench for the enterprise version) is an integrated development environment (IDE) that provides a script editor, console, environment viewer, plot panel, and help browser. RStudio is optional but strongly recommended. VS Code with the R extension is a popular alternative, especially for users already familiar with VS Code.

Package management

Packages are installed with install.packages("name") from CRAN. pak::pkg_install("name") is faster and handles dependencies better. pak::pkg_install("username/repo") installs from GitHub. Once installed, packages are loaded into a session with library(name).

renv manages per-project package libraries: renv::init() creates an isolated library for a project, and renv::snapshot() records the exact package versions in renv.lock. This ensures the same package versions are used across machines and over time.

The .Rprofile and .Renviron files

~/.Rprofile runs on startup and is used for personal defaults (CRAN mirror, default options). ~/.Renviron stores environment variables like API keys: MY_API_KEY=abc123. Access them in R with Sys.getenv("MY_API_KEY"). Never put credentials in .Rprofile, .Renviron is the correct location. Both files are per-user and should not be committed to version control.

Choosing an R environment

R runs in several environments, each with different tradeoffs. RStudio is the most popular IDE for R and provides integrated panes for code editing, console interaction, plot display, package management, and help documentation. It is the standard choice for interactive analysis and package development. VS Code with the R extension is a lighter alternative that integrates R alongside other languages, which matters for projects that combine R with Python or JavaScript.

For server deployments, RStudio Server provides the same IDE interface through a browser without installing software on each user’s machine. Posit Workbench is the commercial version with team management features. For automated pipelines that run R code without user interaction, none of these environments is needed — R runs from the command line and scripts execute with Rscript.

Package management with renv

renv is the standard tool for reproducible R package environments. It captures the exact versions of all installed packages in a lockfile, renv.lock, that can be committed to version control. Running renv::restore() on a new machine installs exactly the same package versions recorded in the lockfile, reproducing the environment. Without renv, different machines install different package versions, which causes “it works on my machine” problems.

The practical workflow is: initialize renv in a new project with renv::init(), install packages normally, run renv::snapshot() to update the lockfile when packages change, and commit the lockfile to version control. Teammates run renv::restore() when they check out the project. For CI/CD pipelines, renv::restore() at the start of the pipeline installs the exact packages the lockfile specifies.

System dependencies

Some R packages require system libraries that are not R packages. The sf package needs GDAL, GEOS, and PROJ for spatial operations. The curl package needs libcurl. The xml2 package needs libxml2. These system dependencies must be installed by the operating system package manager — apt on Ubuntu/Debian, Homebrew on macOS, or the appropriate Windows installer — before the R package can be installed. Errors mentioning “shared library not found” or “configuration failed” usually indicate missing system dependencies.

The pak package, a modern alternative to install.packages, identifies system dependency requirements for packages before installing and can install them automatically on supported systems. For Docker images, the r-base images on Docker Hub include common system libraries, and the Rocker project provides images with common data science system dependencies pre-installed.

Next steps

Congratulations! You have successfully installed R and RStudio. Your next tutorial in the r-fundamentals series covers R Basics: Vectors and Types, where you’ll learn about R’s fundamental data structures.

To practice, try exploring R’s built-in datasets. The data() function lists every dataset available in your installed packages, and head() shows you the first few rows of a data frame so you can inspect its structure before diving into analysis:

# List available datasets
data()

# Load a dataset
data(mtcars)

# View the first few rows
head(mtcars)

This gives you a preview of working with data frames — one of the most common data structures in R.

See also