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'
                            "
                      )
@JNavelski
Copy link

JNavelski commented Mar 25, 2018

@luceromz @teefryingpan

Operating system: MacOS 10.13.3
R Version: 3.4.4

I have installed rjags,using both the standard way of dowloading a package and through @Ibrocas comment, but for some reason I cannot get it to load the rjags package from my package library. There error code I am getting is:

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

Can anyone offer some advice on how to call rjags? I have removed and reinstalled multiple times.

Thanks to everyone on the forum!

@CommonClimate
Copy link

Just wanted to thank you for taking the time to write this. Everything appears to be working, thanks to your instructions.

@SoniaGasmi
Copy link

Hi guys,
I am facing the same problem! did anyone find a solution? @luceromz @teefryingpan @JNavelski
Thanks!

@mellowish
Copy link

Has anyone successfully gotten this to work recently? I am also trying to load rjags on my mac and I swear that it was working fine with install.packages last week. Today, I am getting the following error when loading my library. None of the above solutions seemed to work. Any ideas?

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

@Kumailwasif
Copy link

It worked for me after I installed the standalone JAGS:

https://sourceforge.net/projects/mcmc-jags/files/JAGS/4.x/Mac%20OS%20X/

@th2018
Copy link

th2018 commented Jan 25, 2019

I tried to install the package with:
devtools::install_url("http://sourceforge.net/projects/mcmc-jags/files/rjags/3/rjags_3-15.tar.gz",
args="--configure-args='--with-jags-include=/Users/casallas/homebrew/opt/jags/include/JAGS
--with-jags-lib=/Users/casallas/homebrew/opt/jags/lib'
")

Then I got this:
(converted from warning) installation of package ‘/var/folders/nd/ndldk_zn24jg80m122j__9dm0000gn/T//Rtmpi0yBew/file22371c86add/rjags_3-15.tar.gz’ had non-zero exit status

I think there is something wrong with the .gz file?

@fahrenfort
Copy link

Same here, it worked for me after I installed the standalone JAGS, very easy solution:

https://sourceforge.net/projects/mcmc-jags/files/JAGS/4.x/Mac%20OS%20X/

@lucaspaolucci
Copy link

It worked for me after I installed the standalone JAGS:

https://sourceforge.net/projects/mcmc-jags/files/JAGS/4.x/Mac%20OS%20X/

Thanks, this helped me too!

@Luffy-wu
Copy link

Thanks! the following works for me:

steps:

  1. From the following link, download the newest version of JAGS (like, JAGS-4.3.0.dmg) and install JAGS
    https://sourceforge.net/projects/mcmc-jags/files/JAGS/4.x/Mac%20OS%20X/
  2. Install rjags: in R
    install.packages("rjags")
  3. library(rjags)

@tonomuniz
Copy link

I was having similar problems to the previously mentioned and after I installed the standalone JAGS, it worked.

https://sourceforge.net/projects/mcmc-jags/files/JAGS/4.x/Mac%20OS%20X/

@IndrajeetPatil
Copy link

The instructions don't work.

Running

> 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'
                             "
 )

gives me-

Downloading package from url: http://sourceforge.net/projects/mcmc-jags/files/rjags/3/rjags_3-2.tar.gz
Error in utils::download.file(url, path, method = method, quiet = quiet,  : 
  cannot open URL 'http://sourceforge.net/projects/mcmc-jags/files/rjags/3/rjags_3-2.tar.gz'
Warning message:
1 components of `...` were not used.

We detected these problematic arguments:
* `args`

Did you misspecify an argument? 

@krakla
Copy link

krakla commented Jan 16, 2021

The instructions don't work.

Running

> 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'
                             "
 )

gives me-

