Installing R Packages From CRAN to Your Home Directory (Linux - RHEL7)

If extra functionalities are needed when using the R programming language, you can install additional packages provided by the Comprehensive R Archive Network—or CRAN—through commands in the language itself. It is also possible to install packages from a compressed file of binaries.

To install additional R packages into your home directory, follow these steps.

Creating the install directory inside your home directory

  1. It is advised to make a place to store these R packages that is easily accessible in your home directory, such as '~/R/library' or '~/Programming/R/libs'. The structure of the directory can be anything but will be referred to as '~/your_R_dir' for the remainder of this tutorial.

    1. You can create this directory by using the command 'mkdir -p ~/your_R_dir' in a terminal:

    2. If a directory is not specified during installation, R will create a default directory named  '~/R/computer_architecture-linux_distribution-library/R_version'
      personal library

Installing R Packages

Method 1 - Installing from CRAN inside R

  1. From inside the R language interpreter, you can call the install.packages function. This has many options but the default syntax is as follows: 'install.packages("package_name", repos="", lib="~/your_R_dir")’
    install packages
    1. For a list of repos, or servers containing the packages, see the CRAN website. Michigan Tech hosts a mirror for packages that can be used in the ‘repo’ option as seen above, at
    2. If a needed option is missing, a new window will pop up and prompt you to choose, i.e., if a mirror is not specified from which to download packages. For a full list of the options of the function, refer to this R documentation link.
      install packages
      NOTE: Without the ‘lib’ option, it is not possible to tell R the directory to which you would like packages installed, only to the default directory. Choice is only possible from the initial function call.

Method 2 - Installing from compressed binaries

  1. This method assumes that you have a compressed tarball for the package you wish to install. To do so, use the command 'R CMD INSTALL -l ~/your_R_dir ~/path/to/package_name.tar.gz' in a terminal.
    cmd install
    NOTE: To install a package that requires dependencies all dependencies will either need to be preinstalled or to be specified to be installed alongside the highest level package. Otherwise, dependency resolution will fail and you will see an error like this:
    • Error: dependencies

Using the installed packages

  1. Once the installation process is complete, you will need to tell R where the package was installed. You can do this in a multitude of ways. Note that this does not allow the use of the packages immediately. It only tells R where the packages are located.
    1. In a terminal window: 
      echo 'R_LIBS_USER="~/your_R_dir"' >>  $HOME/.Renviron
      1. This will add ’~/your_R_dir’ to your R environment variables, which will tell R where your packages are installed every time R is started.
    2. Calling the R function '.libPaths("~/your_R_dir")'
      1. This only works for the current session of R and will need to be run again for every new session.
  2. To begin using the packages you installed, you’ll need to tell R to load their files into memory.
    1. To load them in for all future R sessions use the terminal command: 
      echo 'library("package_name")' >> $HOME/.Rprofile
    2. To load an R package for only the current session, use the R function:

Removing packages from R

NOTE: It is not possible to uninstall a package that has been loaded into R. To unload a package which has been loaded, use the ‘detach’ R function as such:
detach("package:package_name", unload=TRUE)

Once packages are no longer needed or causing conflicts for some reason, packages can be removed in the same fashion as installing them but with ‘install’ replaced with ‘remove’, as such:
remove packages

Was this helpful?
0 reviews


Article ID: 98050
Tue 2/11/20 11:02 AM
Tue 3/15/22 9:46 AM