How to update R 3.x to the new R 4.x in Linux Ubuntu
I must confess: I’d rather use stable and long-term release software. Maybe I’m just getting older and, as the time flies, more resistant to dealing with changes. Today I was forced to update to R 4.0 due to a new research project I was invited to collaborate. Finally, it’s time to say goodbye to ‘stringsAsFactors=FALSE’. Updating R was a bit of a headache that I wouldn’t like others going through. In this post you’ll learn how to:
- Keep your R version always updated using the CRAN version
- Install R 4.0 in your Linux Ubuntu
- Reinstall your 3.x packages to the new 4.0
How can I update my R 3.x to the new shiny R4.x?
If you’re using a Linux distro, the process is a breeze. I use and love the PopOS 20.04.
First, you need to check the file ‘/etc/apt/sources.list’, and look for the line. ‘deb https://cloud.r-project.org/bin/linux/ubuntu focal-cran40/’
If you don’t have this line, you can run the following in a terminal session to use the R CRAN version:
$ sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9
$ sudo add-apt-repository 'deb https://cloud.r-project.org/bin/linux/ubuntu focal-cran40/'
Now install R by typing:
$ sudo apt install r-base
After that, check the R version.
$ R --version
You’ll get a similar output to this.
We still have work to do though…
Updating R 3.x packages to the new R 4.0.
As R 4.0 introduced several changes to base functions, programmers had to rewrite their packages. In order to update your already installed, just type the following in the R console:
update.packages(ask = FALSE, checkBuilt = TRUE)
This will probably will take a few minutes.
If you’re lazy like me, you’re ready to go. However, If want to update all your packages at once, stay with me.
Update all packages
In the R console, create an object with all installed packages in the previous version. You have to point to your installation. Mine was:
‘/home/henriquegomide/R/x86_64-pc-linux-gnu-library/3.6/’
old_packages <- installed.packages(lib.loc = "/home/henriquegomide/R/x86_64-pc-linux-gnu-library/3.6/")head(old_packages[, 1])
After that, compare the packages you had in 3.6 to your new 4.0 installation:
new_packages <- installed.packages()missing_packages <- as.data.frame(old_packages[!old_packages[, "Package"] %in% new_packages[, "Package"], ])
Then:
install.packages(missing_packages$Package)
This will probably take several minutes to run.
References
https://linuxize.com/post/how-to-install-r-on-ubuntu-20-04/
https://www.r-bloggers.com/get-all-your-packages-back-on-r-4-0-0/








