Skip to content

building from source on Apple M1 and M2 computers #145

Description

@ajrominger

Installing Rcpp Packages on M2 Mac: Fixing gfortran Dependencies

Problem Description

When trying to install R packages that use Rcpp and RcppArmadillo on an M2 Mac, you might encounter linking errors related to gfortran. This typically occurs when building packages that require LAPACK/BLAS linear algebra operations. The error message might look something like this:

ld: warning: search path '/opt/gfortran/lib/gcc/aarch64-apple-darwin20.0/12.2.0' not found
ld: warning: search path '/opt/gfortran/lib' not found
ld: library 'gfortran' not found
clang++: error: linker command failed with exit code 1

Solution Using Homebrew

1. Install gcc/gfortran

First, install gcc (which includes gfortran) using Homebrew:

brew install gcc

2. Verify Installation

You can verify that gfortran was installed correctly by checking its location and version:

# Check location
which gfortran
# Should show: /opt/homebrew/bin/gfortran

# Check version
gfortran --version

3. Configure R to Use Homebrew's gfortran

Create or modify your R Makevars file:

# Create ~/.R directory if it doesn't exist
mkdir -p ~/.R

# Create or modify Makevars
touch ~/.R/Makevars

Add the following lines to ~/.R/Makevars:

FLIBS=-L/opt/homebrew/lib/gcc/current -lgfortran -lquadmath

4. Rebuild Your Package

After setting up gfortran, rebuild your R package

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions