List

Here is a super-easy visual guide to setting up and running RStudio Server for Ubuntu 20 on Windows 10. This is useful if you want to run R for Linux (e.g., due to its faster computation and better parallelization or simply to do cross-platform development) while on a Windows machine. Although there are several such guides already (linked below), this one combines the content of several and, through the use of screenshots and examples, is meant to be especially accessible to those without prior Linux experience.


1. Update Windows 10

We want to use the Windows Subsystem for Linux version 2 (WSL2), but to do so we need to update a recent build of Windows 10.

In the Windows search bar at the bottom-left of the screen, type in Check for updates and left-click the matching result (or click here). Install all updates (this might take a while), restarting when required.

When done, you should have Windows 10 version 1903, 1909, 2004, or 20H2 (or higher). Do not proceed to further steps until this one is complete. To check this, press the Windows logo key and the R key at the same time (⊞+R), type in winver, and hit Enter.


2. Open PowerShell

To quickly and easily change some Windows settings (i.e., enable some features that are disabled by default), we can use the Windows PowerShell.

In the Windows search bar at the bottom-left of the screen, type in Windows PowerShell, and left-click the matching result. This will pull up a big blue console window.


3. Enable WSL1 and the Virtual Machine Platform

Now we need to enable those disabled features: the Windows Subsystem for Linux version 1 (WSL1) and the Virtual Machine Platform.

Enter the following commands into the PowerShell console, hitting Enter after each:

dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all

Powershell

Once complete, save and close down all your documents and restart your machine. It's not necessary to do it this way, but you can restart your computer from the PowerShell console by typing Restart-Computer and hitting Enter.


4. Download and install WSL version 2

Now that we have WSL1 enabled and installed, we can upgrade it to version 2 (WSL2).

Go to this page and follow the instructions to download and install the latest WSL2 Linux kernel. (Note that different installers are needed for x64 and ARM64 machines.)

https://aka.ms/wsl2kernel

WSL2 Download


5. Open PowerShell (again)

Since we restarted the computer during Step 3, the PowerShell console is gone and we have to re-open it using the instructions from Step 2.

In the Windows search bar at the bottom-left of the screen, type in Windows PowerShell, and left-click the matching result.


6. Change the default WSL version to 2

Now we need to tell Windows to use WSL2 by default instead of WSL1.

Enter the following command into the PowerShell console and hit Enter:

wsl --set-default-version 2


7. Install and Launch Ubuntu from the Microsoft Store

We are finally ready to install Ubuntu on Windows using WSL2.

Go the this page in your web browser, which will open the Microsoft Store. Left-click the “Ubuntu” button to open the Ubuntu download page and left-click the “Install” button to download and install it. When it is finished, left-click the “Launch” button to open it. Alternatively, and in the future, you can open Ubuntu by finding it in your start menu as you would any other Windows program/app. Either way, this will open a black terminal window.

https://aka.ms/wslstore


8. Configure your Ubuntu account

When launching for the first time, the Ubuntu terminal will ask you to create a username and password for your Ubuntu account. This can be whatever you'd like it to be. Don't forget these as you will need them later.

Type in your desired Ubuntu username into the Ubuntu terminal and hit Enter, then type your desired Ubuntu password and hit Enter, then type in your desired Ubuntu password again and hit Enter.

9. Check that Ubuntu is using WSL2

Before continuing, let's just double-check that Ubuntu is using WSL version 2.

In Powershell, use the following command to check that Ubuntu appears, has the “Running” state and the “2” version. If this does not appear, you may need to run the previous Powershell steps as Administrator (or not as Administrator).

wsl -l -v


10. Enable copy-pasting into your Ubuntu terminal

Some of the next steps involve entering rather long and complicated commands into the Ubuntu terminal. To make this easier, we can enable copy-paste functionality.

With your Ubuntu terminal window open, left-click the center of the orange Ubuntu icon on the top-left of the window; this will open a contextual menu (see the image below). Select the “Properties” option from the menu. From here, left-click the checkbox next to “Use Ctrl+Shift+C/V as Copy/Paste” to add a check to it. Then click the “OK” button.

Now you can press the Ctrl, Shift, and C keys (Ctrl+Shift+C) at the same time to copy text from Windows (e.g., from this webpage) and then, with the Ubuntu terminal open, press the Ctrl, Shift, and V keys (Ctrl+Shift+V) at the same time to paste that text into the terminal.


11. Update Ubuntu

When you first install Ubuntu, several packages that come with it may be slightly outdated. Quickly update them to the newest version before continuing.

Enter the following commands into the Ubuntu console and hit Enter:

sudo apt update
sudo apt upgrade -y

You will need to enter your newly created password and hit Enter to confirm some of these commands.


12. Install the newest version of R and dependencies for RStudio

To install the newest version of R on your virtual Ubuntu machine, you will first need to add a long key phrase and the appropriate repository to download R from.

