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:
| Component | Why | Windows | Mac |
|---|---|---|---|
| R (≥ 4.3) | the language | CRAN installer | CRAN installer (pick Intel or Apple-Silicon build) |
| RStudio | the IDE we use in class | Posit installer | Posit installer |
| Compiler toolchain | Nimble compiles models to C++ | Rtools | Xcode Command Line Tools |
| JAGS | a Bayesian sampler used alongside Nimble | JAGS installer (.exe) | JAGS installer (.dmg) or Homebrew |
| R packages | nimble, rjags, R2jags, coda, MCMCvis, HDInterval | install.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
- Go to cran.r-project.org/bin/windows/base and run the installer for the latest R. Accept the defaults.
- Go to posit.co/download/rstudio-desktop and install RStudio Desktop (free).
- Open RStudio. In the Console, run
R.version.stringto confirm the version.
Mac
- Go to cran.r-project.org/bin/macosx. Download the
.pkgthat 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. - Run the installer, accepting defaults.
- Install RStudio Desktop from posit.co/download/rstudio-desktop.
- Open RStudio and run
R.version.stringto 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
- Go to cran.r-project.org/bin/windows/Rtools.
- 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.
- Restart RStudio, then confirm the toolchain is visible:
R console
IfSys.which("make") # should return a non-empty path ending in make.exe pkgbuild::has_build_tools(debug = TRUE) # TRUE if Rtools is foundpkgbuildis missing, runinstall.packages("pkgbuild")first.
Mac — Xcode Command Line Tools
- Open Terminal (Applications → Utilities → Terminal) and run:
Terminal
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.)xcode-select --install - Verify:
Terminal
xcode-select -p # prints a path like /Library/Developer/CommandLineTools clang --version # prints the compiler version - 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
- Download the JAGS installer from SourceForge (JAGS 4.x, Windows). Choose the latest
JAGS-4.x.x.exe. - Run it with default options. It installs to
C:\Program Files\JAGS\JAGS-4.x.xby default. - No PATH editing is normally required —
rjagsfinds JAGS through the registry. (If it doesn't, see Troubleshooting.)
Mac
Option A — installer (simplest):
- Download the JAGS
.dmgfrom SourceForge (JAGS 4.x, Mac OS X). - Open the
.dmgand run the.pkginstaller.
Option B — Homebrew (if you use it):
brew install jags
4. Install the R packages
In the RStudio Console (same on Windows and Mac):
install.packages(c(
"coda", "rjags", "R2jags",
"nimble", "MCMCvis", "HDInterval",
"ggplot2", "dplyr"
))
Notes:
rjagsmust be installed after JAGS, or it will fail to load.nimbleis 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
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)
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)
library(MCMCvis)
MCMCsummary(s) # tidy table with mean, sd, 95% CI, Rhat, n.eff
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
rjagsbefore JAGS. Reinstall JAGS, then runinstall.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 reloadlibrary(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 --installagain; if it says "already installed" but compiling still fails, reset it withsudo 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.