How to Install R Lang on Ubuntu 22.04 or 20.04

R Programming Language is a robust open-source tool for data analysis, statistics, and graphics. R has garnered immense popularity among data scientists, statisticians, and researchers worldwide with its user-friendly syntax. If you’re aiming to install R on Ubuntu 22.04 Jammy Jellyfish or its older stable release of Ubuntu 20.04 Focal Fossa, this introduction will provide insights into R’s capabilities and its significance in the data-driven world.

Key Attributes of R Programming Language:

  • Open-Source Nature: Being open-source, R is freely accessible and offers customization flexibility.
  • Diverse Statistical Techniques: R boasts a comprehensive library equipped with statistical and graphical methods, catering to diverse data analysis requirements.
  • Platform Independence: R’s compatibility with Windows, Mac, and Linux ensures a broad user reach.
  • Efficient Data Handling: Built-in functions in R facilitate seamless data manipulation, including tasks like merging, sorting, and reshaping datasets.
  • Reproducibility: R’s design emphasizes reproducible analyses, making it a preferred choice for scientific research and studies.

Originating in the early 1990s by Ross Ihaka and Robert Gentleman at the University of Auckland, New Zealand, R’s initial focus was statistical computing. However, its adaptability has led to widespread application across various sectors, including finance, healthcare, and government.

R’s Application Domains:

  • Data Analysis & Statistics: R excels in statistical modeling, data visualization, and machine learning applications.
  • Academic Pursuits: Researchers and academicians frequently opt for R for its data visualization and analysis capabilities.
  • Financial Sector: Risk analysis, portfolio management, and financial modeling in finance often rely on R.
  • Healthcare Industry: R finds its use in healthcare for data interpretation, clinical trials, and predictive modeling.

Follow our guide to harness the full potential of R’s data analysis and statistical prowess. We’ll walk you through the steps to install R Programming Language on Ubuntu 22.04 Jammy Jellyfish or Ubuntu 20.04 Focal Fossa, utilizing the command line terminal and the official CRAN repository for the most up-to-date version.

Import CRAN APT Repository on Ubuntu 22.04 or 20.04

Step 1: Update Ubuntu Before R Installation

Before installing R Programming Language on Ubuntu 22.04 (Jammy Jellyfish) or Ubuntu 20.04 (Focal Fossa), it’s crucial to start by updating your system. This ensures all existing packages are current. To do this, open your terminal and execute:

sudo apt update && sudo apt upgrade

Step 2: Installing Necessary Packages

Following the system update, the next step involves installing required packages. These packages are essential for the successful installation of R. Input the following command:

sudo apt install curl dirmngr apt-transport-https ca-certificates software-properties-common

Step 3: Importing the GPG Key for R Package Verification

To ensure the authenticity of the R package from the CRAN repository, you must import the GPG key. This key verifies the integrity and origin of the package. Use the following command to import the GPG key:

curl -fSsL https://cloud.r-project.org/bin/linux/ubuntu/marutter_pubkey.asc | sudo gpg --dearmor | sudo tee /usr/share/keyrings/cran.gpg >> /dev/null

Step 4: Adding the CRAN Repository to Your Ubuntu System

Once the GPG key is in place, the next action is to add the CRAN repository to your system’s sources. This repository provides the latest R packages. Implement this by entering:

echo deb [signed-by=/usr/share/keyrings/cran.gpg] https://cloud.r-project.org/bin/linux/ubuntu $(lsb_release -cs)-cran40/ | sudo tee /etc/apt/sources.list.d/cran.list

Finalizing with an APT Repository Update

After adding the CRAN repository, conclude the setup process by updating your APT repository list. This update ensures your system recognizes the newly added CRAN repository. Execute the following command:

sudo apt update

Install R on Ubuntu 22.04 or 20.04 via APT Command

Step 1: Core R Installation

After adding the CRAN repository to your Ubuntu system, you can proceed to install the R Programming Language. This foundational installation equips your system with the base R environment. To initiate the installation, open your terminal and execute:

sudo apt install r-base

Step 2: Installing R Development Tools (Optional)

For an enhanced development experience, you might consider installing r-base-dev. This package includes additional development tools and libraries that are essential for more advanced R programming tasks. Install it using the following command:

 sudo apt install r-base r-base-dev

Step 3: Verifying the R Installation

To confirm a successful installation, it’s important to check the installed version of R. This step ensures that R is properly installed on your system. In the terminal, run:

R --version

This command displays the version and build information of R, confirming the success of your installation.

Screenshot of R language version details on Ubuntu 22.04 or 20.04.
Displaying the installed R version on Ubuntu.

Step 4: Additional R Installation Options on Ubuntu

Installing Recommended R Packages

r-recommended is a valuable package that includes a curated selection of R packages, widely used in data analysis and statistical modeling. To install this package, use the command:

sudo apt install r-recommended