Copy and paste the following commands into the Ubuntu terminal and hit Enter:

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9
sudo add-apt-repository 'deb https://cloud.r-project.org/bin/linux/ubuntu focal-cran40/'
sudo apt update
sudo apt install -y r-base r-base-core r-recommended r-base-dev gdebi-core build-essential libcurl4-gnutls-dev libxml2-dev libssl-dev

Again, you may need to enter your newly created password and/or type Y and hit Enter to confirm some of these commands.


13. Check the version of R that was installed

If all went well, you should now have R version 4.0.5 (or higher) installed.

Enter the following commands into the Ubuntu console and hit Enter:

R --version

R Version


14. Install the newest version of RStudio Server on Ubuntu

Although we could now run R from the Ubuntu terminal by typing R and hitting enter, it will be much more convenient to install RStudio Server (free version) and use that as an interface to R on our virtual Ubuntu machine.

Copy and paste the following commands into the Ubuntu terminal to download the newest version of RStudio Server for Ubuntu.

wget https://rstudio.org/download/latest/stable/server/bionic/rstudio-server-latest-amd64.deb
sudo gdebi rstudio-server-latest-amd64.deb


15. Start your RStudio Server

Now that we have RStudio Server installed, we can start it up.

Type the following command into the Ubuntu terminal and hit Enter.

sudo rstudio-server start


16. Access your RStudio Server

Now we can access our server via our favorite web browser (on Windows).

Go to the following link in your Windows web browser: http://localhost:8787
You will need to log in using the Ubuntu username and password you set in Step 8.


17. Install R packages in RStudio Server

We will need to re-install all our packages on the virtual Ubuntu machine.

Type in the RStudio Server console like you would in R. For example, use the install.packages() function to install whatever packages you'd like. The only difference I've found is that occasionally a package with compiled code will need something to also be installed on Ubuntu in order for it to work.

For example, in order to get rstan to work, I needed to install the dependency V8, but this would not install in R until I installed libnode-dev on Ubuntu:

sudo apt-get install -y libv8-dev

This was pretty clear from the V8 website, so just do a bit of searching around if you run into problems.


18. Create (or open) RStudio Projects on the Windows file system

I find it most convenient to save the files created on my virtual Ubuntu machine to my Windows file system. This allows me to manage my input and output files on Windows.

In our RStudio Server window, left-click the “File” menu and select “New Project…” (or “Open Project…”). Navigate the project creation (or opening) wizard as you normally would on Windows, but when it comes time to select the file location of your project, click the ... button (see the image below), type in /mnt/, and click “OK”. It will show you the drive letters on your Windows file system (e.g., a folder called c will contain everything on the C: drive). You can then choose whichever folder on your Windows file system you want the project to live in.


19. Stopping your RStudio Server

When you are done with your session, in addition to closing your browser window, you should stop your RStudio Server.

Type in the Ubuntu console the following command and hit Enter:

sudo rstudio-server stop


References

