Introduction

Some R packages (e.g., sglasso, RcppArmadillo) include source code written in C++ or Fortran. To compile these packages from source, your system must have a working Fortran compiler, specifically gfortran.

This guide covers installation instructions for both Windows and macOS on one page.


🔧 Windows Instructions

1. Install Rtools

Rtools includes gfortran and other compilers needed for building R packages.

2. Verify Installation

After installation, restart your computer and run this in R:

Sys.which("gfortran")
system("gfortran --version")

You should see version output like:

GNU Fortran (GCC) 13.2.0

If gfortran is not found, ensure Rtools is added to your PATH. You can add this line to .Renviron:

PATH="${RTOOLS40_HOME}\\usr\\bin;${PATH}"

🍎 macOS Instructions

1. Install Command Line Tools

Open Terminal and run:

xcode-select --install

This installs essential development tools including clang.

2. Install Apple Silicon-Compatible gfortran

Go to:

👉 https://mac.r-project.org/tools/

Download and install:

  • For Apple Silicon (M1/M2/M3):
    gfortran-13.2-arm64.pkg (or latest ARM version)

  • For Intel Macs:
    gfortran-13.2.pkg or gfortran-12.pkg (Intel-compatible version)

3. Verify Installation

Check from Terminal:

gfortran --version

You should see:

GNU Fortran (GCC) 13.2.0

If the command is not found, restart your Terminal or reinstall from the correct .pkg.


✅ Test Compilation in R

Once gfortran is properly installed, try compiling a package from source:

install.packages("devtools")
devtools::install_github("byuzbasi/sglasso")

🛠 Troubleshooting

Common error messages if gfortran is not installed or misconfigured:

Fix: Ensure you have the correct gfortran version for your CPU architecture (Intel vs ARM).


📌 Conclusion

Installing the correct version of gfortran ensures smooth compilation of many powerful R packages. If you run into errors, double-check:

Happy coding! 🎯