To install the R programming environment on Linux is pretty straightforward, but it does require a little bit of know how in order to find the correct packages. As is typically the case with Linux, there are multiple ways to get things done. I like to use Synaptic for installing and removing software, but you can also use the software manager that comes with your Linux distribution (in Linux Mint it’s called Software Manager) or the command line (in KDE based distributions, Konsole).
For the most up-to-date installation of R, it’s actually best to install directly from the R repository. A list of Linux repositories for the R environment is located here. In order to install from the repository, you need to update your list of repositories in Synaptic. To access your repository list in Synaptic, click on Settings -> Repositories.
In the new Software Sources window, click on “Additional repositories” and you’ll get this window:
Click on Add a new repository. You’ll get this window:
The exact information you put into that window will vary based on which mirror you chose. Here is what I added in mine:
deb https://cran.cnr.berkeley.edu/bin/linux/ubuntu xenial/
In order to ensure you have the right files and to follow best security practices, you should install the signing key as well. Directions for installing the signing key are found here, but it can be done with a simple command from a terminal:
sudo apt-key adv –keyserver keyserver.ubuntu.com –recv-keys E084DAB9
Once you have done all of that, you can install R from Synaptic.
First, open Synaptic, which will require your password. You’ll get the basic Synaptic Package Manager window:
Next, in the search box, search for “r-base”. Right-click it and select “Mark for installation” to install “r-base”:
In the above screenshot, I have already installed r-base, so the option “Mark for installation” is greyed out. But, obviously, that’s what I already did. When you select this, Synaptic will automatically select all the other necessary packages (there are about 10 to 15 additional packages necessary for R to run: r-cran-class, r-cran-lattice, r-cran-spatial, r-cran-survival, r-cran-codetools, r-cran-nnet, r-cran-mass, r-cran-boot, r-cran-nlme, r-cran-rpart, r-cran-cluster, r-cran-kernsmooth, r-cran-foreign, r-cran-mgcv, r-cran-matrix, r-recommended, r-base-core).
If you plan on installing any other R packages, it’s not a bad idea to also install “r-base-dev,” as it helps fill in dependencies for other packages.
Once you’ve selected r-base, hit Apply in Synaptic and all the software will be installed.
You now have the base software for R installed.
To open the R environment in a terminal, launch a terminal and simply type “R” at the prompt, like this:
Here’s where things can get a little complicated. To do different things in R requires various libraries or packages. Some of these can be installed using the R terminal while others need to be installed from your Linux distribution’s repositories. To install a library or package using the R terminal, you use the following command once you have opened the R environment:
The first time you run this, the R environment will ask you to select a mirror.
Choose one close to your location. R will then install the package, assuming you type everything correctly.
Before you start trying to install additional R packages, it’s a very good idea to install the following Linux packages:
If you run into an error message, there are several possibilities. First, check to make sure you typed everything correctly. R is not forgiving on spelling mistakes. Second, if the error is something like:
installation of package ‘PACKAGENAME’ had non-zero exit status
dependency ‘PACKAGENAME’ is not available
There is a good chance that you need to install a package or library using Synaptic (or from a terminal using apt). For instance, to install the “tm” package, there is an unsatisfied dependency (meaning, a library or package that needs to be installed but cannot be installed using the R installer). The dependency is the ‘slam’ package. This can be installed using Synaptic (or, from a terminal, using the command “sudo apt-get install r-cran-slam”). Once you’ve installed the dependency, try re-installing the package and the error messages should go away.
I also have found that I like RStudio as an IDE for working with R. It’s a little bit friendlier to use than a straight command line interface as it keeps track of variables and loaded libraries. The personal version for your desktop can be downloaded here.
And a note on RStudio on Linux. I regularly get an offset from the cursor location and where the cursor actually is in the command window. It turns out this is a font issue. If you go up to Tools -> Global Options -> Appearance and change the font to anything else, this problem will go away.