
What is R?
R is a popular statistical computing programming language commonly used by data scientists and software developers to manipulate data in various use cases.
With the use of R you are able to execute various tasks such as statistical calculations of data, data visualization through graphical sets or even hypothesis testing.
Another excellent aspect of the R programming language is that it is also open source and offers a wide range of packages within its libraries.
Within this article we’ll explain how you can install R on Ubuntu so you can begin to utilize it within your environments.
There are two methods which we will go over such as direct install from the Ubuntu repositories or from the CRAN repository which is slightly more involved but still relatively straightforward.
It is worth noting that using the CRAN repository for the installation of R is typically the best bet as then you can ensure you’re installing the most recent release of R on your Ubuntu machine.
How to install R on Ubuntu using the CRAN repository
As mentioned if we want to obtain the most recent release of R it would be best to install from the CRAN repository which we’ll go over in this section.
1. Run an apt upgrade
First we’re going to make sure all of our software packages are up to date on Ubuntu with the following command (use sudo apt update or upgrade if running as a sudo privileged user):
$ apt upgrade -y

2. Adding helper packages for the CRAN repository:
We will also add the following packages although they are not always needed but can be helpful:
$ apt install software-properties-common dirmngr -y

3. Add the public signing key for the CRAN repository
Now that all of our software packages are up to date we can proceed to add the needed repository so R can be installed.
First we will need to use ‘wget’ to obtain the a public signing key for the CRAN repo:
$ 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 CRAN repository so we can then install the latest version of R
Now we can add the needed repository so R can be installed with the following command:
$ add-apt-repository "deb https://cloud.r-project.org/bin/linux/ubuntu $(lsb_release -cs)-cran40/"

If prompted with the below press ENTER to continue:

From the below screen shot we can see the repository was successfully installed so we’re ready to move forward:

4. Install R base and its dependencies:
Now that we’ve updated our Ubuntu system and added the needed repository to obtain the newest version of R we’re now ready to add it to our system.
In the following example we’re going to also add the r-base-dev package so users will be able to utilize a function in the R console to add new packages:
$ apt install r-base r-base-dev -y

This install may take a minute or so to finish but once complete we will be all set and ready to verify.
5. Verify the installation of R
Now we can verify with the following command and pressing ENTER to ensure everything was installed correctly:
$ R
From the below we see we’re launched into a command prompt within the R environment control prompt:

To exit the R control prompt we would just type in the following and press ENTER.
It will ask if we want to save any work completed before exiting which we can enter in Y for yes, N for no, or C for cancel and remain in the console:

Now we have successfully verified our installation of R and its dependencies were a success.
How to install R using Ubuntu repository
On the other hand we can also install R using the Ubuntu repositories and do not necessarily need to add another repo onto Ubuntu in order to do so.
In this example we will be installing R from the Ubuntu repository and it is a pretty straight forward process overall.
Most of the steps are the same except we won’t be needing to add a signing key or repo to our work station for the installation.
1. Run an apt upgrade
Just like in the other method when using the CRAN repository to install R, we’re going to first update our system packages (use sudo apt update or upgrade if running as a sudo privileged user):
$ apt upgrade -y

2. Install R base and its dependencies:
Now that everything is up to date lets install R and its dependencies such as in the other method we mentioned above:
$ apt install r-base r-base-dev -y

3. Verify the installation
Let’s verify everything was installed correctly by launching the R control prompt:

And if we need to exit we can use the following:

Everything appears to be launching correctly per the above after verifying our installation of R and its dependencies so now we can begin using the program.
Installing a package within the R control prompt
In the above two installation methods we also added r-base-dev during our install which allows users to install programs in the R console using this syntax with the following function:
install.packages('name_of_package')
1. Launch into the R console prompt
First we’re going to launch into the R console just by hitting R and pressing enter in our terminal:

2. Install a package using the install.packages function
Now lets say we would like to add the ‘readr’ package. To do so, we would just enter in the following based off our above examples which was:
install.packages('name_of_package')
So to install ‘readr’ we would use the following:
install.packages('readr')

The install may take a minute or so to complete but the above would add that specific package.
Installing multiple packages within the R control prompt
You can also install more than one package at a time using the following syntax with the install.packages function on the R console:
$ install.packages(c('example-package1', 'example-package2', 'example-package3'))
Notice how we’ve introduced the c() function in the above example. This will allow for us to add as many packages at a time that we would like.
Finding packages from the CRAN repository
If you’re unsure of what packages you need for a specific project you can also visit the CRAN Modules page which lists categories of tasks/topics and after selecting one the topics it will list R packages related to that topic.
Conclusion
R is a very popular programming language which allows you to manipulate data in various ways.
Most commonly used by software developer and statisticians, R allows for data visualization, hypothesis testing, and overall data manipulation.
Within this article we explain two methods for installing R on Ubuntu through different repositories.
As we mentioned you can install R using the Ubuntu repositories however if you want to ensure you’re installing the latest release of R it is typically best to install using the CRAN repository.
Leave a Reply