The use of computers for computations and data analysis is inevitable. Numerous programming languages have been developed today to enhance computational efficiency and are continuously updated and improved to meet the evolving needs of users. Among a wide variety of programming languages, R and Python programming languages are very popular.
R is an open-source, free programming language and software environment developed for statistical computations, graphics, and data analysis. R has extensive libraries encompassing various statistical models and plotting methods, making it one of the most widely used programming languages in various domains. Moreover, the R programming language covers a wide range of domains, including Machine Learning Algorithms, Linear Regression, Time Series analysis, bioinformatics, and Statistical Inference.
If you are using Ubuntu Linux and intend to benefit from the features of the R programming language, this article is for you. In this article, you will learn how to install the R programming language in Ubuntu. Read this article to the end to benefit from the features of the R programming language.
R programming language Features
- Providing statistical and graphical techniques and models (linear and nonlinear modeling, classical statistical tests, time-series analysis, clustering, and more) for Statistical Analysis and Modeling
- Supporting extensive capabilities for Data Manipulation
- Providing various packages such as ggplot2, base R graphics for data visualization, customizable designs, charts, and visual representations of data.
- extensible and customizable
- Promotes reproducibility in research and data analysis
- Large and active community support
- Cross-platform compatibility and available for Linux and Windows
Prerequisite to install R in Ubuntu
- Ubuntu VPS
- User account with administrative privileges (Sudo access)
- Considering that data analysis requires significant computational resources, ensure your system meets the hardware requirements for your specific data analysis needs; for example, consider at least 1GB of RAM.
- Update package management system in Ubuntu (APT)
After considering these prerequisites, you are ready to install R in Ubuntu.
Quick guide to install R programming language on Ubuntu Linux
There are two ways to install the R programming language:
- Installing R from the official Ubuntu repository
- Installing R from the CRAN (Comprehensive R Archive Network) repository
The official Ubuntu repository supports the R programming language by default, so you can easily install the R programming language in Ubuntu by running a few commands. However, since Ubuntu’s default libraries are not updated regularly, the latest version of R may not be available in the Ubuntu repository.
CRAN stands for the Comprehensive R Archive Network and is the central hub for distributing R packages. The CRAN repository is maintained and supported by the R Foundation. CRAN includes updated, identical, and freely available versions of R along with R packages, documents, and other required resources. So, if you prefer to use the most up-to-date version of R, install R in ubuntu from the external CRAN repository.
Installing R in Ubuntu via the Ubuntu repository
If you don’t care about using the latest version of the R programming language and are looking for a simple and fast way to install R software, we recommend installing R through the official Ubuntu repository. To install R in Linux Ubuntu using the default Ubuntu repository, follow these steps:
1. Update Package Lists for Up-to-Date Information
Open the Ubuntu terminal by pressing Ctrl+Alt+T and update the list of packages by running the following command:
sudo apt update
2. Install R in Ubuntu
To install R from the default Ubuntu repository, use the following command:
sudo apt install r-base
By running this command, the base R package, along with essential components, will be installed.
3. Install R Development Tools (Optional and Recommended for Developers)
To install additional development tools and install new packages in the R console, install the r-base-dev package:
sudo apt install r-base-dev
Installing the r-base-dev package lets you use header files and static libraries to build the R package.
4. Verify Installation
To ensure a successful installation of R in Ubuntu, run the following command in the terminal:
By launching the R console, you can confirm the successful installation of R in Linux Ubuntu.
Running the R command without sudo will generate a personal library for the user. Launch the R console with sudo privileges to ensure that all users on the system have access to the libraries:
sudo -i R
R version 4.2.0 (2022-04-22) -- "Vigorous Calisthenics" Copyright (C) 2022 The R Foundation for Statistical Computing Platform: x86_64-pc-linux-gnu (64-bit) . . . Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. >
5. Install RStudio in Ubuntu (Optional)
RStudio is an Integrated Development Environment for R. You can enjoy the benefits of RStudio by downloading the .deb package from the official site and using
dpkg. First, go to the RStudio download page and download the appropriate version of RStudio for the version of Ubuntu you are using.
Then open the Linux terminal, navigate to the directory containing the downloaded .deb file, and install the .deb package using dpkg:
sudo dpkg -i rstudio-x.y.z-amd64.deb
Replace [rstudio-x.y.z-amd64.deb] with the actual file name of the downloaded package in the previous command.
After completing the installation process of RStudio in the Ubuntu system, you can start RStudio by running the following command:
Now, by installing R and RStudio, your Ubuntu system is equipped to analyze data and respond to programming needs.
Installing R in Linux Ubuntu via CRAN
Users who prefer to use the latest version of R should add the external CRAN repository to Ubuntu and install R using the APT package manager. Follow the steps below to install R using CRAN:
1. Download and add CRAN repository key
The process of installing R on Ubuntu through CRAN starts by adding the GPG public key of the R project to the Ubuntu system. Download the CRAN repository key using the wget command and use the gpg—dearmor command to change the format of the downloaded packages so that the apt package manager can verify the downloaded packages:
wget -qO- https://cloud.r-project.org/bin/linux/ubuntu/marutter_pubkey.asc | sudo gpg --dearmor -o /usr/share/keyrings/r-project.gpg
2. Add CRAN repository to source
To make apt aware of new sources, add the R source list to the source.list.d directory:
echo "deb [signed-by=/usr/share/keyrings/r-project.gpg] https://cloud.r-project.org/bin/linux/ubuntu jammy-cran40/" | sudo tee -a /etc/apt/sources.list.d/r-project.list
By typing the section [signed-by=/usr/share/keyrings/r-project.gpg] in the file, the apt package manager is instructed to use the downloaded GPG key to verify the repository information and files for R packages and add the CRAN repository to the list of Ubuntu sources.
3. Update and Install R
To make APT aware of the new R source, update the Ubuntu system and the list of available packages by running the following command:
sudo apt update
To ensure that the CRAN repository has been successfully added to the list of Ubuntu sources, you should see the following lines in the output of the previous command:
... Get:7 https://cloud.r-project.org/bin/linux/ubuntu jammy-cran40/ InRelease [3622 B] Get:8 https://cloud.r-project.org/bin/linux/ubuntu jammy-cran40/ Packages [15.6 kB] ...
After ensuring that the external repository is added to the Ubuntu system, install R ubuntu from the CRAN repository using the following command:
sudo apt install --no-install-recommends r-base
By running –no-install-recommends arguments, you prevent the installation of additional packages.
4. Verify Installation
Check if R is successfully installed by running the following command:
This command should display the version of R.
5. Launch R
Start R with sudo privileges as Root:
sudo -i R
Congratulations, you have successfully installed the R programming language in Ubuntu using the CRAN repository.
6. install the R packages from CRAN
The R programming language supports various packages, including machine learning, statistical modeling, and text mining, and you can install the available add-on packages using the
For example, to install the “ggplot2” package, which is a data visualization package and is used to create complex and adjustable graphics or charts, you must first enter the R console or (RStudio) and Run the following command to install the ggplot2 package from the CRAN repository:
In response to the ‘lib= “/usr/local/lib/R/site-library” is not writable‘ prompt, type yes.
After the installation is complete, run the following command to load the ggplot2 library:
If you don’t see an error, ensure the desired library is loaded successfully.
The ggplot2 package comes with preinstalled datasets to make the features and functions of the package available for the use of R. Use the following command to check the list of preinstalled datasets:
Press the q button to return to the R console.
For example, we will use one of the datasets you saw in the previous command’s output to plot both numeric and categorical variables. First, create a bar plot to visualize the relationship between the two variables:
# Load the ggplot2 library library(ggplot2) # Create example data set.seed(123) # for reproducibility n <- 100 data <- data.frame( category = sample(letters[1:5], n, replace = TRUE), value = rnorm(n, mean = 10, sd = 3) )
Now, create a bar plot to visualize the mean value for each category:
Create a bar plot using ggplot2 ggplot(data, aes(x = category, y = value)) + geom_bar(stat = "summary", fun = "mean", fill = "skyblue") + labs(x = "Category", y = "Mean Value", title = "Mean Value by Category")
Therefore, we were able to design a chart where the X-axis includes the categorical variable and value maps the numeric variable to the y-axis and create a bar plot where the height of each bar represents the mean value of value for each category.
In addition, the color of the bars is set to sky blue, and this chart will display the mean value of value for each category.
In addition to these options, features such as colors, labels, and other plot properties can be customized based on your preferences and the specific dataset you’re working with.
Note: To install other packages, for example, txtplot, dplyr, tidyr, etc., you must replace the name of the desired package with ggplot2 in the previous commands. Check the Available CRAN Packages By Name list for more information about official packages organized by name.
Therefore, to install precompiled packages from the CRAN repository, use the install.package() function. To get more information about the ggplot2 package, use the following command:
To update installed packages to the latest versions available on CRAN, use the following function:
As a result, by installing packages available from CRAN, you can improve your data analysis and modeling capabilities in R.
The R programming language is extensively used today for statistical computations and data analysis in universities, finance, commerce, research, healthcare, government, and various industries. It has become a standard tool for statisticians, data analysts, data scientists, and researchers working with data-centric insights. Therefore, utilizing the R programming language offers valuable advantages to a wide spectrum of individuals. This article taught how to install R in Ubuntu in two ways, enabling you to make the most of the R programming language’s features and capabilities.
As you know, R language is as popular as Python. Python is a general-purpose programming language with a broad range of applications, including data science. R, on the other hand, is a specialized language designed specifically for statistical analysis and visualization, and it is used as a powerful tool in the field of data science and statistics. Combining R with Python, two powerful ecosystems, may be the best way to take advantage of a wide range of tools and functions; for this reason, professional users in the field of data science and statistics, in addition to R, also install the Python on Linux system, which we have already taught. But if you had to choose one of these two popular programming languages, which one would you choose?