Accessible version · How to use this site with a screen reader · Course home
Pre-course preparation

Setting up R, JAGS, and Nimble on Windows and Mac

Do this before Day 1. Bayesian labs (Days 4–5) use JAGS and Nimble, and Nimble compiles C++, so a working compiler toolchain must be in place. Budget 30–60 minutes and finish the verification step.

What you need

By the end of this page you should have all of the following working:

ComponentWhyWindowsMac
R (≥ 4.3)the languageCRAN installerCRAN installer (pick Intel or Apple-Silicon build)
RStudiothe IDE we use in classPosit installerPosit installer
Compiler toolchainNimble compiles models to C++RtoolsXcode Command Line Tools
JAGSa Bayesian sampler used alongside NimbleJAGS installer (.exe)JAGS installer (.dmg) or Homebrew
R packagesnimble, rjags, R2jags, coda, MCMCvis, HDIntervalinstall.packages(...) (same on both)

Key idea

Order matters. Install R first, then the compiler toolchain, then JAGS, then the R packages. Installing rjags before JAGS, or nimble before the compiler, is the most common cause of "it won't install" problems.

1. Install R and RStudio

Windows

  1. Go to cran.r-project.org/bin/windows/base and run the installer for the latest R. Accept the defaults.
  2. Go to posit.co/download/rstudio-desktop and install RStudio Desktop (free).
  3. Open RStudio. In the Console, run R.version.string to confirm the version.

Mac

  1. Go to cran.r-project.org/bin/macosx. Download the .pkg that matches your chip: the arm64 build for Apple Silicon (M1/M2/M3/M4), or the x86_64 build for older Intel Macs. If unsure, click the Apple menu → About This Mac and read the "Chip"/"Processor" line.
  2. Run the installer, accepting defaults.
  3. Install RStudio Desktop from posit.co/download/rstudio-desktop.
  4. Open RStudio and run R.version.string to confirm.

2. Install the compiler toolchain (needed by Nimble)

Nimble writes C++ and compiles it, so it needs a compiler. This is the step people forget.

Windows — Rtools

  1. Go to cran.r-project.org/bin/windows/Rtools.
  2. Download the Rtools version that matches your R version (e.g., Rtools45 for R 4.5.x, Rtools44 for R 4.4.x). Run the installer with defaults.
  3. Restart RStudio, then confirm the toolchain is visible:
    R console
    Sys.which("make")      # should return a non-empty path ending in make.exe
    pkgbuild::has_build_tools(debug = TRUE)   # TRUE if Rtools is found
    If pkgbuild is missing, run install.packages("pkgbuild") first.

Mac — Xcode Command Line Tools

  1. Open Terminal (Applications → Utilities → Terminal) and run:
    Terminal
    xcode-select --install
    Click "Install" in the dialog that appears and wait for it to finish. (You do not need the full Xcode app, just the Command Line Tools.)
  2. Verify:
    Terminal
    xcode-select -p        # prints a path like /Library/Developer/CommandLineTools
    clang --version        # prints the compiler version
  3. Fortran (needed by some packages): install the gfortran package recommended on the CRAN macOS tools page. Nimble itself does not require Fortran, but installing it now avoids surprises with other spatial/mixed-model packages.

Watch out

On Apple Silicon, make sure your R build, Xcode tools, and packages are all the arm64 versions. Mixing an Intel R with arm64 tools (or vice versa) is a common source of Nimble compile errors.

3. Install JAGS

