I can't install any package on Linux (Ubuntu) with the install.packages() function!

Together with a seasoned Linux user at my institution, we managed to solve the issue - Thanks, Dr Ayan Sengupta!

Below is the "recipe" that worked for me.

Prune away your library paths

The problem was likely caused by some conflicts in the folder tree of R's library. The easiest thing I could do was to delete the library's folders.

Instructions

  1. Open the terminal.
  2. Run R -e '.libPaths()'.
    • The output will look something like this:
> .libPaths()
[1] "/usr/local/lib/R/site-library"                  
[2] "/usr/lib/R/site-library"                        
[3] "/usr/lib/R/library"
  1. Remove, one by one, all the directories you just printed out.
    • for example, run sudo -rm -r /usr/lib/R/library.

Start from a blank slate

Now, you should uninstall R and RStudio completely.

Instructions

To do so, open the terminal and run in sequence:

sudo apt-get remove r-base-core
sudo apt-get remove r-base
sudo apt-get remove r-studio
sudo apt-get autoremove

If necessary, remove any R and RStudio folders that the process left behind.

Re-install R

Install R again and make sure to install the utilities too. [^1]

Instructions

You can follow Nick Zeng's guide.

Install the packages you need

At this point you should be able to install your packages. I will use the tidyverse package as an example.

Instructions

  1. Open the terminal.
  2. Type R and press enter.
    • You just accessed the R console and can run R commands from the terminal.
  3. Run install.packages("tidyverse").
  4. Wait for the installation to finish and check with library("tidyverse")

Conclusions

You should be able to access the package-specific functions now!
If install.package("my_package") did not work properly, you might want to try its verbose version: specify the repos and dependencies arguments inside brackets.

Best wishes,
Valerio

[^1]:
At the time of writing, I have not installed the RStudio IDE yet. This is a secondary issue that is not strictly related to the present post.

3 Likes