Install Python and R packages in user’s home directory (BBC)

This short tutorial will guide you to install and load python and R packages in your home directory without permission from administrator on BBC. We will use alfpyremeta and ggplot2 as our examples for demonstration purpose.

 

Install python package alfpy via pip

Pip is a recommended package management system used to install and 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 install a package via pip, because pip will handle that for you. Since pip has already been installed in BBC, and alfpy can be found in Python Package Index, we may call command below in your home directory (the first directory you meet whenever you login into BBC, you can enter cd or cd ~/ to go to your home directory) to install latest version of alfpy:

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 python version in BBC or some features you need only exist in a certain version. Try to install an older version may help you solve the problems. Again we will use alfpy for demonstration purpose. To install alfpy 1.0.4. we first need to make sure current version of the package is uninstalled by entering command below, note that flag “–user” is not required anymore in uninstalling.

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 may test if the package has been successfully installed in python. First enter python in command line to access python console, note that the default python on server is python 2, and commands below work only in python 2.

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()

 

Install R package rmeta via CRAN

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

mkdir ~/local/R_libs

Then, load latest version of R available on server. To check the latest version on server, use 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 use R/3.3.1. To load  R and run R console on BBC, 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. First is package name, “remeta” in this case. Next parameter is where repository where we obtain the package. Most R packages including “rmeta” are available on “http://cran.r-project.org” which is also known as CRAN (The Comprehensive R Archive Network). Last one is the destination of 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, we use 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, we may 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, command wget is 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

Then run 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 common uses do not have permission to write on.

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

Finally, we may open R console and load the package by using command:

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