22 Responses to “Running RStudio Server via Ubuntu 20 on Windows 10”

  1. Jeffrey Girard

    Exciting news that Microsoft has added support for WSL2 on older Windows 10 versions (1903 and 1909) so if you are having trouble upgrading to version 2004 in Step 1, you can still make this work: https://devblogs.microsoft.com/commandline/wsl-2-support-is-coming-to-windows-10-versions-1903-and-1909/

  2. Matt

    Hi Jeffrey,

    I’m not understanding how the file system works on rstudio server.
    I am trying to set a variable to be a folder and R keeps listing it as a single character vector.

    >path path
    [1] “/mnt/c/Users/heron/OneDrive/Desktop/Paprica/Paprica Tutorial Server”

    list.files however shows the content of the files

    • Jeffrey Girard

      Hi Matt,
      I’m not sure what you are trying to do/what you are asking. Isn’t this the same behavior as you would get in RStudio on Windows?

  3. Jeffrey Girard

    Installing V8 on Linux is now even easier: https://ropensci.org/technotes/2020/11/12/installing-v8/

  4. Ariel

    Thank you for this article! Why is it important to stop the session? What happens if you leave it running?

    • Jeffrey Girard

      If you restart your computer without stopping the server first, you may get an error on trying to access the server such as “RStudio Initialization Error: Error occurred during transmission.” When this happens, you can fix it by typing the following command into the Ubuntu console: rm ~/.rstudio -R

      • Tales

        Hi, thanks for this very helpful article.

        I was wondering if this ‘exiting without first stopping’ issue is related to wsl rstudio needing manual start/stop instead of being installed as service.

        regards

  5. Jordan T

    Hi Jeffery, helpful post! Do you know how I could access my WSL2 RStudio Server remotely? I haven’t been able to connect from anything other than my local machine.

  6. Prince

    I found this very helpful, thanks. However, I had an error in point 13.

    For point 13 you need to include the command below to be able to use the gdebi command in the last line, else you get an error

    sudo apt-get install gdebi-core

    see https://rstudio.com/products/rstudio/download-server/debian-ubuntu/

    regards,

    • Jeffrey Girard

      Hi Prince, gdebi should have been installed earlier in point 12.

      • Prince

        Thanks, Jeffrey. I may have missed that in following the steps. Again, thank you for this helpful guide.

  7. Luk

    Thanks Jeffrey, it was extremely useful! 🙂 Greetings from Poland

  8. Steve

    Thanks for the really helpful guide to setting this up. Have you found a way to enable remote login as well as using localhost:8787

  9. Rick

    I encountered the following error while trying to access RStudio from Chrome: ERR_EMPTY_RESPONSE

    `sudo rstudio-server start` seems to work as it doesn’t produce any error, only I can’t access it. Would you or anyone happen to know why

  10. David

    Can anyone access the rstudio server or only someone on the local machine?

    • Jeffrey Girard

      The approach I described will only let someone on the local machine access it.

  11. David McAllister

    Does this allow people onto your rstudio server via the internet (if they know the password?)

  12. Yas

    When we open rstudio from localhost, where is its directory? mine is ho,e/, but I cant tell where is home in my computer.

  13. Jannatul Ferdush

    Hi Dr. Girard,

    Thank you for sharing the instructions on installing R in Ubuntu at WSL. I have been trying to add the Repository: ‘deb https://cloud.r-project.org/bin/linux/ubuntu focal-cran40/’, but I am getting the error given below. It is saying the public key is not available. Can you please give me solution for this. Thank you.

    Sincerely,
    Jannatul Ferdush

    $ sudo add-apt-repository ‘deb https://cloud.r-project.org/bin/linux/ubuntu focal-cran40/’
    Repository: ‘deb https://cloud.r-project.org/bin/linux/ubuntu focal-cran40/’
    Description:
    Archive for codename: focal-cran40/ components:
    More info: https://cloud.r-project.org/bin/linux/ubuntu
    Adding repository.
    Press [ENTER] to continue or Ctrl-c to cancel.
    Adding deb entry to /etc/apt/sources.list.d/archive_uri-https_cloud_r-project_org_bin_linux_ubuntu-jammy.list
    Adding disabled deb-src entry to /etc/apt/sources.list.d/archive_uri-https_cloud_r-project_org_bin_linux_ubuntu-jammy.list
    Hit:1 http://security.ubuntu.com/ubuntu jammy-security InRelease
    Get:2 https://cloud.r-project.org/bin/linux/ubuntu focal-cran40/ InRelease [3622 B]
    Get:3 https://cloud.r-project.org/bin/linux/ubuntu jammy-cran40/ InRelease [3626 B]
    Hit:4 http://archive.ubuntu.com/ubuntu jammy InRelease
    Err:3 https://cloud.r-project.org/bin/linux/ubuntu jammy-cran40/ InRelease
    The following signatures couldn’t be verified because the public key is not available: NO_PUBKEY 51716619E084DAB9
    Get:5 https://cloud.r-project.org/bin/linux/ubuntu focal-cran40/ Packages [61.0 kB]
    Hit:6 http://archive.ubuntu.com/ubuntu jammy-updates InRelease
    Hit:7 http://archive.ubuntu.com/ubuntu jammy-backports InRelease
    Reading package lists… Done
    W: https://cloud.r-project.org/bin/linux/ubuntu/focal-cran40/InRelease: Key is stored in legacy trusted.gpg keyring (/etc/apt/trusted.gpg), see the DEPRECATION section in apt-key(8) for details.
    W: GPG error: https://cloud.r-project.org/bin/linux/ubuntu jammy-cran40/ InRelease: The following signatures couldn’t be verified because the public key is not available: NO_PUBKEY 51716619E084DAB9
    E: The repository ‘https://cloud.r-project.org/bin/linux/ubuntu jammy-cran40/ InRelease’ is not signed.
    N: Updating from such a repository can’t be done securely, and is therefore disabled by default.
    N: See apt-secure(8) manpage for repository creation and user configuration details.

    • Jeffrey Girard

      “`
      # update indices
      sudo apt update -qq
      # install two helper packages we need
      sudo apt install –no-install-recommends software-properties-common dirmngr
      # add the signing key (by Michael Rutter) for these repos
      # To verify key, run gpg –show-keys /etc/apt/trusted.gpg.d/cran_ubuntu_key.asc
      # Fingerprint: E298A3A825C0D65DFD57CBB651716619E084DAB9
      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
      # add the R 4.0 repo from CRAN — adjust ‘focal’ to ‘groovy’ or ‘bionic’ as needed
      sudo add-apt-repository “deb https://cloud.r-project.org/bin/linux/ubuntu $(lsb_release -cs)-cran40/”
      “`

Leave a Reply

Your email address will not be published. Required fields are marked *

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