In this guide, we’ll learn to install R in Ubuntu. R is an open-source programming language used extensively for statistical and scientific computing. Due to the ever-growing demand for data science and analytics, the popularity of R has increased multiple folds. At the time of this writing, R is placed 14th in the most popular languages list by TIOBE, beating out other commercial data science languages such as SAS and Stata.
Steps to install R on Ubuntu
Let’s get into the steps to install and set up R in Ubuntu and its derivatives which include Linux Mint, Xubuntu, and many others!
1. Install Prequisites
First, we have to download some prerequisites which will help us manage external software sources and manage GPG keys.
apt install --no-install-recommends software-properties-common dirmngr
2. Setup GPG Keys
Now we can go ahead and the GPG key of CRAN repository. Adding GPG keys will tell the system that this external repository (CRAN) that we are going to add is trusted.
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
3. Add the R repository
Now we can go ahead and add the actual repository.
sudo add-apt-repository "deb https://cloud.r-project.org/bin/linux/ubuntu $(lsb_release -cs)-cran40/"
NOTE: This uses the
lsb_release -cs command to access the Ubuntu version and add the relevant repository. For example, if you are running Ubuntu 20.04 the $(lsb_release -cs) will be replaced by “Focal”. So if you are using an Ubuntu derivative like Mint or Elementary, you will have to replace
$(lsb_release -cs) with the corresponding Ubuntu release.
4. Update system and install R-base
Now we can go ahead and install the R package.
sudo apt update sudo apt install r-base
We have successfully installed R. To verify you can run
R --version which will give you the version of the R package that we just installed.
Installing R packages
You can install any R package using
install.packages('package_name') in the R console
Here is a list of all the packages offered by R.
Steps to Install R GUI mode
You can use this command to run R in graphical mode instead of terminal.
R -g Tk &
Run your first R script
There are two ways to do it.
You can start R in GUI using the above command and then go to File > Source R code and choose the program that you want to run there.
Another way is to use the RScript command.
NOTE: Any graphs/plots will be saved as pdf in the same directory as the R source code instead of being shown in the terminal.
Installing RStudio on Ubuntu
There are many IDEs available for R for example Emacs with ESS and VimR but Rstudio is probably the most popular RStudio across platforms.
To install RStudio we have to install the relevant deb package from here and then install it.
wget https://download1.rstudio.org/desktop/bionic/amd64/rstudio-2021.09.0%2B351-amd64.deb sudo dpkg -i rstudio-2021.09.0+351-amd64.deb
We have downloaded the package for Ubuntu 18 (Bionic) because that is the latest package available.
We have covered how to install R in Ubuntu and its derivatives. For any additional help, you can check out the official R website. Have fun and keep exploring 🙂