Skip to content

Installing leidenAlg for Mac OS

evanbiederstedt edited this page Apr 15, 2022 · 7 revisions

The package leidenAlg is currently on CRAN. We recommend that Mac OS users install via the binaries provided there, using the command:

install.packages('leidenAlg')

This is the way we would recommend users install leidenAlg.

Installing from Source

Before leidenAlg was on CRAN, users had to install the package from source, either using

devtools::install_github('kharchenkolab/leidenAlg')

or cloning the repo and installing via

git clone https://github.com/kharchenkolab/leidenAlg.git
cd leidenAlg
R CMD build .
R CMD install leidenAlg*.tar.gz

When installing from source, the R package (with the associated C++ code) must compile locally. If you are a Mac OS X user, it's highly likely that you're using the gcc and clang built by Xcode. This results in some frustration for users trying to install leidenAlg. For instance, Apple explicitly disabled OpenMP support in compilers that they ship in Xcode:

$ clang -c test_omp.c -fopenmp
clang: error: unsupported option '-fopenmp'

Note: OpenMP (Open Multi-Processing) is a library within leidenAlg which supports multi-platform shared-memory multiprocessing programming in C, C++, and Fortran.

There are some work arounds if you google solutions, e.g. this Stack Overflow post

Therefore, we recommend installing compilers and linking to them appropriately.

(This procedure has been tested on Mac OS Catalina, Version 10.15.5.)

Using Homebrew, install the following:

brew install gcc gfortran llvm libomp

Then, include the following in your ~/.zshrc (or ~/.bash_profile):

export PATH="/usr/local/opt/llvm/bin:$PATH"
export LDFLAGS="-L/usr/local/opt/llvm/lib"
export CPPFLAGS="-I/usr/local/opt/llvm/include"

Then, add the compiler paths to your ~/.R/Makevars file.

XCBASE:=$(shell xcrun --show-sdk-path)
LLVMBASE:=$(shell brew --prefix llvm)
GCCBASE:=$(shell brew --prefix gcc)
GETTEXT:=$(shell brew --prefix gettext)

CC=$(LLVMBASE)/bin/clang -fopenmp
CXX=$(LLVMBASE)/bin/clang++ -fopenmp
CXX11=$(LLVMBASE)/bin/clang++ -fopenmp
CXX14=$(LLVMBASE)/bin/clang++ -fopenmp
CXX17=$(LLVMBASE)/bin/clang++ -fopenmp
CXX1X=$(LLVMBASE)/bin/clang++ -fopenmp

CPPFLAGS=-isystem "$(LLVMBASE)/include" -isysroot "$(XCBASE)"
LDFLAGS=-L"$(LLVMBASE)/lib" -L"$(GETTEXT)/lib" --sysroot="$(XCBASE)"

FC=$(GCCBASE)/bin/gfortran -fopenmp
F77=$(GCCBASE)/bin/gfortran -fopenmp
FLIBS=-L$(GCCBASE)/lib/gcc/9/ -lm

(If you do not have a ~/.R/Makevars file, make one with mkdir .R; touch Makevars.)

Troubleshooting

Note: This is relevant for the package leidenAlg versions <= 1.0.2. Since version 1.0.3, this specific set of instructions is no longer valid.

Mac OS users have reported issues installing leidenAlg whereby the error states that the shared object igraph.so cannot be found.

Note: This shared object igraph.so is created when igraph is installed correctly. Upon compilation/installation, the R package leidenAlg needs to link to this shared object in order to install properly.

Here's how to troubleshoot (and hopefully solve) this issue if it arises:

  1. In order to fix this error, make sure that you have indeed installed igraph correctly. Users must have the associated system libraries installed correctly (e.g. libxml2)x. For details see the installation instructions for the igraph R package here.

  2. We'll use install_name_tool to change the dynamic library install names. (Mac OS users have this command line tool installed by default.) That's the operation the Makevars file is meant to perform for Mac OS users upon installation (see here). Steps A-C below should guide you in fixing this.

    (A) First find the path where you're using R. On the command line, type which R. You'll see something like the path /usr/local/bin/R.

    (B) Assuming your path is /usr/local/bin/R, now run the command:

    echo 'library(igraph); cat(system.file("libs", package="igraph"))' | /usr/local/bin/R --vanilla --slave
    

    (Naturally, change /usr/local/bin/R with your location of R.)

    This should give the location of the shared objects compiled when installing igraph. (For an example, here is an example output of the above command: /Library/Frameworks/R.framework/Versions/4.1/Resources/library/igraph/libs)

    (C) There should be the shared object igraph.so at that location. Assuming the path above is /Library/Frameworks/R.framework/Versions/4.1/Resources/library/igraph/libs, run the following command:

    install_name_tool -id '@rpath/igraph.so'  /Library/Frameworks/R.framework/Versions/4.1/Resources/library/igraph/libs/igraph.so
    

    (Naturally, change /Library/Frameworks/R.framework/Versions/4.1/Resources/library/igraph/libs/ with whatever output was given above.)

    Now, try re-installing leidenAlg.


Clone this wiki locally