Windows

  1. Download the JAGS installer from SourceForge (JAGS 4.x, Windows). Choose the latest JAGS-4.x.x.exe.
  2. Run it with default options. It installs to C:\Program Files\JAGS\JAGS-4.x.x by default.
  3. No PATH editing is normally required — rjags finds JAGS through the registry. (If it doesn't, see Troubleshooting.)

Mac

Option A — installer (simplest):

  1. Download the JAGS .dmg from SourceForge (JAGS 4.x, Mac OS X).
  2. Open the .dmg and run the .pkg installer.

Option B — Homebrew (if you use it):

Terminal
brew install jags

4. Install the R packages

In the RStudio Console (same on Windows and Mac):

R console
install.packages(c(
  "coda", "rjags", "R2jags",
  "nimble", "MCMCvis", "HDInterval",
  "ggplot2", "dplyr"
))

Notes:

  • rjags must be installed after JAGS, or it will fail to load.
  • nimble is large and may take several minutes; it does not fully compile until you build your first model (that's the verification below).

5. Verify everything works

Run each block. If all three succeed, you are ready for the Bayesian labs.

a. JAGS + rjags

R console
library(rjags)
cat("model { p ~ dbeta(1,1); y ~ dbin(p, 10) }", file = "m.bug")
m <- jags.model("m.bug", data = list(y = 7), n.chains = 3, quiet = TRUE)
s <- coda.samples(m, "p", n.iter = 2000)
summary(s)$statistics       # posterior mean of p should be near 0.67

b. Nimble (this is the real compiler test)

R console
library(nimble)
code <- nimbleCode({
  p ~ dbeta(1, 1)
  y ~ dbinom(p, size = 10)
})
fit <- nimbleMCMC(code = code, data = list(y = 7),
                  inits = list(p = 0.5),
                  monitors = "p", niter = 2000, nburnin = 500)
mean(fit[, "p"])          # near 0.67; if this compiles, your toolchain works

c. MCMCvis (we use it to summarize posteriors)

R console
library(MCMCvis)
MCMCsummary(s)             # tidy table with mean, sd, 95% CI, Rhat, n.eff
Output
       mean        sd      2.5%       50%     97.5% Rhat n.eff
p 0.6691019 0.1272098 0.4078615 0.6802727 0.8862891    1  4003

Your digits will differ slightly — the samplers are stochastic and the blocks above set no seed. What matters is that the table prints: the mean sits near 0.67 and Rhat is about 1.00. The three chains in block (a) are what make Rhat computable; with a single chain it comes back NA and MCMCvis warns.

Key idea

If block (b) prints a posterior mean without errors, your C++ toolchain is correctly installed — that is the step most likely to fail, so this is the one that matters most.

6. Troubleshooting

"Error: .onLoad failed ... cannot find JAGS" (rjags)

  • You installed rjags before JAGS. Reinstall JAGS, then run install.packages("rjags", type = "source").
  • Windows: if it still can't find JAGS, set the path once per session with Sys.setenv(JAGS_HOME = "C:/Program Files/JAGS/JAGS-4.3.1") (adjust the version), then reload library(rjags).
  • Mac (Homebrew on Apple Silicon): JAGS lands under /opt/homebrew. If rjags can't find it, install rjags from source: install.packages("rjags", type = "source").

Nimble compile errors ("make: command not found", "clang: error", "cannot compile")

  • Windows: Rtools is missing or the wrong version. Reinstall the Rtools that matches your R version and restart RStudio. Confirm with pkgbuild::has_build_tools(debug = TRUE).
  • Mac: run xcode-select --install again; if it says "already installed" but compiling still fails, reset it with sudo xcode-select --reset.
  • Restart R after installing any toolchain (Session → Restart R).

Package won't install / binary vs source

  • If a binary install fails, try install.packages("nimble", type = "source").
  • Keep R and RStudio up to date; very old R versions may not have current package binaries.

Still stuck?

Email the instructor before Day 1 with: your operating system and chip, your R.version.string, and the exact error text. Setup problems are much faster to fix ahead of time than during a lab.

Final checklist

Try it yourself

  • R and RStudio open and report a version.
  • Compiler toolchain present (Rtools on Windows / Xcode CLT on Mac).
  • JAGS installed.
  • library(rjags), library(nimble), library(MCMCvis) all load without error.
  • The Nimble verification block (5b) compiled and printed a posterior mean.
  • You have also worked through the R Markdown tutorial, since all assignments are submitted as knitted HTML.