Setting Up Rstudio and Cmdstanr on AWS EC2

I recently had the joy of setting up my environment on AWS EC2 and I wanted to put this out there for anyone who just wants to get started as fast as possible.

I’ll assume that you set your EC2 instance up already. Let me note that you should add some type of persistent storage of your data. EC2 instances can be deleted at will by Amazon 😱 (author note: it happened to me). Back up your stuff, this ain’t your grandma’s cloud.

You can add an S3 volume, push to Git, or make images of your instance. You can follow adding S3 through AWS support. Furthermore, I recommend that you UNCHECK delete on termination. As of January 28, 2021 the default is to delete on termination. I am talking to Amazon about this issue and it may get changed.

I’m running Ubuntu on EC2 and these are the steps I take.

Installing RStudio Server

Update Ubuntu

sudo apt update && sudo apt upgrade -y

Download Rstudio by (from Rstudio help)

sudo apt-get install gdebi-core
wget https://download2.rstudio.org/server/bionic/amd64/rstudio-server-1.4.1103-amd64.deb
sudo gdebi rstudio-server-1.4.1103-amd64.deb

Then adduser

sudo adduser YOURNAME

Set up ports
Need to make sure can access port 8787 for web access

port 22 for ssh
go to the security group for the instance “edit inbound rules” option

Configure Security Group — Create group to open the following ports

1. SSH — Port Range: Port 22 — Source:
2. Custom TCP Rule — Port Range: Port 8787 — Source: 0.0.0.0/0

Installing stuff on AWS linux

You need rstan for Rstudio to open .stan files. Before installing rstan do these two things. Doesn’t matter which one is first.

In Rstudio

Sys.setenv(DOWNLOAD_STATIC_LIBV8 = 1)
install.packages("rstan")

Now, you really do need to open the terminal and add

sudo apt-get install libnode-dev
sudo apt-get install  libcurl4-openssl-dev
sudo apt-get install libxml2-dev

Install cmdstanr

install.packages("cmdstanr", repos = c("https://mc-stan.org/r-packages/", getOption("repos")))
# if you want dev of cmdstanr
# remotes::install_github("stan-dev/cmdstanr")

cmdstanr::install_cmdstan(version = "2.26.0", cores = 4)

Optional: set unlimited memory

You may not be able to use the full amount of memory on EC2. I followed the steps of increasing the ulimit: How to increase ulimit on Amazon EC2 instance? – Stack Overflow.

Recover files from Rstudio crash

Sometimes Rstudio will crash and you’ll have to SSH into your instance to recover it. The best way I found is by going in to the share folder and copying the rstudio folder to a new folder aptly named rstudio-old. Then you can go into rstudio-old under sources and find the files with s in front. These are all the files you had opened in Rstudio! So you can recover any unsaved work.

cd  /.local/share 
# next line is only needed if rstudio-old already exists
# sudo rm -rf rstudio-old 
sudo mv rstudio rstudio-old 

# Recover files from

Home/.local/share/rstudio-old/sources/s-****

Appendix

If you are not running Ubuntu the way to install V8 above may not work. You can try installing via the terminal:

sudo apt-get install libv8-dev

2 thoughts on “Setting Up Rstudio and Cmdstanr on AWS EC2

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s