Install RStudio Server on Ubuntu 22.04

By Anurag Singh

Updated on Jul 12, 2024

Install RStudio Server on Ubuntu 22.04

RStudio Server is a popular integrated development environment (IDE) for R, which is widely used for data analysis and statistical computing. This tutorial provides step-by-step instructions on how to install and configure RStudio Server on an Ubuntu system.

Prerequisites

  • Ubuntu Server: Ensure you have an Ubuntu dedicated server or KVM VPS up and running. This guide is tested on Ubuntu 22.04 LTS.
  • Root Access: You will need root access to install and configure software on the server.
  • Domain name with DNS A record to the server IP.

Note: Do not use SSH Key to connect to the server because at the end it use server's username and password to login into the panel.

Step 1: Update the System

Before installing any new software, it's a good practice to update the package index and upgrade the installed packages to the latest versions.

sudo apt update
sudo apt upgrade -y

Step 2: Install R

R is a programming language for statistical computing and graphics. To install R, follow these steps:

Add the CRAN Repository:

sudo apt install -y dirmngr gnupg apt-transport-https ca-certificates software-properties-common
wget -qO- https://cloud.r-project.org/bin/linux/ubuntu/marutter_pubkey.asc | sudo tee -a /etc/apt/trusted.gpg.d/cran_ubuntu_key.asc
sudo add-apt-repository "deb https://cloud.r-project.org/bin/linux/ubuntu $(lsb_release -cs)-cran40/"

Install R:

sudo apt install -y r-base

Step 3: Install RStudio Server

First, install gdebi to install RStudio.

sudo apt-get install gdebi-core

Download RStudio Server:

Go to the RStudio Server download page and find the URL for the latest .deb package. Use wget to download it:

wget https://download2.rstudio.org/server/focal/amd64/rstudio-server-2024.04.2-764-amd64.deb

(Note: The version and URL may change, so ensure you get the correct link from the official download page .)

Install the Downloaded Package:

sudo gdebi rstudio-server-2024.04.2-764-amd64.deb

Step 4: Configure Firewall

If you have a firewall running, you'll need to allow traffic on the port RStudio Server uses (8787 by default).

sudo ufw allow 8787/tcp
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw reload

Step 5: Install Nginx

First, install Nginx if it's not already installed:

sudo apt install -y nginx

Step 6: Configure Nginx

Create a new configuration file for your RStudio Server in Nginx's sites-available directory:

sudo nano /etc/nginx/sites-available/rstudio

Add the following content to the file, replacing your_domain with your actual domain name:

server {
    listen 80;
    server_name your_domain;

    location / {
        proxy_pass http://localhost:8787;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

Save and close the file.

Enable the configuration by creating a symbolic link to the sites-enabled directory:

sudo ln -s /etc/nginx/sites-available/rstudio /etc/nginx/sites-enabled/

Test the Nginx configuration for syntax errors:

sudo nginx -t

If the test is successful, reload Nginx to apply the changes:

sudo systemctl reload nginx

Step 7: Install Certbot

Install Certbot and the Nginx plugin:

sudo apt install -y certbot python3-certbot-nginx

Obtain an SSL Certificate

Use Certbot to obtain an SSL certificate for your domain and configure Nginx to use it:

sudo certbot --nginx -d your_domain

Follow the prompts to complete the process. Certbot will automatically configure Nginx to use the SSL certificate and set up automatic certificate renewal.

Step 8: Access RStudio Server

RStudio Server is now installed and running. You can access it by opening a web browser and navigating to https://domain_name. Replace domain_name with the domain name or hostname of your server.

You will be prompted to log in. Use your Ubuntu server username and password to log in.

Optional Step: Install Additional R Packages

You may want to install additional R packages for your data analysis work. This can be done from within RStudio or by using the R console.

To install a package, open the R console and use the install.packages() function. For example:

install.packages("ggplot2")

Troubleshooting

Check Service Status: If RStudio Server is not running, check its status:

sudo systemctl status rstudio-server

View Logs: To troubleshoot issues, view the RStudio Server logs:

sudo journalctl -u rstudio-server

Configuration Files: Configuration files are located in /etc/rstudio. You can modify these files to change settings such as the port RStudio Server runs on.

Conclusion

You have successfully installed and configured RStudio Server on Ubuntu. You can now use it for data analysis and statistical computing. RStudio Server provides a powerful and user-friendly interface for working with R, making it an excellent choice for data scientists and statisticians.