How To Install R Programming Language in Linux

R is a popular programming language and software environment used to build statistical and graphical computing tools for data science. It is in many ways similar to the S programming language and environment; R is a different implementation of S. Although there are some significant differences between the two.

R is free software available under the terms of the Free Software Foundation’s GNU General Public License. It is also cross-platform, it can be compiled and run on Linux, and other UNIX-based operating systems including FreeBSD and MacOS; and Windows as well.

R supports a variety of statistical (linear and nonlinear modeling, classical statistical tests, time-series analysis, classification, clustering, etc) and graphical techniques.

R Programming Language’s Key Features

  • Offers effective data handling and storage facility.
  • Provides a suite of operators for calculations on arrays, in particular matrices.
  • Ships with a large, coherent, integrated collection of intermediate tools for data analysis.
  • Offers graphical facilities for data analysis and display either on-screen or on hardcopy.
  • Provides conditionals, loops, user-defined recursive functions, and input and output facilities.
  • Allows users to add additional functionality by defining new functions.
  • It’s highly extensible via packages, about eight packages are supplied with the R distribution and many others are available through the CRAN (Comprehensive R Archive Network) family of Internet sites.
  • Supports for easily creating well-designed publication-quality plots such as mathematical symbols and formulae where needed, and.
  • Most S programs can run unaltered in R.
  • Also, for computationally-intensive tasks, C, C++, and Fortran code can be linked and called at run time.
  • Ships with comprehensive documentation, in LaTeX-like documentation format.

Installing R Programming Language in Linux

You can install R programming language packages on your Linux system as shown below. On RHEL-based distributions, you need to it from the EPEL (Extra Packages for Enterprise Linux) repository.

Note: Remember to run the correct set of commands for your Linux distribution.

Install R on Ubuntu

$ sudo apt update -qq
$ sudo apt install --no-install-recommends software-properties-common dirmngr 
$ 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 
$ sudo add-apt-repository "deb https://cloud.r-project.org/bin/linux/ubuntu $(lsb_release -cs)-cran40/"
$ sudo apt update
$ sudo apt install --no-install-recommends r-base

Install R on Debian

$ sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-key '95C0FAF38DB3CCAD0C080A7BDC78B2DDEABC47B7'
$ sudo add-apt-repository "deb https://cloud.r-project.org/bin/linux/debian $(lsb_release -cs)-cran40/"
$ sudo apt update
$ sudo apt install --no-install-recommends r-base

Install R on Fedora

$ sudo dnf install R

Install R on RHEL Systems

--------- On RHEL 9 --------- 
$ sudo subscription-manager repos --enable codeready-builder-for-rhel-9-$(arch)-rpms
$ sudo dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-9.noarch.rpm
$ sudo dnf install R

--------- On RHEL 8 ---------
$ sudo subscription-manager repos --enable codeready-builder-for-rhel-8-$(arch)-rpms
$ sudo dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
$ sudo dnf install R

--------- On RHEL 7 ---------
$sudo subscription-manager repos --enable rhel-*-optional-rpms \
                           --enable rhel-*-extras-rpms \
                           --enable rhel-ha-for-rhel-*-server-rpms
$ sudo yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
$ sudo dnf install R

Install R on CentOS Stream

--------- On CentOS Stream 9 ---------
$ sudo dnf config-manager --set-enabled crb
$ sudo dnf install epel-release epel-next-release
$ sudo dnf install R

--------- On CentOS Stream 8 ---------
$ sudo dnf config-manager --set-enabled powertools
$ sudo dnf install epel-release epel-next-release
$ sudo dnf install R

--------- On CentOS 7 ---------
$ sudo yum install epel-release
$ sudo dnf install R

Install R on Rocky and AlmaLinux

--------- On Rocky and AlmaLinux 9 ---------
$ sudo dnf config-manager --set-enabled crb
$ sudo dnf install epel-release
$ sudo dnf install R

--------- On Rocky and AlmaLinux 8 ---------
$ sudo dnf config-manager --set-enabled powertools
$ sudo dnf install epel-release
$ sudo dnf install R

Install R on openSUSE

$ sudo VERSION=$(grep "^PRETTY_NAME" /etc/os-release | tr " " "_" | sed -e 's/PRETTY_NAME=//' | sed -e 's/"//g')
$ sudo zypper addrepo -f http://download.opensuse.org/repositories/devel\:/languages\:/R\:/patched/$VERSION/ R-base
$ sudo zypper install R-base R-base-devel

Verify R Installation in Linux

