Econometrics and Free Software by Bruno Rodrigues.
RSS feed for blog post updates.
Follow me on Mastodon, twitter, or check out my Github.
Check out my package that adds logging to R functions, {chronicler}.
Or read my free ebooks, to learn some R and build reproducible analytical pipelines..
You can also watch my youtube channel or find the slides to the talks I've given here.
Buy me a coffee, my kids don't let me sleep.

A Linux Live USB as a statistical programming dev environment


This blog post is divided in two parts: in the first part I’ll show you how to create a Linux Live USB with persistent storage that can be used as development environment, and in the second part I’ll show you the easiest way to set up RStudio and R in Ubuntu.

Making your own, portable, development environment based on Ubuntu or Debian

I’m currently teaching a course at the University of Luxembourg, which focuses on setting up reproducible analytical pipelines (if you’re interested, you can find the course notes here).

The problem is that my work laptop runs Windows, and I didn’t want to teach on Windows since I make heavy use of the command line. Plus I don’t have admin rights on this machine, so installing what I needed would have been a pain. I also don’t have a personal laptop, so I use my wife’s laptop. However, the laptop is completely full of pictures of our kids, so I couldn’t install what I needed… This is when I thought about making a persistent live USB with Kubuntu on it (Kubuntu is a variant of Ubuntu with KDE as the desktop manager instead of Gnome) with all the software I needed (R, Quarto, RStudio basically). It works quite well, and was also quite easy to do. But what is a live USB anyways? A live USB is a full Linux installation on a USB stick, that you can use to test different Linux distributions or even to install said distribution on computers.

The first step is to get a USB stick. Those are quite cheap nowadays, but you’ll need at least one with 8GB of space, and ideally USB 3 (you probably can’t find any USB 2 these days anyways). I’ve bought a 32GB one for 10€.

Then, we need to install Ubuntu on it. I’ll be using Kubuntu 22.04, which is an LTS release. I would always recommend an LTS release for something like crafting a development environment. So if you’re reading this in the future, and there’s a new LTS (could be 24.04, 26.04, etc), you’d need to get that one.

Creating a live USB is quite simple, but the issue if you create a live USB using the standard methods is that whatever you do on it once you’re logged in will get erased after rebooting. A persistent live USB, I’m sure you’ve guessed it, keeps your changes even after rebooting, which means that you basically end up with a portable development environment. Note however that only Ubuntu (and variants) or Debian can be used to create persistent live USBs.

You can create persistent live USB from another Linux distro, Windows or macOS.

If you’re already running Ubuntu on your pc, you might want to take a look at this page. You’ll need to install a tool called mkusb. If you’re not running Ubuntu, but find this tool in your distribution’s package manager, I guess you’re good to go as well. In my case, I’m running opensuse tumbleweed, and could not find this program in the opensuse’s repositories. So I’ve used this guide that shows how to achieve the same thing using a very simple to use shell script which you can get here called mkusb-minp. So in my case, I simply had to stick the USB stick in my computer, find out where it was mounted by running df in bash (in my case it was in /dev/sdd), download Kubuntu’s iso image and run the following in my terminal:

sudo ./mkusb-minp -p kubuntu-22.04.1-desktop-amd64.iso /dev/sdX

(/dev/sdX: replace the X by the right letter, for me it was /dev/sdd)

If you’re using Windows, you can install Rufus to create a persistent live USB.

It would seem that for macOS the process is a bit more involved, but I’ve found this blog post that explains the process.

Once the process is finished, you can boot into your live USB key. For this, you might need to press delete or F2 when your computer starts booting to access the boot menu. You can then choose to boot from your USB device.

Wait a bit and at some point you should see a prompt asking you if you want to try or install Ubuntu. Choose Try Ubuntu:

And then wait some minutes. Yes booting takes some time because you’re loading an entire operating system from a USB stick (hence why it’s a good idea to go with a USB 3 stick). After some time you should see a new window:

Once again, try Ubuntu, wait a bit, and that’s it you’re inside your dev environment!

Setting up R and RStudio

Now that you’re inside your dev environment, you actually need to start adding some tools. Let’s start by adding R. The easiest way that I found is to use the r2u project by Dirk Eddelbuettel. If you’re on Ubuntu 22.04, run this script, as explained in the tutorial. This will add the required repositories that will install binary versions of R packages in mere seconds. The script will also add a repository to install the most recent version of R, so once the script is done running, install R and the {tidyverse} (or any other package) with the following command:

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

You can then install other packages from R using install.packages("package_name") as usual, and this will also make use of the r2u repositories.

All that’s missing now is RStudio (if you use RStudio). Surprisingly, when I set up my live USB two weeks ago, the current version of RStudio for Ubuntu would not install. This is apparently fixed with the daily versions which you can get here. But before that, do try to install the stable version. If you’re reading this sometime in the future, maybe the issue I encountered has been fixed. Download RStudio from here, and then double click on the downloaded .deb package. If you see this message:

The following packages have unmet dependencies:
 rstudio : Depends: libssl1.0.0 but it is not installable or
                    libssl1.0.2 but it is not installable or
                    libssl1.1 but it is not installable
           Recommends: r-base (>= 3.0.1) but it is not going to be installed
E: Unable to correct problems, you have held broken packages.

then this means that the problem has not been fixed. In that case, run the following line to repair everything:

sudo apt-get update --fix-missing

This should put you back into a clean state. So to continue, install a daily build from the link above. Simply click on the Ubuntu 22 button to download the daily. Unfortunately daily builds can be unstable and are usually used for testing purposes. So hopefully Posit will fix this soon.

Of course, if you’re using the greatest IDE ever made instead of RStudio, you won’t have this issue.

You can now keep installing things, for example Quarto, or Python, or, or, or… there are no limits, and performance, as you would have noticed is great, because the operating system has access to all the resources from your machine. A persistent live USB is a great solution if you need a portable dev environment and don’t want/can’t use Docker for example.

Hope you enjoyed! If you found this blog post useful, you might want to follow me on Mastodon or twitter for blog post updates and buy me an espresso or, or buy my ebook on Leanpub. You can also watch my videos on youtube. So much content for you to consoom!

Buy me an EspressoBuy me an Espresso