How to Install R in Windows Subsystem for Linux (WSL)

How to Install R in Windows Subsystem for Linux (WSL)
How to Install R in Windows Subsystem for Linux (WSL)

The Windows Subsystem for Linux (WSL) has opened up a plethora of new possibilities for Windows users interested in dabbling with Linux-based software. For data scientists and developers alike, one crucial tool is the programming language R, renowned for its statistical computing and graphics capabilities. While R is certainly installable directly on Windows, some users might find it more beneficial to install and use R within the WSL environment. This article is designed to help you install R on your WSL, so you can utilize its full potential from the comforts of your Windows machine.

Before diving into the installation process, it's important to remember that the article assumes that you already have WSL installed on your Windows machine. If not, you would need to install it first. For the scope of this article, we will be focusing on the Ubuntu distribution on WSL but the process should be similar for other Linux distributions available in WSL.

Step 1: Update Your WSL System

The first step in the installation process is ensuring that your WSL system is up-to-date. To do this, open the WSL terminal (simply search for "WSL" in your start menu), and then run the following commands:

sudo apt update
sudo apt upgrade

The apt update command updates the list of available packages and their versions, but it does not install or upgrade any packages. The apt upgrade command actually installs newer versions of the packages you have.

Step 2: Install R on WSL

Now that your system is up-to-date, you can proceed with installing R. To do this, run the following commands:

sudo apt install r-base

The r-base package is a basic R package that will install R onto your system. The installation may take a few minutes to complete.

After installation, you can verify that R was successfully installed by checking the version. Enter R by just typing R and then:

version

If the installation is successful, it should return information about the R version installed on your machine.

Step 3: Install R Packages

R's functionality can be extended with packages. They are collections of R functions, data, and compiled code that can be used to perform specific tasks. To install R packages, you first need to open R in the WSL terminal by typing R. Once you're in R, you can install a package using the install.packages function.

For example, if you want to install the popular dplyr package, you would do it like so:

install.packages('dplyr')

After the installation process is complete, you can load the dplyr package using the library function:

library(dplyr)

This makes the functionality of the dplyr package available for use in your R session.

Step 4: Install RStudio Server (Optional)

While the terminal provides all the functionality needed to run and manage R scripts, many users prefer a more visual interface. This is where RStudio comes into play. However, WSL doesn't support GUI applications, so we'll have to install RStudio Server, which allows us to access RStudio via a web browser.

First, download the RStudio Server package:

wget https://download2.rstudio.org/server/bionic/amd64/rstudio-server-1.4.1106-amd64.deb

Here, we are using the wget command to download the .deb package for RStudio Server. Replace the URL with the most recent version available on the RStudio download page.

Next, install it using dpkg:

sudo dpkg -i rstudio-server-1.4.1106-amd64.deb

If the dpkg command fails due to missing dependencies, you can install them with:

sudo apt-get install -f

Start RStudio Server by running

sudo rstudio-server start

Finally, access RStudio Server by opening a web browser and going to localhost:8787. You can log in using your WSL username and password.

Using Visual Studio Code, R, and WSL Together

If you're a data scientist or a developer using R within the Windows Subsystem for Linux (WSL), you may be wondering if you can leverage the power of Microsoft's Visual Studio Code (VSCode) as your integrated development environment (IDE). Fortunately, this setup is not only possible but also straightforward. This guide will show you how to set up and use VSCode, R, and WSL together.

Please note that this guide assumes that you have already installed WSL, Ubuntu on WSL, and R within WSL, as discussed in the previous guide.

Step 1: Install VSCode on Windows

If you have not already installed VSCode on your Windows machine, you will need to do this first. Go to the VSCode download page and download the installer for Windows. Once the download is complete, run the installer and follow the prompts to install VSCode.

Step 2: Install the Remote - WSL Extension

Once you have VSCode installed, you will need to install the Remote - WSL extension. This extension allows VSCode to interface directly with WSL, allowing you to write, run, and debug code stored within your WSL environment.

To install the Remote - WSL extension:

  1. Open VSCode.
  2. Click on the Extensions view icon on the Sidebar (or use the shortcut Ctrl+Shift+X).
  3. Search for 'Remote - WSL'.
  4. Click on the Install button.

After installation, you'll see a green button on the bottom left corner indicating that the Remote - WSL extension is installed and active.

Step 3: Connect to WSL and Install the R Extension

Now that you have the Remote - WSL extension installed, you can connect VSCode to your WSL environment:

  1. Open the Command Palette using the shortcut Ctrl+Shift+P.
  2. Type 'WSL' and select 'Remote-WSL: New Window'.
  3. This will open a new VSCode window connected to your WSL environment.

Once you're connected to WSL, you should install the R extension in this VSCode window. This extension provides support for the R language, including features like syntax highlighting, code navigation, and even debugging.

To install the R extension:

  1. Click on the Extensions view icon on the Sidebar (or use the shortcut Ctrl+Shift+X).
  2. Search for 'R'.
  3. Click on the Install button.

Remember, you must install the R extension while you are connected to your WSL environment, not on your local Windows VSCode.

Step 4: Configure the R Extension (Optional)

The R extension works out of the box, but you may want to configure it according to your preferences. For example, you can set VSCode to use RStudio's Rterm console, which offers syntax highlighting and multi-line editing. You can access the extension's settings through the gear icon in the Extensions view or by searching for 'R' in the main settings.

Step 5: Start Using R in VSCode

Now that you have everything set up, you can start writing and running R code using VSCode in your WSL environment. You can create a new R script by creating a new file with the .R extension.

To run the code, you can use the Ctrl+Enter shortcut to run the current line or selected lines in the R terminal.

Remember, since this setup is running within WSL, it can interact with any other software or files that are also within your WSL environment. This means you can easily incorporate other Linux-based tools into your R projects.

Conclusion

This step-by-step guide shows you how to install and run R on your WSL system. Whether you're a developer, a data scientist, or someone who's just curious about R, this setup can offer you a powerful, Linux-like environment without ever having to leave your Windows system.

Remember that while R itself is incredibly powerful, a significant portion of its power comes from the vast array of available packages. So don't hesitate to explore and experiment with them. Finally, if you prefer a more visual interface or need to develop complex R projects, the optional RStudio Server installation provides a feature-packed IDE accessible right from your web browser.

VSCode, when combined with WSL and R, provides a robust, feature-rich environment for data science and development tasks. VSCode's intuitive interface, coupled with the power of R and the versatility of WSL, offers an unparalleled experience for R developers on Windows.

With this setup, you're now fully equipped to start your R programming journey on WSL!

Data Analytics in R
Dive into our R programming resource center. Discover tutorials, insights, and techniques for robust data analysis and visualization with R!