Downloading package from url: http://sourceforge.net/projects/mcmc-jags/files/rjags/3/rjags_3-2.tar.gz
Error in utils::download.file(url, path, method = method, quiet = quiet,  : 
  cannot open URL 'http://sourceforge.net/projects/mcmc-jags/files/rjags/3/rjags_3-2.tar.gz'
Warning message:
1 components of `...` were not used.

We detected these problematic arguments:
* `args`

Did you misspecify an argument? 

Same here :(

@slothlife07
Copy link

It worked for me after I installed the standalone JAGS:

https://sourceforge.net/projects/mcmc-jags/files/JAGS/4.x/Mac%20OS%20X/

Thank you, this worked well for me. The readme is actually helpful and simple -> no actual need for homebrew etc.

@changwn
Copy link

changwn commented Feb 10, 2021

It worked for me after I installed the standalone JAGS:
https://sourceforge.net/projects/mcmc-jags/files/JAGS/4.x/Mac%20OS%20X/

Thank you, this worked well for me. The readme is actually helpful and simple -> no actual need for homebrew etc.

Thank you. This solution is the easiest solution. It works after I install the standalone JAGS. No trobule to find the path of lib/include.

@mkclapp
Copy link

mkclapp commented Jul 10, 2021

Sorry to resurface this thread, I hope this is a quick fix!-- I just bought a new MacBook Air w/ M1 chip; for some reason my installed R packages didn't migrate over and I need to reinstall rjags. I'm receiving the error you describe above "configure: error: C++ compiler cannot create executables" but don't know my way around my computer well enough to understand your suggestion about finding the right compiler and specifying CC and CXX. Where exactly is this done? Thanks v much for any suggestion you can provide!

@mkclapp
Copy link

mkclapp commented Jul 10, 2021

Sorry to resurface this thread, I hope this is a quick fix!-- I just bought a new MacBook Air w/ M1 chip; for some reason my installed R packages didn't migrate over and I need to reinstall rjags. I'm receiving the error you describe above "configure: error: C++ compiler cannot create executables" but don't know my way around my computer well enough to understand your suggestion about finding the right compiler and specifying CC and CXX. Where exactly is this done? Thanks v much for any suggestion you can provide!

Update: Using the terminal I made a Makevars file in the ~/.R folder containing the text
CC=clang
CXX=clang++
And tried again to install rjags but received the same error:

xcrun: error: unable to load libxcrun (dlopen(/Library/Developer/CommandLineTools/usr/lib/libxcrun.dylib, 0x0005): could not use '/Library/Developer/CommandLineTools/usr/lib/libxcrun.dylib' because it is not a compatible arch). this error message is repeated several times
checking for pkg-config... no
configure: Attempting legacy configuration of rjags
checking for jags... /usr/local/bin/jags
checking for g++... g++
checking whether the C++ compiler works... no
configure: error: in /private/var/folders/sv/3zwcb1kj0s32rv6ky634dxx40000gn/T/Rtmp2YJdvZ/R.INSTALL235545c4ce2f/rjags': configure: error: C++ compiler cannot create executables See config.log' for more details
ERROR: configuration failed for package ‘rjags’

@barracuda156
Copy link

Cannot install rjags either. Solutions above do not work:

** testing if installed package can be loaded from temporary location
Error: package or namespace load failed for ‘rjags’:
.onLoad failed in loadNamespace() for 'rjags', details:
call: dyn.load(file, DLLpath = DLLpath, ...)
error: unable to load shared object '/Users/svacchanda/Library/R/powerpc/4.1/library/00LOCK-rjags/00new/rjags/libs/rjags.so':
dlopen(/Users/svacchanda/Library/R/powerpc/4.1/library/00LOCK-rjags/00new/rjags/libs/rjags.so, 10): Symbol not found: __ZN4jags5Model12rngFactoriesB5cxx11Ev
Referenced from: /Users/svacchanda/Library/R/powerpc/4.1/library/00LOCK-rjags/00new/rjags/libs/rjags.so
Expected in: dynamic lookup

Error: loading failed
Execution halted
ERROR: loading failed

  • removing ‘/Users/svacchanda/Library/R/powerpc/4.1/library/rjags’
    Warning message:
    In i.p(...) :
    installation of package ‘/var/folders/rD/rDeCM6SDHv8daLCecrRmrU+++TI/-Tmp-//RtmpgawayR/file130455010bb8a/rjags_4-4.tar.gz’ had non-zero exit status

@peijianying
Copy link

MAC with M1 用不了,不用试了,建议换成window

@ViRb3
Copy link

ViRb3 commented Mar 12, 2022

Easiest way to install native arm64 jags:

brew install pkg-config jags
/Applications/RStudio.app/Contents/MacOS/RStudio
install.packages("rjags", type="source")

It is important to launch RStudio from a terminal and not the app directly, because otherwise the homebrew bin path will not be added to your PATH, and installation of rjags will fail.

@Taojun-Hu
Copy link

Thanks a lot. This works on my computer.

@mskb01
Copy link

mskb01 commented Apr 22, 2022

Unfortunately, I am having an issue with installing RJAGS on Mac OS Big Sur, 2018 Mac Pro, Intel I9.
Running R 4.1.1. I have installed JAGS and I am able to run that program independently.

I also worked through the devtools include option mentioned above but still no luck. Any thoughts?

Approach 1

>install.packages("rjags")
* installing *source* package ‘rjags’ ...
** package ‘rjags’ successfully unpacked and MD5 sums checked
** using staged installation
checking for pkg-config... /usr/local/bin/pkg-config
configure: Setting compile and link flags according to pkg-config
configure: Compile flags are -I/usr/local/include/JAGS
configure: Link flags are -L/usr/local/lib -ljags
checking for gcc... /usr/local/bin/gcc-11
checking whether we are using the GNU C compiler... no
checking whether /usr/local/bin/gcc-11 accepts -g... no
checking for /usr/local/bin/gcc-11 option to accept ISO C89... unsupported
checking for jags_version in -ljags... yes
checking version of JAGS library... OK
configure: creating ./config.status
config.status: creating src/Makevars
configure: creating ./config.status
config.status: creating src/Makevars
config.status: creating R/unix/zzz.R
** libs
/usr/local/bin/gcc-11 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I/usr/local/include/JAGS   -I/usr/local/include   -fPIC  -Wall -g -O2  -c init.c -o init.o
/usr/local/bin/g++-11 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I/usr/local/include/JAGS   -I/usr/local/include   -fPIC  -Wall -g -O2  -c jags.cc -o jags.o
/usr/local/bin/g++-11 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I/usr/local/include/JAGS   -I/usr/local/include   -fPIC  -Wall -g -O2  -c parallel.cc -o parallel.o
/usr/local/bin/g++-11 -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/Library/Frameworks/R.framework/Resources/lib -L/usr/local/lib -o rjags.so init.o jags.o parallel.o -L/usr/local/lib -ljags -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation
installing to /Library/Frameworks/R.framework/Versions/4.1/Resources/library/00LOCK-rjags/00new/rjags/libs
** R
** data
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded from temporary location
Error: package or namespace load failed for ‘rjags’:
 .onLoad failed in loadNamespace() for 'rjags', details:
  call: dyn.load(file, DLLpath = DLLpath, ...)
  error: unable to load shared object '/Library/Frameworks/R.framework/Versions/4.1/Resources/library/00LOCK-rjags/00new/rjags/libs/rjags.so':
  dlopen(/Library/Frameworks/R.framework/Versions/4.1/Resources/library/00LOCK-rjags/00new/rjags/libs/rjags.so, 10): Symbol not found: __ZN4jags11SimpleRangeC1ERKSt6vectorIiSaIiEES5_
  Referenced from: /Library/Frameworks/R.framework/Versions/4.1/Resources/library/00LOCK-rjags/00new/rjags/libs/rjags.so
  Expected in: flat namespace
 in /Library/Frameworks/R.framework/Versions/4.1/Resources/library/00LOCK-rjags/00new/rjags/libs/rjags.so
Error: loading failed
Execution halted
ERROR: loading failed
* removing ‘/Library/Frameworks/R.framework/Versions/4.1/Resources/library/rjags’

The downloaded source packages are in
	‘/private/var/folders/_3/8v09s_c91fs5myl646c2_ts80000gp/T/Rtmp0930vW/downloaded_packages’
Warning message:
In install.packages("rjags") :
  installation of package ‘rjags’ had non-zero exit status

Approach 2: Devtools

>devtools::install_url("http://sourceforge.net/projects/mcmc-jags/files/rjags/4/rjags_4-4.tar.gz",
                      args="--configure-args='--with-jags-include=/usr/local/include/JAGS 
                                                                 --with-jags-lib=/usr/local/opt/jags/lib'
                            ")

Downloading package from url: http://sourceforge.net/projects/mcmc-jags/files/rjags/4/rjags_4-4.tar.gz
✓  checking for file ‘/private/var/folders/_3/8v09s_c91fs5myl646c2_ts80000gp/T/Rtmpark1rv/remotes3785557112a/rjags/DESCRIPTION’ ...
─  preparing ‘rjags’:
✓  checking DESCRIPTION meta-information ...
─  cleaning src
─  running ‘cleanup’
─  checking for LF line-endings in source and make files and shell scripts
─  checking for empty or unneeded directories
─  looking to see if a ‘data/datalist’ file should be added
─  building ‘rjags_4-4.tar.gz’
   
* installing *source* package ‘rjags’ ...
** using staged installation
checking for pkg-config... /usr/local/bin/pkg-config
configure: Setting compile and link flags according to pkg-config
configure: Compile flags are -I/usr/local/include/JAGS
configure: Link flags are -L/usr/local/lib -ljags
checking for gcc... /usr/local/bin/gcc-11
checking whether we are using the GNU C compiler... no
checking whether /usr/local/bin/gcc-11 accepts -g... no
checking for /usr/local/bin/gcc-11 option to accept ISO C89... unsupported
checking for jags_version in -ljags... yes
checking version of JAGS library... OK
configure: creating ./config.status
config.status: creating src/Makevars
configure: creating ./config.status
config.status: creating src/Makevars
config.status: creating R/unix/zzz.R
** libs
/usr/local/bin/g++-11 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I/usr/local/include/JAGS   -I/usr/local/include   -fPIC  -Wall -g -O2  -c jags.cc -o jags.o
jags.cc: In function 'jags::SimpleRange makeRange(SEXP, SEXP)':
jags.cc:213:29: warning: catching polymorphic type 'class std::logic_error' by value [-Wcatch-value=]
  213 |     catch (std::logic_error except) {
      |                             ^~~~~~
/usr/local/bin/g++-11 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I/usr/local/include/JAGS   -I/usr/local/include   -fPIC  -Wall -g -O2  -c parallel.cc -o parallel.o
/usr/local/bin/g++-11 -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/Library/Frameworks/R.framework/Resources/lib -L/usr/local/lib -o rjags.so jags.o parallel.o -L/usr/local/lib -ljags -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation
installing to /Library/Frameworks/R.framework/Versions/4.1/Resources/library/00LOCK-rjags/00new/rjags/libs
** R
** data
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded from temporary location
Error: package or namespace load failed for ‘rjags’:
 .onLoad failed in loadNamespace() for 'rjags', details:
  call: dyn.load(file, DLLpath = DLLpath, ...)
  error: unable to load shared object '/Library/Frameworks/R.framework/Versions/4.1/Resources/library/00LOCK-rjags/00new/rjags/libs/rjags.so':
  dlopen(/Library/Frameworks/R.framework/Versions/4.1/Resources/library/00LOCK-rjags/00new/rjags/libs/rjags.so, 10): Symbol not found: __ZN4jags11SimpleRangeC1ERKSt6vectorIiSaIiEES5_
  Referenced from: /Library/Frameworks/R.framework/Versions/4.1/Resources/library/00LOCK-rjags/00new/rjags/libs/rjags.so
  Expected in: flat namespace
 in /Library/Frameworks/R.framework/Versions/4.1/Resources/library/00LOCK-rjags/00new/rjags/libs/rjags.so
Error: loading failed
Execution halted
ERROR: loading failed
* removing ‘/Library/Frameworks/R.framework/Versions/4.1/Resources/library/rjags’
Warning messages:
1: In i.p(...) :
  installation of package ‘/var/folders/_3/8v09s_c91fs5myl646c2_ts80000gp/T//Rtmpark1rv/file3781530638c/rjags_4-4.tar.gz’ had non-zero exit status
2: 1 components of `...` were not used.

We detected these problematic arguments:
* `args`

Did you misspecify an argument? 

@peijianying
Copy link

peijianying commented Oct 11, 2022 via email

@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