In this new post, I like to explain how to configure RStudio in Azure with an Ubuntu virtual machine. In the last few months, I worked a lot on R, RStudio and the integration with Azure. I have created those posts:
- Getting started with R
- Deploying dockerized R/Shiny Apps on Microsoft Azure
- Deploy ShinyApps with Azure and Docker
Also, I worked on the integration between R/Plumber, the library for creating API with R, and Azure using the API Management Service
In Azure I have a very expensive virtual machine with Ubuntu 20.04 because I need a power machine for long and complex calculation. I shutdown the machine when I finish my stuff. When I restart the machine, I have to open again the connection with the Azure Container Repository (ACR). Then, restart the Docker container with RStudio.
So, I don’t want to do everything manually and I started to try to run automatically all the command at the startup. I tried and I found difficult to configure services in Ubuntu, mostly because Ubuntu removed some commands.
After few days, I found the way and I want to share it with you. From the beginning…
Configure the Virtual Machine (VM)
As I said, I created a virtual machine in Azure with Ubuntu 20.04. I don’t explain how to create a virtual machine in this post. If you need help, see this post. So, the machine is ready and I have access to it via SSH.
So, the first thing I want to set up is:
- RDP (Remote Desktop Protocol) to connect to the virtual machine
- Connection with Azure Container Registry
- Start RStudio
For that, I prepare a Bash script to run. To create a script to execute, you can follow these steps. There is a simple editor and its name is Nano. It is easy to use and it is already installed. First, open a SSH connection with the virtual machine. Then, I’m going to create the file
first.sh typing this command
In this file I added all the command I need to set up all the above applications. This is the script:
sudo apt-get update sudo apt install docker.io sudo systemctl start docker sudo systemctl enable docker sudo apt-get -y install xfce4 sudo apt-get -y install xrdp sudo systemctl enable xrdp echo xfce4-session >~/.xsession sudo service xrdp restart sudo docker login youracr.azurecr.io --username yourusername --password yourpassword sudo docker run -d -p 8787:8787 -e USER=rstudio -e PASSWORD=mypassword youracr.azurecr.io/rstudio
So, you end up to have a screen like the following image.
Now, you have to tell Ubuntu that this file is executable. For that, you have to run this command:
chmod +x first.sh
Now, you run the script with this command
Another option is as follows to execute shell script with one of the following commands:
sh script-name-here.sh bash script-name-here.sh
Now, the virtual machine is ready. If I restart the machine, Docker doesn’t start. So, next step is to configure a service that start at the startup to execute Docker.
Configure a service at the startup with Ubuntu 20.04
So, when I started, I didn’t know that Ubuntu removed
chkconfig to configure a new service or that it requires file with LBS information. I won’t tell you all the story but basically, for every Ubuntu there is some different configuration to apply. The solution with Ubuntu 20.04 is pretty straightforward: I can add the command in
So, move to the
etc folder and with
nano editor open the file
rc.local (probably using
sudo nano /etc/rc.local
This executes the commands as root. To execute commands as a specific user, use
sudo -i -u (
-i to also run the login shell). For example, to establish a persistent SSH tunnel, where
myhost is definde in
sudo -i -u johndoe autossh -nNT -L 1234:localhost:1234 myhost
Sometimes, the file
rc.local didn’t exists. So, you have a blank file in nano. For that, you have to add at the beginning of the file the following code called Shebang line.