Custom R Images On Google Compute Engine

This article will focus on creating an R image with specific module versions on Google Compute Engine. The process is as follows,
  • Create a compute engine VM.
  • SSH into the compute engine VM.
  • Install conda.
  • Install R and R modules.
  • Create an R image using this VM.

Creating a Compute Engine VM

A compute engine VM can be created using the following command,
  1. gcloud compute instances create VM_NAME \  
  2.     [--image IMAGE | --image-family IMAGE_FAMILY] \  
  3.     --image-project IMAGE_PROJECT  
 You can read more about creating Compute Engine VMs here. I'm using debian-10 as the base image.

SSH into the compute engine VM

Once the VM is created it'll be listed in Compute Engine VM instances on Google Cloud Console. Click on the VM instance name to open the VM details page. SSH into the VM using the SSH drop-down and selecting your preferred method.
Switch the user to root using the command sudo su -

Install conda

If you're using debian-10 base image as well, install conda using this tutorial.
Confirm conda installation using conda --version

Install R and R modules

R can be installed using conda through the following command,
  1. conda install 'r-base>3.6.1'  
Here you can specify the version of R you want to install. Once R is installed, there are a couple of ways to install R modules as described below,
Using conda: conda install -c r <r-module>
Using conda-forge: conda install -c conda-forge <r-module>
Using CRAN: R -e "install.packages('<r-module>', repos='')"

Create an R image using this VM

Once the R modules are installed, stop the VM and create an R image using the following command,
  1. gcloud compute images create IMAGE_NAME (--source-disk=SOURCE_DISK     | --source-image=SOURCE_IMAGE     | --source-image-family=SOURCE_IMAGE_FAMILY     | --source-snapshot=SOURCE_SNAPSHOT     | --source-uri=SOURCE_URI) [--csek-key-file=FILE] [--description=DESCRIPTION] [--family=FAMILY] [--forbidden-database-file=[DBX_VALUE,…]] [--force] [--guest-os-features=[GUEST_OS_FEATURE,…]] [--key-exchange-key-file=[KEK_VALUE,…]] [--labels=[KEY=VALUE,…]] [--licenses=[LICENSES,…]] [--platform-key-file=PLATFORM_KEY_FILE] [--no-require-csek-key-create] [--signature-database-file=[DB_VALUE,…]] [--source-disk-zone=SOURCE_DISK_ZONE] [--source-image-project=SOURCE_IMAGE_PROJECT] [--storage-location=LOCATION] [--kms-key=KMS_KEY : --kms-keyring=KMS_KEYRING --kms-location=KMS_LOCATION --kms-project=KMS_PROJECT] [GCLOUD_WIDE_FLAG …]     
You can read more about creating Compute Engine VM Images here.
Once the image is created it'll appear in Compute Engine Images section. This image can be used to further instantiate Compute Engine VMs.