Wednesday, October 9, 2019

H2B SOTU-db Step 2: Installing R

This series of posts documents the process of getting SOTU-db back online after losing access to the LUC servers upon which it was originally hosted.

After getting my Ubuntu virtual machine up and running, I'll need to install some software onto the machine that is not contained in the SOTU-db project repo. The two main programs I'll need are the web server Apache and the statistical analysis package R. I've decided to install R first.

Installing R

Installing R is pretty straightforward once you know the fundamentals of installing software in Ubuntu. The command line installation guide provided by CRAN (the organization that distributes R) is pretty helpful. I'll walk through it here:
  1. add the CRAN repositories to sources.list
Basically, this step tells Ubuntu about the remote servers where it can find R. So, first I'll open an SSH shell into my VM, through the Google Cloud Engine dashboard. Then, I'll navigate over to the folder I want with cd /etc/apt/. Now comes the tricky part... which text editor do I use? How long until I get stuck or accidentally erase the whole file? Let's try nano... sudo nano ./sources.list. That works... the Cloud Engine console even allows me to paste from the Windows clipboard with CTRL+SHIFT+V. So, I'll just plop in the source listing that corresponds to my version of Ubuntu:
deb xenial-cran35/
and save, or CTRL+O for "WRITE OUT" just to remind me why I hate Linux text editors, then CTRL+X to exit, and we should be good on step 1!

STEP 2: Install the "Complete R System"
This step is really simple:

sudo apt-get update
sudo apt-get install r-base

STEP 3: Go back a step, add security keys and another repo

This is where the R setup process always drives me crazy, and is an issue with countless Linux installation guides: running the apt-get update command from Step 2, above, results in a warning that the public key for the CRAN repo that we added in Step 1 is not available. If you scroll down the R installation guide, it tells you how to address this, and also mentions that "Installation and compilation of R or some of its packages may require Ubuntu packages from the “backports” repositories..." So:

STEP 3A: Add the Backports repo

I'm adding this to the same sources.list file as before, which now has two entries that look like this:

STEP 3B: Add public keys for R packages
Keep this step simple by just running
sudo apt-key adv --keyserver --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9
Now we can go back to Step 2 and run those commands again. I'm actually still getting a couple warnings on the update, but they seem ignorable (such as backports not having a "release" file, which seems to make sense to me). And install r-base ran without any errors.

STEP 4: Install r-base-dev
Finally, we're going to install some additional developer packages. The guide says "Users who need to compile R packages from source [e.g. package maintainers, or anyone installing packages with install.packages()] should also install the r-base-dev package," and that's us, so here we go. This should be a simple matter of running
sudo apt-get install r-base-dev
Hm, interestingly, it's telling me I already have the newest version of r-base-dev installed. I wonder if adding in the backports repo before installing r-base had something to do with it?

STEP 5: Profit
Now I can run R (not r) and see that the program runs successfully. Next stop, Apache!