Skip to content

Instantly share code, notes, and snippets.

@casallas
Last active January 31, 2024 23:16
Show Gist options
  • 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'
                            "
                      )
@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