The purpose of this document is to assist people in installing R, RStudio, R packages, and related software. The process should be straightforward and fast ... at least in an ideal world. I have been through this process many times over the years and I don't recall a single instance where I have experienced an error-free installation. Once you encounter errors, the debugging process can be very frustrating and time-consuming. Websites like Stack Overflow are very helpful in solving many of the errors you may encounter. But even that takes time to search for solutions, to understand if and how they apply to your use case, and ultimately the proposed solution may not work for you. Much of the Troubleshooting section is based on advice from Stack Overflow which I have collated, synergised, and streamlined here. It is my hope that this guide saves users hours of frustration.
I plan to update this document periodically. If you use this guide and find any mistakes or have any suggestions, please contact me at earl.w.duncan@gmail.com.
For Windows:
This will install the 32-bit version on 32-bit versions of Windows, and both 32-bit and 64-bit versions on 64-bit verions of Windows. A default GUI is also provided with this installation. However, I highly recommend using RStudio which provides a more user-friendly GUI, but being an integrated development environment (IDE), it also provides a lot of helpful tools and features for developing pacakges, markdown documents, etc. To install RStudio, see the instructions below).
For more advanced instructions, refer to the link at the top of the Windows download page.
For MacOS:
Note: make sure R is installed before installing RStudio.
For Windows:
For MacOS:
For other operating systems:
Packages on the comprehensive R Archive Network (CRAN) can be installed using the function install.packages
. E.g.
# A single package
install.packages("ggplot2")
# Multiple packages as a character vector
install.packages(c("ggplot2", "gridExtra", "tidyr"))
or installed from the RStudio menu: Tools > Install Packages... .
Sometimes it is necessary to download and install the packages on which your primary package depends, called "dependencies". This can be accomplished by changing the dependencies
argument:
install.packages("ggplot2", dependencies = TRUE)
If you see an error message about the package not being available, you may be able to resolve this by explicitly setting the repos
(repository) argument. Usually, this will be the main CRAN mirror site:
install.packages("ggplot2", repos = "http://cran.rstudio.com")
For more tips and troubleshooting this type of error, see Section 4.1.1.
Once a package has been installed, you need to load each library. E.g.
library(ggplot2)
library(gridExtra)
library(tidyr)
You can also unload libraries:
detach("package:ggplot2", unload = TRUE)
Option 1:
Install using the install.packages
function as follows:
install.packages(filepath, repos = NULL, type = "source")
# or
install.packages(filepath, repos = NULL, type = "win.binary")
where filepath
is a string containing the full path and file name, e.g. "C:/tmp.tar.gz"
or "C:/tmp.zip"
.
Option 2:
Install using the RStudio menu:
This requires the devtools
library to be installed and loaded (from CRAN) first. E.g.
library(devtools)
devtools::install_github("twitter/AnomalyDetection")
devtools::install_github("AckerDWM/gg3D")
Alternatively, you can download and install a package in two seperate steps.
First, download the compressed file from GitHub using utils::download.file
, e.g.:
utils::download.file("https://github.com/yihui/testit/archive/master.zip",
destfile = "tmp.zip")
# or
utils::download.file("https://github.com/yihui/testit/archive/master.tar.gz",
destfile = "tmp.tar.gz")
or download it from the GitHub webpage manually through your browser:
Second, install from the local source file you just saved by following the instructions in Section 2.2.
If the source file is located on a website, you can install it directly from the website using install_url
from the devtools
pacakge.
library(devtools)
devtools::install_url(source_URL)
where source_URL
is a string containing the URL, e.g. "http://wrap.warwick.ac.uk/71934/1/mcclust.ext_1.0.tar.gz".
Sometimes you may encounter an error when installing packages to the default library path (directory). Typically, this default path looks like: C:/Program Files/R/R-3.5.2/library
. You can check the library path using .libPaths()
.
Due to folder permission read-write issues, for example, you may want to change this. For illustration, suppose you want to change this to C:/My R Library
.
Option 1: Edit the Rprofile.site file
This option should really only be used in conjunction with the default R GUI. For RStudio or other IDEs, this solution probably won't work (or will reset to defaults after restarting the IDE) - see alternative options below.
C:/Program Files/R/R-3.6.1/etc
.assign(".lib.loc", "C:/My R Library", envir = environment(.libPaths))
Option 2: Edit the library path using .libPaths()
Append your personal library as the default path: .libPaths(c("C:/My R Library", .libPaths()))
Option 3: Change environment variables
Instructions for Windows 7 (other Windows OS will be similar; for other OS like Linux, see the URL below):
Variable name: R_LIBS_USER
Variable value: C:/My R Library
This is a guide to troubleshooting common errors and warnings encountered when installing and loading packages.
Note:
XXX
', 'YYY
', and 'ZZZ
' are used as placeholder names for packages.package 'XXX' is not available (for R version 3.6.1)
install.packages("ggplot2", repos = "http://cran.rstudio.com")
. This will often resolve the problem of workplace firewalls that block CRAN sites.install.packages("XXX", repos = "http://r-forge.r-project.org")
.DPpackage
was removed from CRAN (http://cran.r-project.org/web/packages/DPpackage/index.html).repos
and type
are correct: install.packages(filepath, repos = NULL, type = "source")
To check or confirm what repository the package is in:
library(magrittr) # For the pipe operator, %>%
available.packages() %>% data.frame() %>% subset(Package == "XXX") %>% t()
For example, ggplot2
lies in the CRAN repository (https://cran.rstudio.com). To change the repositories that R will search, use setRepositories()
.
Error in library("XXX") : there is no package called 'XXX'
This happens when you try to load a library that doesn't exist. E.g. library("nonexistent_pacakge_name")
.
Option 1:
p <- installed.packages()[,1]
is.element("XXX", p)
Option 2: Navigate to your library path (i.e. the directory returned by .libPaths()
) and check that there is a folder with the package name. If not, then install the package first.
Error: Failed to install 'unknown package' from ...:
(converted from warning) installation of package 'XXX' had non-zero exit status
dependencies = TRUE
.This is a fairly generic error message, and thus it is difficult to offer specific solutions. Read the accompanying text for hints as to the root cause of the problem.
Error in install.packages : unable to install packages
This error is usually (always) accompanied by another error message. For example, it may be preceded by 'lib = "..."' is not writable
. Please refer to the troubleshooting for the accompanying error message.
Error: package or namespace load failed for 'XXX':
.onLoad failed in loadNamespace() for 'YYY', details:
call: ...
error: '...' is not an exported object from 'namespace:ZZZ'
or
Error: package or namespace load failed for 'XXX':
package 'ZZZ' was installed by an R version with different internals; it needs to be reinstalled for use with this R version
or
Error: package or namespace load failed for 'XXX' in loadNamespace(...):
namespace 'ZZZ' 0.3.0 is already loaded, but >= 0.4.0 is required
These errors indicate a compatibility issue, where package 'XXX
' requires an updated version of package 'ZZZ
', but the latter is not being updated. (Ignore the intermediate package 'YYY
'.)
XXX
' again with dependencies = TRUE
.ZZZ
' and its dependencies manually, e.g. install.packages("ZZZ", dependencies = TRUE)
.
library(ZZZ)
before trying library(XXX)
again.Note: if the steps above don't solve the problem, try executing them in R rather than RStudio.
ERROR: compilation failed for package 'XXX'
* removing '.../XXX'
If you install packages that require compilation, Rtools must be installed.
Rtools is required to build R packages, but is not currently installed
). See section 3.1.install.packages("XXX")
. If that fails, try installing from binary, e.g. install.packages("XXX", type = "binary")
.Error in utils::download.file(url, path, method = method, quiet = quiet, :
unsupported URL scheme
This may be accompanied by Warning: unable to access index for repository https://...
and/or package 'XXX' is not available (for R version 3.6.1)
This may be a firewall/proxy issue (where CRAN mirror sites are blocked).
install.packages("XXX", dependencies = TRUE, repos = "http://cran.rstudio.com/")
. For a more permanent solution, use options(repos = "http://cran.rstudio.com/")
.Downloading GitHub repo ...@master
Error in utils::download.file(url, path, method = method, quiet = quiet, :
cannot open URL 'https://api.github.com/repos/.../master'
This may happen when trying to install a package from github, e.g.
devtools::install_github(repo)
where repo
is the repository address (see ?install_github
).
To see if this problem is specific to this package, or github more broadly, try this test:
devtools::install_github("yihui/testit")
method
(in utils::download.file
called from utils::install.packages
called from install_github
) which is auto
, and this translates into wininet
on Windows machines. Try changing this to one of the other methods (libcurl
, wget
, or curl
). E.g. devtools::install_github("XXX", method = libcurl)
unable to access index for repository https://cran.rstudio.com/src/contrib
cannot open URL 'https://cran.rstudio.com/src/contrib/PACKAGES'
or
trying URL '...'
Error in utils::download.file("https://github.com/repos/...", :
cannot open URL 'https://github.com/repos/...'
install.packages("XXX", repos = "http://cran.rstudio.com")
.InternetOpenUrl failed: 'An error occurred in the secure channel support'
This suggests a firewall/proxy issue.
install.packages("XXX", repos = "http://cran.rstudio.com/")
.method
. E.g. utils::download.file(..., method = "libcurl")
, or a more permanent solution: options(download.file.method = "libcurl")
. Available methods are: wininet
, libcurl
, wget
, and curl
. (This may change the error to HTTP status was '404 Not Found'
.)Installing package into '...' (as 'lib' is unspecified)
This means the packages will be installed into the directory shown because you didn't specify it. This is usually fine. If you need to change the library directory, see Section 2.5.
Warning in install.packages :
'lib = ...' is not writable
When R/RStudio is unable to write to the specified (or default) library path, it usually informs you where the binary source files are located. So R was able to download the source (zipped) files, it just wasn't able to install them. You can try manually installing these packages, but you will probably encounter the same folder permission issue, and this will not solve the root problem.
The above error message may be succeeded by:
Would you like to use a personal library instead? (yes/No/cancel)
If you select "no" you will see the error: Error in install.packages : unable to install packages
. Instead, you shouuld select "yes" and locate your personal library to install the packages to. For a more permanent solution in changing the library path, see Section 2.5.