Brief: This tutorial teaches you to install R on Ubuntu. You’ll also learn how to run your first R program in Ubuntu using various methods.
R, together with Python, is the most commonly used programming language for statistical computing and graphics, making it easy to work with data. With the growing interest in data analysis, data visualization, data science (the machine learning craze), it is now more popular than ever and is a great tool for anyone looking to dive into this fields.
The good thing about R is that its syntax is pretty straight-forward and you can find many tutorials/guides on how R is used in the real world.
In this article, I’ll cover how to install R on Ubuntu Linux. I’ll also show you how to run your first R program in Linux.
Installing R on Ubuntu
R is included in the Ubuntu repositories. It can be easily installed using:
sudo apt install r-base
Do note that this may install a slightly older version. At the time of writing this article, Ubuntu offers version 3.4 whereas the latest is version 3.6.
I advise sticking with whichever version Ubuntu provides unless you must use the newer version.
In order to get the latest version (or any specific version for that matter), you must use CRAN (The Comprehensive R Archive Network). This is a list of mirrors for downloading the latest version of R. Click on the next section to learn how to install the latest version of R on Ubuntu.
How to install latest R version 3.6 on Ubuntu (click to expand)
To get the R version 3.6, you need to add the mirror to your sources list. I have simplified it for you in this command:
sudo add-apt-repository "deb https://cloud.r-project.org/bin/linux/ubuntu $(lsb_release -cs)-cran35/"
Now you should add the key for the repository:
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9
And then update the repository information and install R:
sudo apt update sudo apt install r-base
Using R programming on Ubuntu
R has more than one use. I’ll go over several methods you can use to run R programs.
Interactive Mode in R
After having installed R, you can run the console using:
This should open up the interactive mode:
This R console is very similar to the Python and Haskell interactive prompts. You can enter any R command and you can do basic mathematical computations. For example:
> 20+40  60 > print ("Hello World!")  "Hello World!"
You could test plotting too:
You can quit using q() or pressing CTRL+c. When doing so, you will be asked if you want to save a workspace image; a workspace is an environment for created variables.
Running R program with Rscript
The second way to run R programs is in directly on the Linux command line. You can do so using RScript, a utility included with r-base.
First, you have to save your R program to a file using your favorite code editor on Linux. The file extension should be .r.
This is my sample R program printing “Hello World”. I have saved it in a file name hello.r.
print("Hello World!") a <- rnorm(100) plot(a)
To run the R program, use the command like this:
You should get back the output:
 "Hello World!"
The plot is going to be saved in the working directory, to a file named Rplots.pdf:
Note: Rscript doesn’t load the methods package by default. Make sure to load it explicitly in your script.
Run R scripts with RStudio in Ubuntu
The most common way to use R is using RStudio, a great cross-platform open source IDE. You can install it using deb file in Ubuntu. Download the deb file from the link below. You’ll have to scroll down a bit to locate the DEB files for Ubuntu.
Once you download the DEB file, just double click on it to install it.
Once installed, search for it in the menu and start it. The home window of the application should pop up:
Here you have a working console, just like the one you got in the terminal with the R command.
To create a file, in the top bar click on File and select New File > Rscript (or CTRL+Shift+n):
Press CTRL+s to save the file and choose a location and a name it:
After doing so, click on Session > Set Working Directory > To Source File Location to change the working directory to the location of your script:
You are now ready to go! Write in your code and click run. You should be able to see output both in the console and in the plotting window:
In this article, I showed you step by step how to get started using the R programming language on an Ubuntu system. I covered several ways you can go about this: R console – useful for testing, Rscript – for the terminal lover, RStudio – the IDE for your needs.
Whether you are willing to get into data science or simply love statistics, R is a good addition to your programming arsenal, being the perfect tool for analyzing data.
If you are absolutely new to R, let me recommend you this excellent book that will teach you fundamentals of R. It’s available on Amazon Kindle.
Do you use R? Are you just getting into it? Let us know more about how and why you use or want to learn to use R!