After installing the R packages, you can verify if the environment is set up well. To start the R program, run the R command which will launch the R program shell as shown in the next screenshot.

$ R 
Start R Program in Linux
Start R Program in Linux

You can run a hello world program in R as shown:

>print("HelloWorld")
>print("HelloWorld", quote=FALSE)
Run Hello World Program in R Language
Run Hello World Program in R Language

To quit the R program shell, issue the q() command. You will be prompted whether you want to save the data from your R session, enter y for yes or n for no, or c to cancel:

> q()
Quit R Program Shell
Quit R Program Shell

Besides, you can run an R script using the Rscript (front end for scripting with R) command as follows:

$ cat hello.r
$ Rscript hello.r
Run R Script Using Rscript
Run R Script Using Rscript

For more usage options, read the R and Rscript man pages:

$ man R
$ man Rscript

We have come to the end of this guide. For more information, visit the R project’s official website.

Hey TecMint readers,

Exciting news! Every month, our top blog commenters will have the chance to win fantastic rewards, like free Linux eBooks such as RHCE, RHCSA, LFCS, Learn Linux, and Awk, each worth $20!

Learn more about the contest and stand a chance to win by sharing your thoughts below!

Aaron Kili
Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

4 Comments

Leave a Reply
  1. I installed R on AlmaLinux 9 following the instructions in this article. However, when I try to start R, it fails with the following error message:

    flexiblas dlopen: /lib64/libgfortran.so.5: version `GFORTRAN_8' not found (required by /usr/lib64/flexiblas/libflexiblas_netlib.so)
    flexiblas: Failed to load the BLAS fallback library. Aborted!
    

    System Information:

    5.14.0-427.42.1.el9_4.x86_64 #1 SMP PREEMPT_DYNAMIC Fri Nov 1 14:58:02 EDT 2024 x86_64 x86_64 x86_64 GNU/Linux
    
    Reply
    • @Gosta,

      The error message you’re seeing, specifically GFORTRAN_8 not found, indicates that the installed version of libgfortran does not meet the requirements for FlexiBLAS, which is likely a dependency for R. FlexiBLAS is trying to use a version of libgfortran that isn’t installed on your AlmaLinux 9 system.

      Here’s a step-by-step approach to troubleshoot and resolve this issue:

      1. First, make sure all your system packages are up to date. Sometimes updating can solve compatibility issues.

      sudo dnf update -y
      

      2. Next, you might need to install the GCC version that includes the correct libgfortran library.

      sudo dnf install gcc-gfortran
      

      3. Download and install a compatible libgfortran version.

      sudo dnf install epel-release
      sudo dnf install compat-gcc-8-gfortran
      

      4. If you continue to experience issues with FlexiBLAS, you might want to configure it to use a different BLAS library that doesn’t have the same version dependency.

      sudo update-alternatives --config libblas.so.3
      sudo update-alternatives --config liblapack.so.3
      

      5. After completing the above steps, try running R again to see if the error is resolved:

      R
      

      6. If issues persist, you might consider reinstalling FlexiBLAS or R, as an incorrect installation can sometimes cause these issues.

      sudo dnf reinstall flexiblas
      sudo dnf reinstall R
      

      After completing these steps, R should start without the FlexiBLAS error. Let me know if you run into further issues!

      Reply
      • @Ravi Saive

        Thank you so much for your help!

        I went through it step by step, but R still isn’t working.

        I think the main issue now is with step 3, the second line: sudo dnf install compat-gcc-8-gfortran.

        It gives the following error:

        No match for argument: compat-gcc-8-gfortran Error: Unable to find a match: compat-gcc-8-gfortran
        

        No error messages occurred with any of the other steps, but R still aborts with the message:

        flexiblas dlopen: /lib64/libgfortran.so.5: version 'GFORTRAN_8' not found (required by /usr/lib64/flexiblas//libflexiblas_netlib.so) flexiblas: Failed to load the BLAS fallback library. Abort! Aborted (core dumped)
        
        Reply
        • @Gosta,

          It looks like the issue is with the version of libgfortran required by flexiblas. Since compat-gcc-8-gfortran isn’t available in your current repository, you can try the following steps to resolve the issue:

          Install the correct version of libgfortran:

          sudo dnf install gcc-gfortran
          

          Next, ensure that the correct version of flexiblas is installed:

          sudo dnf install flexiblas
          

          Make sure that your system is up-to-date and has all the necessary dependencies:

          sudo dnf update
          sudo dnf install libgfortran
          

          Once you’ve done this, try running R again.

          Hopefully, this resolves the error. Let me know how it goes!

          Reply

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.