Skip to content

Instantly share code, notes, and snippets.

@casallas
Last active January 31, 2024 23:16
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save casallas/8411082 to your computer and use it in GitHub Desktop.
Save casallas/8411082 to your computer and use it in GitHub Desktop.
Installing rjags on a Mac

To install

  1. Install jags: in the terminal using homebrew brew install jags
  2. Install rjags: in R
install.packages("rjags")
library(rjags)

Potential issues

Compiler issues

If you run into a problem like:

configure: error: C++ compiler cannot create executables

It's probably because R is not finding the right compiler, therefore you need to create or modify the file ~/.R/Makevars

Setting the correct CC and CXX, in my case:

CC=clang
CXX=clang++

Do this and then retry install.packages("rjags") or devtools::install_url.

Non-default jags installation

The problem

I installed jags via homebrew. However, my homebrew is setup on my home directory (/Users/casallas/homebrew) rather than the default, /usr/local.

When I do

install.packages("rjags")

everything works fine, except that loading the library doesn't work:

library(rjags)
Error : .onLoad failed in loadNamespace() for 'rjags', details:
  call: dyn.load(file, DLLpath = DLLpath, ...)
  error: unable to load shared object '/Users/casallas/Library/R/3.0/library/rjags/libs/rjags.so':
  dlopen(/Users/casallas/Library/R/3.0/library/rjags/libs/rjags.so, 10): Library not loaded: /usr/local/lib/libjags.3.dylib
  Referenced from: /Users/casallas/Library/R/3.0/library/rjags/libs/rjags.so
  Reason: image not found
Error: package or namespace load failed for ‘rjags’

As you can see, rjags is expecting jags to be in /usr/local.

The solution

The easiest solution is to install rjags from source, using devtools::install_url with the following configure.args, as specified in rjags README:

devtools::install_url("http://sourceforge.net/projects/mcmc-jags/files/rjags/3/rjags_3-2.tar.gz",
                      args="--configure-args='--with-jags-include=/Users/casallas/homebrew/opt/jags/include/JAGS        
                                              --with-jags-lib=/Users/casallas/homebrew/opt/jags/lib'
                            "
                      )
@vanhoutan
Copy link

[Kumailwasif] your solution worked for me thank you!

@egouldo
Copy link

egouldo commented Jan 31, 2024

I was able to get around the load library error after installing jags with home-brew by installing rjags from source like so:

install.packages("rjags",type = "source")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment