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

My setup

Below I report the details of the software I am running on my local machine.

Linux

This is the output of the lsb_release -a command when I run it in my terminal.

No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 20.04.2 LTS
Release:	        20.04
Codename:	focal

R

This is the output of the R --version command when I run it in my terminal.

R version 4.0.4 (2021-02-15) -- "Lost Library Book"
Copyright (C) 2021 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under the terms of the
GNU General Public License versions 2 or 3.
For more information about these matters see
https://www.gnu.org/licenses/.

RStudio

I am running RStudio Version 1.4.1106.

The problem

Whenever I try to install a package with the install.packages() function in RStudio, the console returns errors. This behaviour is consistent across packages, as I tried installing tidyverse, here, devtools, usethis, reticulate, etc.

The error output

Below is the output when I run install.packages("here") and install.packages("tidyverse"), but the same happens with any package.

The here package

Installing package into ‘/home/valerio/R/x86_64-pc-linux-gnu-library/4.0’
(as ‘lib’ is unspecified)
trying URL 'https://cloud.r-project.org/src/contrib/here_1.0.1.tar.gz'
Warning in install.packages :
  URL 'https://cloud.r-project.org/src/contrib/here_1.0.1.tar.gz': status was 'Couldn't resolve host name'
Error in download.file(url, destfile, method, mode = "wb", ...) : 
  cannot open URL 'https://cloud.r-project.org/src/contrib/here_1.0.1.tar.gz'
Warning in install.packages :
  download of package ‘here’ failed

The tidyverse package

Warning in install.packages :
  installation of package ‘tidyverse’ had non-zero exit status

My troubleshooting attempts

I have tried many things so far, but they were all unsuccessful.

  • I followed the Linuxize's guide.
  • I used the verbose version of the function install.packages(), specifying the arguments repos and dependencies.
  • I run both sudo apt update and sudo apt upgrade in the terminal.
  • I also run sudo apt install r-base-dev.
  • I run sudo apt-get install build-essential too.
  • I used sudo apt install for libcurl4-openssl-dev, libssl-dev, and libxml2-dev.
  • Not to mention the sudo apt-get install g++.
  • I opened the console as a root with sudo -i R and try installing the packages from there.
  • I changed the primary CRAN repository from the RStudio's Global Options (Packages tab).
  • I unticked the use secure download method for HTTP box from the RStudio's Global Options (Packages tab).
  • I restarted RStudio at each step and/or closed it and re-opened it.
  • I also tried installing packages from the RStudio's toolbar (Tools > Install Packages...).

I ended up in such a deep rabbit hole that I cannot even remember how many fixes I tried and in which combination.
I also tried using the code below:

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9

This did not solve the issues either.

Help, please!

Nothing worked thus far and I do not know what to do. I am by no means an expert Linux user and hope that someone in the community could help me with this nightmare.

Thanks, I look forward to hearing from you.

Best wishes,
Valerio

1 Like

Hi , sounds like you've had a hard time of it !
can you verify that your machine can browse to the https link? that would clear up that in general your system can get there.
After that, I would be interested to see what your R session responds with the command

getOption("download.file.method")

Regards,
Nir

Dear Nir,
thanks for your help.

https

I copy/pasted the https://cloud.r-project.org/src/contrib/here_1.0.1.tar.gz URL in my browser (Chromium) and I could download the .tar.gz file with no problems.

getOption()

This is the output when I run getOption("download.file.method") in RStudio:

NULL

All the best,
Valerio

As Nir suggested, for some reason R is unable to resolve the url for the download. Two possible solutions:

  1. Try a different mirror.
  2. Use the c2d4u PPA that I have created to install most of your R package needs via Ubuntu. Link :https://launchpad.net/~c2d4u.team/+archive/ubuntu/c2d4u4.0+

If you use option 2, check out the bspm pacakge, which allows R to use apt when install.package is used inside R.

Perhaps you could try different download methods to see if any work, on your system
R: Download File from the Internet (ethz.ch)
Perhaps installing a system utility to provide one of "libcurl" , "wget" and "curl" may be necessary.
I'm not very well versed in this sysadmin area though I'm afraid...

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

To install RStudio, I followed the guide below and had no issues.

Dear all,
thanks for your help, this is what I have tried so far.

Try a different download method

I used the download.file("https://here.r-lib.org/", destfile = "~/Downloads/") command inside the RStudio console and this is the output.

Warning messages:
1: In download.file("https://here.r-lib.org/", destfile = "~/Downloads/") :
  URL https://here.r-lib.org/: cannot open destfile '~/Downloads/', reason 'Is a directory'
2: In download.file("https://here.r-lib.org/", destfile = "~/Downloads/") :
  download had nonzero exit status

The system utilities wget and curl were already installed and up to date. I tried installing libcurl too, but the process aborts.

(base) valerio@PCLINUX1096:~$ sudo apt-get install libcurl
[sudo] password for valerio: 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
E: Unable to locate package libcurl

Try a different CRAN mirror

I tried several mirrors: I picked some of those at random, but I also tried using those closest to me (London and Bristol mirrors). The error keeps on popping out.

Use the c2d4u PPA

@Michael_A_Rutter, I have not tried the PPA method yet (your solution n. 2), but I am looking into it. I am not familiar with the whole set-up process.

Other information

I read many useful comments from advanced users online. Apparently, one potential cause of my issues might be related to the connection to the R CRAN website.
Below I report the output of theping cloud.r-project.org command when I run it in my terminal.

PING d3caqzu56oq2n9.cloudfront.net (52.84.92.75) 56(84) bytes of data.
64 bytes from server-52-84-92-75.lhr62.r.cloudfront.net (52.84.92.75): icmp_seq=1 ttl=243 time=3.15 ms
64 bytes from server-52-84-92-75.lhr62.r.cloudfront.net (52.84.92.75): icmp_seq=2 ttl=243 time=3.40 ms
64 bytes from server-52-84-92-75.lhr62.r.cloudfront.net (52.84.92.75): icmp_seq=3 ttl=243 time=3.40 ms
64 bytes from server-52-84-92-75.lhr62.r.cloudfront.net (52.84.92.75): icmp_seq=4 ttl=243 time=3.37 ms
64 bytes from server-52-84-92-75.lhr62.r.cloudfront.net (52.84.92.75): icmp_seq=5 ttl=243 time=3.48 ms
64 bytes from server-52-84-92-75.lhr62.r.cloudfront.net (52.84.92.75): icmp_seq=6 ttl=243 time=3.44 ms
64 bytes from server-52-84-92-75.lhr62.r.cloudfront.net (52.84.92.75): icmp_seq=7 ttl=243 time=3.45 ms

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.