How To Install a R Package Locally and Load it Easily?

R LogoR has been one of the popular languages for anything to do with large amount of data. There are loads of useful packages that let you get started immediately in using a wide realm of statistical/computational tools. Installing an R package is easy. Typically, R Packages can be downloaded as “tar.gz” file for Mac OS X from CRAN sources. The command to install a R package to all users in your machine is

R CMD INSTALL -l  myRPackage.tar.gz 

Local Installation of R Packages

Sometimes you might want to install a R package locally, either because you may not have root access to install a package globally or you may just want to try out a new package before installing globally. Here is how to install a R package locally from the terminal.

To install a R package locally, specify the local directory where you want to install by using the “-l” option in the “R CMD INSTALL” command. For example, to install the R package in the local directory “/usr/me/localR/library”, use the “R CMD INSTALL” as follows.

R CMD INSTALL -l /usr/me/localR/library myRPackage.tar.gz 

How to Load a Locally Installed R Package and Use it?

Installing R Packages at a local directory is only a first step. There are a few ways to load the locally installed R packages and use them. One option is to specify the local path to the R package while loading the library. The R command to load a general package is


To load a locally installed R package, use the library command with parameter lib.loc as

library("myRPackage", lib.loc="/usr/me/local/R/library")

Another option is to instruct your .bashrc file to add the the path to local R library. This option lets you load the package without specifying the local every time. To enable this add the following to your .bashrc

if [ -n $R_LIBS ]; then
   export R_LIBS=/usr/me/local/R/library:$R_LIBS
   export R_LIBS=/usr/me/local/R/library

After adding you can check if the local R package directory in the R library path using the R command “.libPaths()“. Typing “.libPaths()” in R, will show all the R library paths. If your local R library path is not added properly, you will only see one general path to R library, like “/opt/R/2.11.1/lib64/R/library”.