Install R packages that require compilation

System
Bash

Some notes derived from attempts to install cplex on MacOS

Author

Chi Zhang

Published

April 2, 2024

System information:

Objective: install Rcplex package so that I can run some analysis

Issue: neither gfortran nor cplex were available.

gfortran

Need to first install the latest gfortran for your system. In my computer it’s by default installed to /opt/gfortran

Pay attention to the error messages, and note down which version is needed. For instance, it could be aarch64-apple-darwin23-gcc-13

You might have to add a makevar file in this location: ~/.R/Makevars

LOC = /opt/gfortran
CC=$(LOC)/bin/aarch64-apple-darwin23-gcc-13 -fopenmp
CXX=$(LOC)/bin/aarch64-apple-darwin23-c++-13 -fopenmp
CXX11 = $(LOC)/bin/g++ -fopenmp # for fst package
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
# CPPFLAGS=-I$(LOC)/include -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include

In case you forgot how to create a new file,

touch ~/.R/Makevars
nano ~/.R/Makevars

This should solve the issue with gfortran.

cplex

I still had problem with cplex. The error message was:

checking for cplex… no Trying to use . as CPLEX installation location

There are 2 issues:

  • I do not have an installation of CPLEX
  • the path is not specified (when checked in R with Sys.getenv("CPLEX_DIR"))

Install CPLEX

To install CPLEX, need to have IBM ILOG CPLEX. One can install the trial version for free here: IBM ILOG CPLEX Optimization Studio.

On macOS, it is by default installing it to Applications/CPLEX_Studio<your_version>. (more information)

Configure the path

Now locate the installed CPLEX and related folders. What I need is the */include/ folder which holds all the files necessary for the compilation.

Can search the file in command line:

find /Applications/CPLEX_Studio_Community2211 -name "cplex.h"

There were a few ways to specify the system path, but the ONLY solution that worked for me on installing this particular package was:

Directly insert the CPLEX path in the installation argument.

install.packages("Rcplex", configure.args = "--with-cplex-dir=/Applications/CPLEX_Studio_Community2211/cplex")

Misc: embedding bash code in quarto

Side note: by including #|eval:false it’s actually quite convenient to highlight bash code without executing.

If want to execute, can simply remove the comment.

#!/bin/bash
echo "Today is " `date`