Debian Codegraph @

inst/doc/package-install.Rmd

raw
1 ---
2 title: "Installing packages"
3 output: rmarkdown::html_vignette
4 vignette: >
5 %\VignetteIndexEntry{Installing packages}
6 %\VignetteEngine{knitr::rmarkdown}
7 %\VignetteEncoding{UTF-8}
8 ---
9
10 ```{r, include=FALSE}
11 knitr::opts_chunk$set(
12 collapse = TRUE,
13 comment = "#>",
14 eval = FALSE
15 )
16 ```
17
18 Package installation is an important part of renv.
19 This vignette gives you the details, starting with an overview of renv's cache, before going into various installation challenges around building from source and downloading with proxies or authentication.
20
21 ## Cache
22
23 One of renv's primary features is the global package cache, which shared across all projects.
24 The renv package cache provides two primary benefits:
25
26 1. Installing and restoring packages is much faster, as renv can find and re-use previously installed packages from the cache.
27
28 2. Projects take up less disk space, because each project doesn't need to contain it's own copy of every package.
29
30 When installing a package, renv installs into the global cache and then adds a symlink[^1] to that directory in the project library.
31 That way each renv project remains isolated from other projects on your system, but they can still re-use the same installed packages.
32
33 [^1]: Or junction points, on Windows.
34 Junction points are unfortunately not supported on Windows network shares; see [Hard links and junctions](https://learn.microsoft.com/en-us/windows/win32/fileio/hard-links-and-junctions) for more details.
35
36 The process by which packages enter the cache is roughly as follows:
37
38 1. Package installation is requested via e.g. `install.packages()`, or `renv::install()`, or as part of `renv::restore()`.
39
40 2. If renv is able to find the requested version of the package in the cache, then that package is linked into the project library, and installation is complete.
41
42 3. Otherwise, the package is downloaded and installed into the project library.
43
44 4. After installation of the package has successfully completed, the package is then copied into the global package cache, and then symlinked into the project library.
45
46 In some cases, renv will be unable to directly link from the global package cache to your project library, e.g. if the package cache and your project library live on different disk volumes.
47 In such a case, renv will instead copy the package from the cache into the project library.
48 This is much slower, so is worth avoiding.
49
50 ### Cache location
51
52 You can find the location of the current cache with `renv::paths$cache()`.
53 By default, it will be in one of the following folders:
54
55 - Linux: `~/.cache/R/renv/cache`
56 - macOS: `~/Library/Caches/org.R-project.R/R/renv/cache`
57 - Windows: `%LOCALAPPDATA%/renv/cache`
58
59 If you'd like to share the package cache across multiple users, you can do so by setting the `RENV_PATHS_CACHE` environment variable to a shared path.
60 This variable should be set in an R startup file to make it apply to all R sessions.
61 While you can set it in a project-local `.Renviron`, or the user-level `~/.Renviron`, we generally recommend using the R installation's site-wide `Renviron.site` if you'd like to ensure the same cache path is visible to all users of R on a system.
62
63 You may also want to set `RENV_PATHS_CACHE` so that the global package cache can be stored on the same volume as the projects you normally work on.
64 This is especially important when working projects stored on a networked filesystem.
65
66 ### Multiple caches
67
68 It is also possible to configure renv to use multiple cache locations.
69 For example, you might want to make both a user-local package cache, as well as a global administrator-managed cache, visible within an renv project.
70 To do so, you can specify the paths to the cache separated with a `;` (or `:` on Unix if preferred).
71 For example:
72
73 ```sh
74 RENV_PATHS_CACHE=/path/to/local/cache;/path/to/global/cache
75 ```
76
77 In such a case, renv will iterate over the cache locations in order when trying to find a package, and newly-installed packages will enter the first writable cache path listed in `RENV_PATHS_CACHE`.
78
79 ### Shared cache locations
80
81 When the renv cache is enabled, if that cache is shared and visible to multiple users, then each of those users will have an opportunity to install packages into the renv cache.
82 However, some care must be taken to ensure that these packages can be used by different users in your environment:
83
84 1. Packages copied into the cache may have [Access-control Lists](https://en.wikipedia.org/wiki/Access-control_list) (ACLs), which might prevent others from using packages that have been installed into the cache.
85 If this is the case, it's important that ACLs be set (or updated) on cache entries so that the cache is accessible to each user requiring access.
86 When deploying renv in an enterprise environment, the system administrator should take care to ensure ACLs (if any) allow users access to packages within the renv cache.
87
88 2. By default, packages copied into the cache will remain "owned" by the user that requested installation of that package.
89 If you'd like renv to instead re-assign ownership of the cached package to a separate user account, you can set the `RENV_CACHE_USER` environment variable.
90 When set, renv will attempt to run `chown -R <package> <user>` to update cache ownership after the package has been copied into the cache.
91
92 ### Caveats
93
94 While we recommend enabling the cache by default, if you're having trouble with it, you can disable it by setting the project setting `renv::settings$use.cache(FALSE)`.
95 Doing this will ensure that packages are then installed into your project library directly, without attempting to link and use packages from the renv cache.
96
97 If you find a problematic package has entered the cache (for example, an installed package has become corrupted), that package can be removed with the `renv::purge()` function.
98 See the `?purge` documentation for caveats and things to be aware of when removing packages from the cache.
99
100 You can also force a package to be re-installed and re-cached with the following functions:
101
102 ```{r}
103 # restore packages from the lockfile, bypassing the cache
104 renv::restore(rebuild = TRUE)
105
106 # re-install a package
107 renv::install("<package>", rebuild = TRUE)
108
109 # rebuild all packages in the project
110 renv::rebuild()
111 ```
112
113 See each function's respective documentation for more details.
114
115 ## Building from source
116
117 Where possible, renv will install package binaries, but sometimes a binary is not available and you have to build from source.
118 Installation from source can be challenging for a few reasons:
119
120 1. Your system will need to have a compatible compiler toolchain available.
121 In some cases, R packages may depend on C / C++ features that aren't available in an older system toolchain, especially in some older Linux enterprise environments.
122
123 2. Your system will need requisite system libraries, as many R packages contain compiled C / C++ code that depend on and link to these packages.
124 On Linux, `renv::sysreqs()` can help identify which system packages are required by your project.
125 See `vignette("docker")` for an example of using `renv::sysreqs()` in a Dockerfile.
126
127 ### Configure flags
128
129 Many `R` packages have a `configure` script that needs to be run to prepare the package for installation.
130 Arguments and environment variables can be passed through to those scripts in a manner similar to `install.packages()`.
131 In particular, the `R` options `configure.args` and `configure.vars` can be used to map package names to their appropriate configuration.
132 For example:
133
134 ```{r}
135 # installation of RNetCDF may require us to set include paths for netcdf
136 configure.args = c(RNetCDF = "--with-netcdf-include=/usr/include/udunits2")
137 options(configure.args = configure.args)
138 renv::install("RNetCDF")
139 ```
140
141 This could also be specified as, for example,
142
143 ```{r}
144 options(
145 configure.args.RNetCDF = "--with-netcdf-include=/usr/include/udunits2"
146 )
147 renv::install("RNetCDF")
148 ```
149
150 ### Install flags
151
152 Similarly, additional flags that should be passed to `R CMD INSTALL` can be set via the `install.opts` `R` option:
153
154 ```{r}
155 # installation of R packages using the Windows Subsystem for Linux
156 # may require the `--no-lock` flag to be set during install
157 options(install.opts = "--no-lock")
158 renv::install("xml2")
159
160 # alternatively, you can set such options for specific packages with e.g.
161 options(install.opts = list(xml2 = "--no-lock"))
162 renv::install("xml2")
163 ```
164
165 ### Vignettes
166
167 renv does not build vignettes when installing a package from source.
168 This is because vignettes often require suggested packages, and installing all suggested packages (particularly from source) can be arduous.
169
170 If you want to distribute vignettes for your own packages, we suggest creating your own repository of binaries, either with [R Universe](https://r-universe.dev/) (for publicly hosted packages on GitHub), with [Posit Package Manager](https://posit.co/products/enterprise/package-manager), or with [drat](https://eddelbuettel.github.io/drat/).
171
172 ## Downloads
173
174 By default, renv uses [curl](https://curl.se/) for file downloads when available.
175 This allows renv to support a number of download features across multiple versions of R, including:
176
177 - Custom headers (used especially for authentication),
178 - Connection timeouts,
179 - Download retries on transient errors.
180
181 If `curl` is not available on your machine, it is highly recommended that you install it.
182 Newer versions of Windows 10 come with a bundled version of `curl.exe`; other users on Windows can use `renv::equip()` to download and install a recent copy of `curl`.
183 Newer versions of macOS come with a bundled version of `curl` that is adequate for usage with renv, and most Linux package managers have a modern version of `curl` available in their package repositories.
184 You can also configure which `curl` executable is used by setting the `RENV_CURL_EXECUTABLE` environment variable, if necessary.
185
186 `curl` downloads can be configured through renv's configuration settings -- see `?renv::config` for more details.
187
188 ### Alternative downloaders
189
190 If you've already configured R's downloader and would like to bypass renv's attempts to use `curl`, you can use the R option `renv.download.override`.
191 For example, executing:
192
193 ```{r}
194 options(renv.download.override = utils::download.file)
195 ```
196
197 would instruct renv to use R's own download machinery when attempting to download files from the internet (respecting the R options `download.file.method` and `download.file.extra` as appropriate).
198 Advanced users can also provide their own download function, provided its signature matches that of `utils::download.file()`.
199
200 You can also instruct renv to use a different download method by setting the `RENV_DOWNLOAD_METHOD` environment variable.
201 For example:
202
203 ```{r}
204 # use Windows' internal download machinery
205 Sys.setenv(RENV_DOWNLOAD_METHOD = "wininet")
206
207 # use R's bundled libcurl implementation
208 Sys.setenv(RENV_DOWNLOAD_METHOD = "libcurl")
209 ```
210
211 If you find that downloads work outside of renv projects, but not within renv projects, you may need to tell renv to use the same download file method that R has been configured to use.
212 You can check which download method R is currently configured to use with:
213
214 ```{r}
215 getOption("download.file.method")
216 ```
217
218 And the downloader currently used by renv can be queried with:
219
220 ```{r}
221 renv:::renv_download_method()
222 ```
223
224 You can force renv to use the same download method as R by setting:
225
226 ```{r}
227 Sys.setenv(RENV_DOWNLOAD_METHOD = getOption("download.file.method"))
228 ```
229
230 and, if necessary, you could also set this environment variable within e.g. your `~/.Renviron`, so that it is visible to all R sessions.
231 See `?Startup` for more details.
232
233 Note that other features (e.g. authentication) may not be supported when using an alternative download file method -- you will have to configure the downloader yourself if that is required.
234 See `?download.file` for more details.
235
236 ### Proxies
237
238 If your downloads need to go through a proxy server, then there are a variety of approaches you can take to make this work:
239
240 1. Set the `http_proxy` and / or `https_proxy` environment variables.
241 These environment variables can contain the full URL to your proxy server, including a username + password if necessary.
242
243 2. You can use a `.curlrc` (`_curlrc` on Windows) to provide information about the proxy server to be used.
244 This file should be placed in your home folder (see `Sys.getenv("HOME")`, or `Sys.getenv("R_USER")` on Windows); alternatively, you can set the `CURL_HOME` environment variable to point to a custom 'home' folder to be used by `curl` when resolving the runtime configuration file.
245 On Windows, you can also place your `_curlrc` in the same directory where the `curl.exe` binary is located.
246
247 See the curl documentation on [proxies](https://ec.haxx.se/usingcurl/proxies/) and [config files](https://ec.haxx.se/cmdline/configfile.html) for more details.
248
249 As an [example](https://github.com/rstudio/renv/issues/146), the following `_curlrc` works when using authentication with NTLM and SSPI on Windows:
250
251 ```sh
252 --proxy "your.proxy.dns:port"
253 --proxy-ntlm
254 --proxy-user ":"
255 --insecure
256 ```
257
258 The [curl](https://cran.r-project.org/package=curl) R package also has a helper:
259
260 ```{r}
261 curl::ie_get_proxy_for_url()
262 ```
263
264 which may be useful when attempting to discover this proxy address.
265
266 ### Authentication
267
268 Your project may make use of packages which are available from remote sources requiring some form of authentication to access -- for example, a GitHub enterprise server.
269 Usually, either a personal access token (PAT) or username + password combination is required for authentication.
270 renv is able to authenticate when downloading from such sources, using the same system as the [remotes](https://cran.r-project.org/package=remotes) package.
271 In particular, environment variables are used to record and transfer the required authentication information.
272
273 | **Remote Source** | **Authentication** |
274 |-------------------|-----------------------------------------|
275 | GitHub | `GITHUB_PAT` |
276 | GitLab | `GITLAB_PAT` |
277 | Bitbucket | `BITBUCKET_USER` + `BITBUCKET_PASSWORD` |
278 | Git Remotes | `GIT_PAT` / `GIT_USER` + `GIT_PASSWORD` |
279
280 These credentials can be stored in e.g. `.Renviron`, or can be set in your R session through other means as appropriate.
281
282 If you require custom authentication for different packages (for example, your project makes use of packages available on different GitHub enterprise servers), you can use the `renv.auth` R option to provide package-specific authentication settings.
283 `renv.auth` can either be a a named list associating package names with environment variables, or a function accepting a package name + record, and returning a list of environment variables.
284 For example:
285
286 ```{r}
287 # define a function providing authentication
288 options(renv.auth = function(package, record) {
289 if (package == "MyPackage")
290 return(list(GITHUB_PAT = "<pat>"))
291 })
292
293 # use a named list directly
294 options(renv.auth = list(
295 MyPackage = list(GITHUB_PAT = "<pat>")
296 ))
297
298 # alternatively, set package-specific option
299 # as a list
300 options(renv.auth.MyPackage = list(GITHUB_PAT = "<pat>"))
301 # as a function
302 options(renv.auth.MyPackage = function(record) {
303 list(GITHUB_PAT = "<pat>")
304 })
305 ```
306
307 For packages installed from Git remotes, renv will attempt to use `git` from the command line to download and restore the associated package.
308 Hence, it is recommended that authentication is done through SSH keys when possible.
309
310 Authentication may be required when resolving a package from a remote specification.
311 If the package name cannot be inferred directly from the remote, it can be supplied with a prefix of the form `<package>=`.
312 For example, the igraph package on GitHub at <https://github.com/igraph/rigraph> could be installed with:
313
314 ```{r}
315 renv::install("igraph=igraph/rigraph")
316 ```
317
318
319 #### Azure DevOps
320
321 Authentication with Azure DevOps may require credentials to be set in a slightly different way. In particular, you can use:
322
323 ```sh
324 GITHUB_USER = <username>
325 GITHUB_PASS = <password>
326 ```
327
328 replacing `<username>` and `<password>` as appropriate -- for example, your password may just be a PAT. See https://github.com/rstudio/renv/issues/1751 for more details.
329
330
331 #### Git and Personal Access Tokens
332
333 Rather than username + password, some remote Git repositories might require Personal Access Tokens (PATs) for authentication. Typically, such servers expect authentication credentials where:
334
335 - Your PAT is supplied as the username,
336 - Either an empty response, or the string `x-oauth-basic`, is provided as the password.
337
338 To help facilitate this, you can set:
339
340 ```sh
341 GIT_USER = <pat>
342 GIT_PASS = x-oauth-basic
343 ```
344
345 in an appropriate startup R file.
346
347
348 ### Custom headers
349
350 If you want to set arbitrary headers when downloading files using renv, you can do so using the `renv.download.headers` R option.
351 It should be a function that accepts a URL, and returns a named character vector indicating the headers which should be supplied when accessing that URL.
352
353 For example, suppose you have a package repository hosted at `https://my/repository`, and the credentials required to access that repository are stored in the `AUTH_HEADER` environment variable.
354 You could define `renv.download.headers` like so:
355
356 ```{r}
357 options(renv.download.headers = function(url) {
358 if (grepl("^https://my/repository", url))
359 return(c(Authorization = Sys.getenv("AUTH_HEADER")))
360 })
361 ```
362
363 With the above, renv will set the `Authorization` header whenever it attempts to download files from the repository at URL `https://my/repository`.
364
365 ### Debugging
366
367 If having problems with downloads, you can get more debugging information (including raw requests and responses) by setting:
368
369 ```{r}
370 options(renv.download.trace = TRUE)
371 ```
372