Question: After installing R and RStudio in Ubuntu 16.04 with the following code:

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

```

and

```
sudo apt-get install gdebi-core
wget https://download1.rstudio.org/rstudio-1.0.143-amd64.deb
sudo gdebi rstudio-1.0.143-amd64.deb

```

I am trying to install packages and I am getting errors in R console like

```
install.packages("fGarch")
Installing package into ‘/usr/local/lib/R/site-library’
(as ‘lib’ is unspecified)
Warning in install.packages :
  'lib = "/usr/local/lib/R/site-library"' is not writable
```

As I am new in Linux I do not know in which directory install the R packages.
I am not sure whether this is the problem or if I did any mistake on the installation. Can anyone help me with this issue? Thanks in advance.

Answer: 
**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?](https://askubuntu.com/questions/919275/installing-rstudio-is-this-very-different-from-other-packages/919288#919288)

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
Try opening up R through the terminal under **sudo**. This will require you typing in your password and will allow you to give permissions to write over files in your system. [Picture](https://i.stack.imgur.com/ceVfa.png)

Edit: See picture for clarity