When installing R packages like xgboost, data.table, farver, or other packages that compile from source on Apple Silicon Macs, you may encounter errors like:
error: invalid value 'gnu23' in '-std=gnu23'
Or C++ symbol linking errors like:
symbol not found in flat namespace '__ZNSt3__113__hash_memoryEPKvm'
This happens because R 4.5.0+ uses the gnu23 C standard that Apple's default clang compiler doesn't support, and C++ packages may have standard library linking issues.
# Install Homebrew if you don't have it
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# Install LLVM, gettext, and libomp
brew install llvm gettext libomp# Create .R directory if it doesn't exist
mkdir -p ~/.R
# Create or edit the Makevars file
nano ~/.R/Makevars# Configuration for R 4.5.0+ with LLVM clang support for gnu23
# Updated 2025-08-31
# Use LLVM clang which supports gnu23 C standard
LLVM_LOC = /opt/homebrew/opt/llvm
CC=$(LLVM_LOC)/bin/clang
CXX=$(LLVM_LOC)/bin/clang++
# Include paths for gettext (libintl.h) and other dependencies
CPPFLAGS=-I/opt/homebrew/include -I/opt/homebrew/opt/gettext/include -I$(LLVM_LOC)/include -I/opt/homebrew/opt/libomp/include
# C++ standard library configuration for LLVM
CXXFLAGS=-stdlib=libc++
LDFLAGS=-L/opt/homebrew/lib -L/opt/homebrew/opt/gettext/lib -L$(LLVM_LOC)/lib -L/opt/homebrew/opt/libomp/lib -stdlib=libc++ -Wl,-rpath,$(LLVM_LOC)/lib -L$(LLVM_LOC)/lib/c++ -lc++
# OpenMP support
SHLIB_OPENMP_CFLAGS=-Xclang -fopenmp
SHLIB_OPENMP_CXXFLAGS=-Xclang -fopenmp
# PKG_CONFIG path
PKG_CONFIG_PATH=/opt/homebrew/lib/pkgconfig:/opt/homebrew/opt/libomp/lib/pkgconfigSave with Ctrl+O, Enter, then Ctrl+X.
install.packages('xgboost', repos='https://cran.r-project.org')
library(xgboost)- ✅ Compilation errors with packages that compile from source
- ✅ C standard compatibility issues between R 4.5.0+ and Apple's compiler
- ✅ Missing header files like
libintl.h - ✅ C++ standard library linking issues (symbol not found errors)
- ✅ Runtime loading failures for C++ packages
- ✅ OpenMP support for parallel processing
xgboost- Machine learning librarydata.table- Fast data manipulationjsonlite- JSON parsingfarver- Color space conversionRcpp-based packages - C++ integration packages- Most R packages requiring compilation from source
- Ensure Homebrew is properly installed
- Restart Terminal after installing Homebrew
- Verify the
~/.R/Makevarsfile was created correctly - Copy the configuration exactly as shown
- Restart R/RStudio after making changes
- These are harmless and can be ignored
- Packages will still work correctly
- The configuration with
-stdlib=libc++and RPATH should resolve this - Common error pattern:
symbol not found in flat namespace '__ZNSt3__1...'
- This is expected and harmless
- Means libc++ is being linked twice (implicitly and explicitly)
This issue occurs because:
- R 4.5.0+ defaults to using the
gnu23C standard - Apple's clang compiler (version 15.0.0) doesn't support
gnu23 - LLVM clang (version 21.1.0+) does support
gnu23 - C++ packages may use LLVM's libc++ standard library needing proper runtime linking
- Some packages need additional libraries like gettext for internationalization
The configuration:
- Compiler: Uses LLVM clang instead of Apple's clang
- C++ Standard Library: Explicitly uses LLVM's libc++ with
-stdlib=libc++ - Runtime Paths: Adds RPATH so packages can find symbols when loading
- Include/Library Paths: Provides access to all required headers and libraries
- OpenMP: Enables parallel processing support
- macOS: Big Sur 11.0+ (Apple Silicon)
- Hardware: M1, M2, M3, or newer Apple Silicon Macs
- R: Version 4.5.0 or higher
- Homebrew: For package management
Once configured, this setup persists and works for all future R package installations requiring compilation. No need to repeat the process.
Tested with: R 4.5.1, macOS Sequoia, M1 Max MacBook Pro
Last updated: August 2025
If this helps you, please ⭐ star this gist and share it with others facing the same issue!