Search code examples
rgithubdevtools

Install with devtools::install_github() fails to detect build tools


This is my first time trying to download a Github package, and I'm running into some trouble using RStudio v1.2.5033 on MacOS Big Sur v11.2.1.

Initially, when running

library(devtools)
devtools::install_github('xuyiqing/gsynth')

I was getting this error:

Warning in install.packages :
  installation of package ‘gsynth’ had non-zero exit status

Did some reading, and it looked like I needed to download Command Line Tools, so I downloaded XCode CLI, clang4, and gfortran. Now I am getting a pop-up message saying:

"Install Build Tools Building R package from source requires installation of additional build tools. Do you want to install the additional tools now? Y/N"

If I click "No", I get this error:

Error: Failed to install 'gsynth' from GitHub:
  Could not find tools necessary to compile a package
Call `pkgbuild::check_build_tools(debug = TRUE)` to diagnose the problem.

Running the above pkgbuild code in an R console, this is what I get:

Trying to compile a simple C file
Error: Could not find tools necessary to compile a package
Call `pkgbuild::check_build_tools(debug = TRUE)` to diagnose the problem.
rror: Could not find tools necessary to compile a package

Solution

  • Looks like there are a few issues that you need to overcome to install this package (xcode command line tools and OpenMP support for a start), but you should overcome them if you follow the instructions here: https://stackoverflow.com/a/65334247/12957340

    After making the necessary changes, I successfully installed gsynth on my system (macOS Big Sur 11.2.3 / R version 4.0.3) using devtools::install_github('xuyiqing/gsynth') without issue.

    --

    Here are the instructions in case the link above dies:

    1. Reinstall xcode command line tools (even if it says "up to date")
    sudo rm -rf /Library/Developer/CommandLineTools
    sudo xcode-select --install
    
    1. Install gcc & llvm via Homebrew (instructions for installing Homebrew) or, if you already have gcc/llvm installed via Homebrew, skip to the next step
    # This can take several hours
    brew install gcc
    brew install llvm
    
    1. Once you have gcc & llvm installed via Homebrew:
    brew cleanup
    brew update
    brew upgrade
    brew reinstall gcc
    brew reinstall llvm
    
    1. Link some headers into /usr/local/include
    sudo ln -s /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/* /usr/local/include/
    
    # You can safely ignore warnings like this:
    #ln: /usr/local/include//tcl.h: File exists
    #ln: /usr/local/include//tclDecls.h: File exists
    #ln: /usr/local/include//tclPlatDecls.h: File exists
    #ln: /usr/local/include//tclTomMath.h: File exists
    #ln: /usr/local/include//tclTomMathDecls.h: File exists
    #ln: /usr/local/include//tk.h: File exists
    #ln: /usr/local/include//tkDecls.h: File exists
    #ln: /usr/local/include//tkPlatDecls.h: File exists
    
    1. Create a new ~/.R/Makevars file (if you already have a ~/.R/Makevars file, save it in a different directory (away from ~/.R/)) and include only these lines in the file:
    FLIBS=-L/usr/local/gfortran/lib/gcc/x86_64-apple-darwin19/10.2.0 -L/usr/local/gfortran/lib -lgfortran -lquadmath -lm
    CXX1X=/usr/local/gfortran/bin/g++
    CXX98=/usr/local/gfortran/bin/g++
    CXX11=/usr/local/gfortran/bin/g++
    CXX14=/usr/local/gfortran/bin/g++
    CXX17=/usr/local/gfortran/bin/g++
    
    LLVM_LOC = /usr/local/opt/llvm
    CC=/usr/local/gfortran/bin/gcc -fopenmp
    CXX=/usr/local/gfortran/bin/g++ -fopenmp
    CFLAGS=-g -O3 -Wall -pedantic -std=gnu99 -mtune=native -pipe
    CXXFLAGS=-g -O3 -Wall -pedantic -std=c++11 -mtune=native -pipe
    LDFLAGS=-L/usr/local/opt/gettext/lib -L$(LLVM_LOC)/lib -Wl,-rpath,$(LLVM_LOC)/lib
    CPPFLAGS=-I/usr/local/opt/gettext/include -I$(LLVM_LOC)/include -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include
    
    1. Compile a package from source in R/Rstudio
    # To check whether openmp is enabled, compile data.table:
    install.packages("data.table", type = "source")
    
    1. If your package fails to compile, a couple of SO users have had to install a fresh gfortran (re: https://stackoverflow.com/a/65334247/12957340), which you can download from https://github.com/fxcoudert/gfortran-for-macOS/releases/tag/10.2-bigsur-intel

    Update (2022/04/05)

    I had an error when updating "RcppAlgos" (couldn't find gmp.h or libgmp). I checked gmp was installed (brew install gmp), then added /usr/local/include to CPPFLAGS and /usr/local/lib to LDFLAGS in the ~/.R/Makevars file to solve the problem, i.e.

    cat ~/.R/Makevars
    LOC=/usr/local/gfortran
    CC=$(LOC)/bin/gcc -fopenmp
    CXX=$(LOC)/bin/g++ -fopenmp
    CXX11 = $(LOC)/bin/g++ -fopenmp
    
    CFLAGS=-g -O3 -Wall -pedantic -std=gnu99 -mtune=native -pipe
    CXXFLAGS=-g -O3 -Wall -pedantic -std=c++11 -mtune=native -pipe
    LDFLAGS=-L$(LOC)/lib -Wl,-rpath,$(LOC)/lib,-L/usr/local/lib
    CPPFLAGS=-I$(LOC)/include -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include -I/usr/local/include
    
    FLIBS=-L/usr/local/gfortran/lib/gcc/x86_64-apple-darwin19/10.2.0 -L/usr/local/gfortran/lib -lgfortran -lquadmath -lm
    CXX1X=/usr/local/gfortran/bin/g++
    CXX98=/usr/local/gfortran/bin/g++
    CXX11=/usr/local/gfortran/bin/g++
    CXX14=/usr/local/gfortran/bin/g++
    CXX17=/usr/local/gfortran/bin/g++