# Installing R and JAGS on Ubuntu OS

HOME I recently tried installing R and JAGS on my machine running Ubuntu. I wanted to test my BEST and wiqid packages with the new version of JAGS on Ubuntu. It took me a while, but I finally found a simple way to do this which might be of interest to others.

I already had R and JAGS 3 installed, together with the rjags package version 3. Installing the rjags package within R (or updating it with update.packages()) installs the new version of rjags, v.4, which requires JAGS 4 and throws an error if it isn't found. But the Ubuntu repository still has JAGS 3, so you cannot update JAGS with Ubuntu Software Center.

I was running the Long Term Support (LTS) version of Ubuntu, Trusty, so decided to upgrade to the latest, Wily, before going further, but I found that crashed regularly, so reinstalled Trusty. At this point I had a clean OS install.

The simplest way to install R, JAGS and rjags is to use Michael Rutter's Personal Package Archive (PPA). Update 2017-05-23: Check Michael's page for the latest info: look for "r-cran-rjags" which is available for Trusty and Xenial. You can do it all with 3 lines of code in the Terminal:

sudo add-apt-repository ppa:marutter/rrutter
sudo apt-get update
sudo apt-get install r-cran-rjags

Installing r-cran-rjags in this way will automatically install both R and JAGS 4.

Update 9 Aug 2017: With a fresh install of Ubuntu 16.04.3 LTS (Xenial), I ran the above 3 lines, then in R did library(rjags). I got an error saying that /usr/lib/JAGS/whatever.so could not be found, and indeed the directory didn't exist. I restarted R and reinstalled rjags with  install.packages("rjags") and replied y to install in a personal library. It then worked correctly with latest versions of R, JAGS and rjags.

#### If you already have JAGS 3 installed

I actually tried to install JAGS 4 on the machine before reinstalling Ubuntu. On Windows, JAGS 4 is installed in a separate folder to JAGS 3, so you can happily have both on your machine and both old and new versions of rjags will work. On Ubuntu, the new obliterates the old ... mostly! When I tried this, I got an error when I tried to load rjags in R: "unable to load shared object: ... rjags.so" and a further note about an "undefined symbol".

The apparent reason for this was components of JAGS 3 which had not been removed when JAGS 4 was installed, as explained by Martyn Plummer here. So I uninstalled and purged, and then checked for remaining files:

sudo apt-get --purge autoremove jags
/sbin/ldconfig -p | grep jags -

I still had stuff in /usr/local/lib, so navigated to there and manually deleted the JAGS directory and all the jagslib* files (with sudo rm -r as they are read-only). Then reinstalled JAGS 4 from Michael Rutter's PPA and it all worked.

#### Installing RStudio

In Windows, I like to use Notepad++ with NppToR, but those aren't available for Linux distros, so on Ubuntu I use RStudio.

Update 9 Aug 2017: With Xenial, that didn't work; I needed to install libjpeg62 first. I failed to do that with apt-get, so downloaded the libjpeg62 installer from packages.ubuntu.com and the RStudio installer as above, then did:

cd Downloads
dir

That gave me the names of the installers that I could copy/paste into the following commands:

sudo dpkg -i <name-of-libjpeg62.deb-installer>
sudo dpkg -i <name-of-RStudio.deb-installer>

Once that was done, I could run RStudio normally.

Updated 9 Aug 2017 by Mike Meredith