How To Install R on Ubuntu 16.04
R is an open source programming language which specializes in statistical computing and graphics. It is usually used for developing statistical software and performing data analysis. R is pretty extensive and the community always adds user-generated packages for particular areas of study, which helps it be applicable to a lot of fields.
In this tutorial, we will teach you how to install R and show you how to can add packages from the official Comprehensive R Archive Network (CRAN).
To complete this tutorial, you will require an Ubuntu 16.04 server with the following specifications:
– Minimum of 1 GB of Ram
– Non-root user with sudo privileges
Once these requirements are in place, you are ready to start.
R is a project which progresses fast, therefore the latest stable version won’t always be available from Ubuntu’s repositories; due to that, begin by appending the external repository which is maintained by CRAN:
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9
Output: E298A3A825C0D65DFD57CBB651716619E084DAB9 gpg: requesting key E084DAB9 from hkp server keyserver.ubuntu.com gpg: key E084DAB9: public key "Michael Rutter <[email protected]>" imported gpg: Total number processed: 1 gpg: imported: 1 (RSA: 1)
After obtaining the trusted key in every server’s database, you may add the repository.
sudo add-apt-repository 'deb [arch=amd64,i386] https://cran.rstudio.com/bin/linux/ubuntu xenial/'
You will have to run an update after this is done so the package is included, allowing it to manifest from the new repository.
sudo apt-get update
Output . . . Get:6 https://cran.rstudio.com/bin/linux/ubuntu xenial/ InRelease [3,590 B] Get:7 https://cran.rstudio.com/bin/linux/ubuntu xenial/ Packages [31.5 kB] . . .
In the case you have the same lines as shown above, it means that you’ve successfully added the repository. You are guaranteed to not install an older version of R.
So, let’s install R:
sudo apt-get install r-base
When this tutorial was made, the latest version from CRAN was 3.3.1, this will be presented to you once you’ve started R.
As you’re planning to install the example package for everybody on the system, initiate R as the root user so that the libraries are available to every user automatically.
sudo -i R
Output R version 3.3.1 (2016-06-21) -- "Bug in Your Hair" . . . 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. >
This is going to verify that you have successfully installed R and entered its interactive shell.
Installing R Packages from CRAN
One of the many reasons that R is excellent is the availability of add-on packages. As an example, we are showing you how to install ‘txtplot’, this is a library which outputs ASCII graphs, as well as scatterplot, line plot, density plot, acf, and bar charts.
While going through the installation process, you will be presented with a couple of mirrors to install from.
Output --- Please select a CRAN mirror for use in this session --- HTTPS CRAN mirror 1: 0-Cloud [https] 2: Algeria [https] 3: Australia (Melbourne) [https] 4: Australia (Perth) [https] 5: Austria [https] 6: Belgium (Ghent) [https] 7: Brazil (SP 1) [https] 8: Bulgaria [https] 9: Canada (MB) [https] 10: Chile [https] 11: China (Beijing 4) [https] 12: Colombia (Cali) [https] 13: Czech Republic [https] 14: Denmark [https] 15: France (Lyon 1) [https] 16: France (Lyon 2) [https] 17: France (Marseille) [https] 18: France (Paris 2) [https] 19: Germany (Falkenstein) [https] 20: Germany (Münster) [https] 21: Iceland [https] 22: Ireland [https] 23: Italy (Padua) [https] 24: Japan (Tokyo) [https] 25: Malaysia [https] 26: Mexico (Mexico City) [https] 27: New Zealand [https] 28: Norway [https] 29: Philippines [https] 30: Russia (Moscow) [https] 31: Serbia [https] 32: Spain (A Coruña) [https] 33: Spain (Madrid) [https] 34: Switzerland [https] 35: Taiwan (Chungli) [https] 36: Turkey (Denizli) [https] 37: UK (Bristol) [https] 38: UK (Cambridge) [https] 39: UK (London 1) [https] 40: USA (CA 1) [https] 41: USA (IA) [https] 42: USA (KS) [https] 43: USA (MI 1) [https] 44: USA (TN) [https] 45: USA (TX) [https] 46: USA (WA) [https] 47: (HTTP mirrors) Selection: 1
In the example, we have used 1 for 0-Cloud, this will connect us to the Content Delivery Network (CDN) given by RStudio and through this we’ll receive the closest geographic option.
This mirror is going to be set as your default for the rest of the session. After you exit R and re-enter, you will be asked to select a mirror again.
Before the list of mirrors, the output below showed the location the package was being installed on.
Output Installing package into ‘/usr/local/lib/R/site-library’ (as ‘lib’ is unspecified) . . .
This site-wide path is available since we’ve ran R as root and it is the right location to make the package available to every user.
Once the installation is done, you may load ‘txtplot’.
If you didn’t receive any error messages, this means the library was loaded successfully.
Let’s see how it does in action now using an example which tests a basic plotting function with axis labels. The example data, provided by R’s datasets package, has the speed of cars and the distance those cars needed to stop based on data from the 1920s.
txtplot(cars[,1], cars[,2], xlab = "speed", ylab = "distance")
Output +----+-----------+------------+-----------+-----------+--+ 120 + * + | | d 100 + * + i | * * | s 80 + * * + t | * * * * | a 60 + * * * * * + n | * * * * * | c 40 + * * * * * * * + e | * * * * * * * | 20 + * * * * * + | * * * | 0 +----+-----------+------------+-----------+-----------+--+ 5 10 15 20 25 Speed
If you’d like to know more about ‘txtplot’, use ‘help (txtplot)’.
You have the option to install any precompiled package from CRAN using ‘install.packages()’.
If you would like to know more about what’s available, you could find a listing of official packages organized by name and publication date under the Packages link on any mirror.
You should now have R successfully installed and ready to go. If you’re interested, we have made a guide to installing the open source RStudio server, it is an interface to a version of R running on a remote Linux server.