Installing SSL Support for CRAN Packages

For installing CRAN packages that require SSL encryption, such as the “httr” package, libssl-dev is necessary. Install this package with:

sudo apt install libssl-dev

Adding XML Parsing Capability

To install CRAN packages needing XML parsing, libxml2-dev is required. This is particularly crucial for packages like “XML”. Install it using:

sudo apt install libxml2-dev

Enabling CURL Support in R

For CRAN packages that require CURL (Client URL) support, like the “curl” package, libcurl4-openssl-dev is essential. Use this command for installation:

sudo apt install libcurl4-openssl-dev

These additional packages enhance R’s functionality on Ubuntu, enabling it to efficiently interact with various software and tools.

Install R Packages on Ubuntu 22.04 or 20.04 via CRAN

Launching the R Interpreter

With the R Programming Language installed on your Ubuntu system, you’re now ready to install R packages from the Comprehensive R Archive Network (CRAN). To start the R interpreter, open your terminal and run the following command:

sudo -i R

This command initiates the R interpreter with root user privileges, granting you the necessary permissions to install packages system-wide.

Installing R Packages

In the R environment, you can install packages using the install.packages() function. This function is the standard method for adding new packages to your R setup. For instance, to install the ggplot2 and tidyr packages, input:

install.packages(c("ggplot2", "tidyr"))
Screenshot demonstrating package installation from CRAN for R on Ubuntu 22.04 or 20.04.
Process of adding new R packages from CRAN on Ubuntu.

Searching for CRAN Packages

To explore available packages in CRAN, utilize the available.packages() function. This is particularly useful for finding packages related to specific fields like machine learning. To search for machine learning-related packages, use:

available.packages("machine learning")

This function returns a comprehensive list of relevant packages along with their descriptions, aiding in informed decision-making about which packages to install.

Updating Installed R Packages

Keeping your R packages up-to-date is crucial for security and functionality. To update all installed packages, use the update.packages() function. For example, to update without individual confirmations, execute:

update.packages(ask = FALSE)

This command efficiently updates all your installed packages to their latest versions, bypassing the need for manual confirmation.

Removing R Packages

If you need to remove an installed R package, the remove.packages() function comes in handy. To delete a specific package, such as dplyr, from your system, input:

remove.packages("dplyr")

This straightforward approach ensures you can manage the packages on your system effectively, keeping only those that are necessary for your work.

Understanding CRAN in R and CRAN LaunchPAD PPA

When working with the R Programming Language on Ubuntu systems, it’s vital to distinguish between two methods of installing R packages: through the R interpreter and via the CRAN LaunchPAD Personal Package Archive (PPA). The CRAN repository within the R interpreter is used for installing R packages directly in the R environment. In contrast, the CRAN LaunchPAD PPA enables Ubuntu users to install R packages system-wide using the APT package manager.

This distinction plays a significant role in package installation and management for R developers, particularly those new to the ecosystem.

Adding CRAN LaunchPAD PPA to Ubuntu

To expand your Ubuntu system’s R package capabilities, add the current R 4.0 or later c2d4u repository. This repository enhances your system with a wider range of R packages, optimized for Ubuntu-based distributions. Run the following command with root privileges or using sudo:

sudo add-apt-repository ppa:c2d4u.team/c2d4u4.0+

Adding this repository is a crucial step in accessing an extensive array of R packages through your system’s package manager.

Installing R Packages from the PPA

With the repository added, you can now install R packages across the entire system. For instance, to install specific packages like r-cran-rstan or r-cran-tidyverse without the recommended dependencies, execute one of these commands:

sudo apt install --no-install-recommends r-cran-rstan

or

sudo apt install --no-install-recommends r-cran-tidyverse

Using these commands, you install the desired R packages system-wide. This approach is particularly beneficial in shared or multi-user environments where system-wide access is required.

Note on LTS Releases and Unsupported Usage

It’s crucial to note that the c2d4u repository is officially supported only on Long-Term Support (LTS) releases of Ubuntu. Usage on non-LTS releases may lead to compatibility challenges. Although it’s feasible to modify the c2d4u_team-ubuntu-c2d4u4_0_-*.list file to use a focal repository for non-LTS versions, this method is not officially sanctioned and could result in unforeseen issues.

Conclusion

In this guide, we’ve walked through the essential steps to install R Programming Language on Ubuntu 22.04 or 20.04, showcasing how to add the CRAN LaunchPAD PPA for a wider array of R packages and manage these packages effectively. Remember, choosing the right method for installing R packages—whether through the R interpreter or using the CRAN LaunchPAD PPA—can significantly impact your development workflow.

Our final recommendation is to stay updated with the latest R versions and packages for optimal performance and security. And don’t forget, if you’re working with LTS releases of Ubuntu, you’ll find the c2d4u repository particularly useful for maintaining a robust and diverse R environment. Happy coding!

Leave a Comment