Configure RStudio in Azure with Ubuntu

Linux Ubuntu HD Wallpapers

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:

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:

  • Docker
  • 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 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
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 --username yourusername --password yourpassword
sudo docker run -d -p 8787:8787 -e USER=rstudio -e PASSWORD=mypassword

So, you end up to have a screen like the following image.

Nano editor via SSH in Ubuntu - Configure RStudio in Azure with Ubuntu
Nano editor via SSH in Ubuntu

Now, you have to tell Ubuntu that this file is executable. For that, you have to run this command:

chmod +x

Now, you run the script with this command


Another option is as follows to execute shell script with one of the following commands:


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 /etc/rc.local

So, move to the etc folder and with nano editor open the file rc.local (probably using sudo)

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 johndoe~/.ssh/config file:

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.

Example of rc.local

I added the last line in the file because when Docker runs the second line but a container already exists, it raises an error. So, the third line runs Docker with the local image.

How to start the container

A full example of the /etc/rc.local is the following one

sudo docker login --username yourusername --password yourpassword
sudo docker rm rstudio
sudo docker run -d -p 8787:8787 -e USER=rstudio -e PASSWORD=mypassword

So, when the machine restarts, Docker logins to the ACR, remove the rstudio instance, if it exists, and then runs the new instance.

Check the permission of rc.local

Last thing is about /etc/rc.local: be sure it is executable:

sudo chown root /etc/rc.local 
sudo chmod 755 /etc/rc.local
rc.local is in green

If you have to change the rc.local file and you applied the 755 you have to change it:

sudo chmod 777 /etc/rc.local

One thought on “Configure RStudio in Azure with Ubuntu

  1. Ensure /etc/rc.local is executable:

    sudo chown root /etc/rc.local
    sudo chmod 755 /etc/rc.local

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.