R and RStudio installation and package error

Solution 1:

I don't know why R 3.4.1 changes the library path. To change it back, simply edit /etc/R/Renviron:

$ sudo gedit /etc/R/Renviron

Comment-out R_LIBS_SITE=... then uncomment R_LIBS_USER=...

# edd Jun 2017  Comment-out R_LIBS_USER
R_LIBS_USER=${R_LIBS_USER-'~/R/x86_64-pc-linux-gnu-library/3.4'}

# edd Apr 2003  Allow local install in /usr/local, also add a directory for
#               Debian packaged CRAN packages, and finally the default dir 
# edd Jul 2007  Now use R_LIBS_SITE, not R_LIBS
#R_LIBS_SITE=${R_LIBS_SITE-'/usr/local/lib/R/site-library:/usr/lib/R/site-library:/usr/lib/R/library'}  

Solution 2:

This answer is about installing additional R packages within R and RStudio. If you are looking for how to install R and RStudio see Installing RStudio -- is this very different from other packages?

Two Ways to Install R packages

I do not recommend RStudio or R with sudo privileges. Running a single R command with sudo like sudo R INSTALL fGarch should be fine. The two methods described below are other alternatives.

1. R package as Ubuntu software

As you use R, you will need to install other R packages. Sometimes these are available in the repositories. Say the r package you want it fGarch then the package in the repositories is called r-cran-fGarch. Open a terminal by pressing Cntrl+Alt+T and enter:

sudo apt install r-cran-fGarch

Installing R packages in this way has two benefits:

  1. It will keep fGarch up-to-date. Whenever fGarch is updated in R-Cran repository, it will be updated in your computer along with the system update process.
  2. All users of this computer will be able to use the fGarch package within R and RStudio.

However, not every R package has a corresponding precompiled installation deb package in the Cran repository. fGarch just happens to to one of them. So, the above method does not work for this package.

2. R Package from inside RStudio

R packages such as fGarch can be easily installed and updated from inside RStudio. If you do this, you will see the error message and find that RStudio puts the new files (including updates) inside in your home directory /home/<user>/R/site-library/. This is fine if you are the only user of this computer who use R and RStudio. The "warning" you see will not stop you from using the package fGarch. However, other users of this computer won't have access to it.

One way to solve this problem and let RStudio install all future additional packages in their proper place is to add yourself to the group staff. Open a terminal by pressing Cntrl+Alt+T and enter:

sudo adduser <user> staff

Replace <user> with your username.

After this, you will get the option of installing or updating packages /usr/local/lib/R/site-library/ or in /home/<user>/R/site-library/. Then you will be able to choose the former as the default install location.

Source: https://stackoverflow.com/questions/5560139/install-r-package-xml-in-debian-ubuntu

Hope this helps

Solution 3:

If you want to install the R add-on package for all users to /usr/local/lib you'll need to give R super-user privileges to create files there. To install R add-on packages as super-user run:

sudo R CMD INSTALL <PACKAGES…>

where you replace <PACKAGES…> by the names of any number add-on packages to install.


If you want to install a package for yourself only you don't need to give R super-user privileges but you need to specify an installation target. One convenient way to do this is with an environment variable:

export R_LIBS_USER=<PATH>

where you replace <PATH> by the path to a directory for your user-specific R packages. I recommend ~/.local/lib/R/site-library. Now you can install the packages:

R CMD INSTALL <PACKAGES…>

You'll need to assure that this environment variable is set before starting R or it won't find the packages installed to that location. The best option would be to append the above export command to the file ~/.profile. See Environment Variables for more.