Install Python and R packages in local (home) directories

This short tutorial will provide instruction for installing and loading Python and R packages in your home directory without administrative access. We will use alfpyremeta and ggplot2 in this tutorial.

Install python package alfpy via pip

Pip is a package management system used to install/manage software packages from Python Package Index. Pip is simple and user friendly. Usually, as users, we don’t need to worry about prerequisites when we install a package via pip.  Since pip is included in python packages and once you load it in Xanadu, and alfpy can be found in Python Package Index, we can use the following command from the home directory (the first location you arrive at after login to BBC or Xanadu).

pip install --user alfpy
note that flag --user is required because pip by default will install packages in root packages directory where most of users do not have permission to make changes.

Sometimes the latest version of a package is not compatible with the latest Python version in BBC/Xanadu or some features you need only exist in a certain version. Try to install an older version if you encounter this issue.  To install alfpy 1.0.4. we first need to make sure current version of the package is uninstalled (note that flag “–user” is not required to uninstall).

pip uninstall alfpy

Then begin installation by entering:

pip install --user alfpy==1.0.4

where the number after == is version number of the package you wish to install.

We can test if the package has been successfully installed in Python. First, enter python in command line to access python console (all commands are for Python2):

import alfpy

This command will load package alfpy we just installed. To check version number type:

print alfpy.__version__

The command should return 1.0.4 as shown below:

To exit python console, call command exit()

 

Python packages can also be installed using conda command which comes with Anaconda or miniconda packages detailed instructions are given here

 

Install R package rmeta via CRAN

It is helpful to create a new directory for R package installation.

mkdir ~/local/R_libs

Load latest version of R available on server. To check the latest version on server, use the command module avail. A list of available module will pop up as shown below. You are able to see all versions of R available R on server.

For this tutorial, we will use R/3.3.1. To load  R and run R console on BBC/Xanadu, enter:

module load R/3.3.1
R

Apply function “install.packages” in the R console to download and install desired package. There are three required parameters for “install.packages” function.  (1) Package name, “remeta” in this case.  (2) URL for the repository where we can obtain the package. Most R packages including “rmeta” are available on “http://cran.r-project.org” which is also known as CRAN. (3)  Destination for the installed package, which, in this case, is the directory we just created “~/local/R_libs”.

install.packages("remeta", repos="http://cran.r-project.org", lib="~/local/R_libs/")

To use the package from R, we call the function library. It is important to note that library by default only loads packages from root directory, so we have to specify location of the package whenever we try to include a package installed in our home directory as shown below.

library("remeta", lib="~/local/R_libs/")

To quit R console, enter command “q()”.

Install R package from Release Binaries (without CRAN)

If the R package is not available on CRAN or you want to install an old version of packages, you can download the compressed file to your home directory and install it. In this section, we use ggplot2 2.0.0 as example.

It is helpful to create a new directory and move to this directory for R package installation.

mkdir ~/local/R_libs

cd ~/local/R_libs/

Since we know the URL, the command wget can be used to download the .tar.gz file. You can also copy the compressed file to the directory, if you have the file on your local machine or somewhere else in server.

wget http://cran.r-project.org/src/contrib/Archive/ggplot2/ggplot2_2.0.0.tar.gz

Next, we need to load R. For this tutorial, we choose R/3.3.1.

module load R/3.3.1

Run the command below to install the package to a specific directory. If we do not specify the path, installation will fail since by default the the package will be installed in root directory which you do not have access to.

R CMD INSTALL --library=~/local/R_libs/ ggplot2_2.0.0.tar.gz

Open the R console and load the package with the following command:

library("ggplot2", lib="~/local/R_libs/")