Want to share your content on R-bloggers? click here if you have a blog, or here if you don’t.
Introduction
I was interested to see whether and how well R would run on the new RISC-V architecture.
A while ago I read that RISC-V is now a
first class architecture for Ubuntu.
This got me thinking, instead of having to build R from source maybe the r-base
package might be available for RISC-V. It turns out that this is indeed the case, the architecture we are interested in is riscv64
. The launchpad page for r-base
is
here. Clicking through the subpages for each version of Ubuntu I can see that R is available for RISC-V from Ubuntu Focal Fossa onwards (for which the version of R is 3.6.3; and the latest version of Ubuntu has the current version of R of 4.5.0).
Why do this? I have no immediate need for this. However, there are now quite a few affordable RISC-V single board computers available, and so a similar argument holds to that made by
the R4Pi (R for the Raspberry Pi project) that running R on such affordable machines is a
great benefit because it opens R up to a whole new user base and whole new set of low power use cases.
Emulating RISC-V on an Apple Silicon Mac
I don’t have a RISC-V computer, therefore, I needed to use emulation.
My setup is that I’m on an Apple Silicon M4 MacBook Air. I thought this might be promising to use because this has an ARM processor which is a reduced instruction set architecture, as is RISC-V.
I wondered whether to try
UTM or
QEMU. I tried UTM first but I couldn’t make any progress. So I found a
tip online saying that RISC-V Ubuntu could be launched under QEMU on Ubuntu Linux using the following command.
qemu-system-riscv64 \ -machine virt \ -nographic \ -m 12288 -smp 4 \ -bios /usr/lib/riscv64-linux-gnu/opensbi/generic/fw_jump.elf \ -kernel /usr/lib/u-boot/qemu-riscv64_smode/uboot.elf \ -device virtio-net-pci,netdev=eth0 \ -netdev user,id=eth0,hostfwd=tcp::2222-:22 \ -drive file=ubuntu-24.10-preinstalled-server-riscv64.img,format=raw,if=virtio
Firstly one needs to obtain the Ubuntu img. Following
this incredible guide by Canonical we can choose one of the three versions of Ubuntu listed (Noble, Oracular, and Plucky).
The image downloads as an .img.xz archive, which you can extract by installing xz
(I use
Homebrew for system packages on macOS)
brew install xz
and decompressing with
xz --decompress ubuntu-24.10-preinstalled-server-riscv64.img.xz
The extracted file is about 4GB, but later on I realised I needed a slightly larger harddisk for the virtual machine, so I increased it to 8GB with the following command.
qemu-img resize ubuntu-24.10-preinstalled-server-riscv64.img 8G
I then realised that I needed QEMU on my Mac. Again Homebrew to the rescue with the following command.
brew install qemu u-boot-tools
(Admittedly I don’t think I ended up using the u-boot-tools.) Next I needed the two files; fw_jump.elf and uboot.elf. I had a look in /opt/homebrew/Cellar/qemu/10.0.2/ but I couldn’t work out if they are in there or not (there are some zip archives in some subdirectories). There are some official documentation pages
here and
here but I couldn’t follow them. I then found a comment that said you can
copy them from an Ubuntu installation, which I implemented in Docker.
docker run -it --rm --platform linux/arm64 \ -v $PWD:/home ubuntu:24.04 bash /home/copy-files.sh
where copy-files.sh contains
apt update apt upgrade -y apt install -y opensbi qemu-system-misc u-boot-qemu cp /usr/lib/u-boot/qemu-riscv64_smode/uboot.elf /home/uboot.elf cp /usr/lib/riscv64-linux-gnu/opensbi/generic/fw_jump.elf /home/fw_jump.elf
This saves the two files to the current working directory, so I could modify my call to qemu-system-riscv64
to be as follows.
qemu-system-riscv64 \ -machine virt \ -nographic \ -m 12288 -smp 4 \ -bios fw_jump.elf \ -kernel uboot.elf \ -device virtio-net-pci,netdev=eth0 \ -netdev user,id=eth0,hostfwd=tcp::2222-:22 \ -drive file=ubuntu-24.10-preinstalled-server-riscv64.img,format=raw,if=virtio
After a couple of seconds you obtain a GRUB screen in which you select the default of Ubuntu. Then after a further approx. 20 seconds of screen output, a little bit to my surprise this worked and I was presented with the login screen to Ubuntu server. The default username is ubuntu and the default password is ubuntu. On login you are immediately prompted to change the password but then you’re in.
So next it’s essentially standard Ubuntu commands to update the system and install r-base
. I also install r-base-dev
to obtain the necessary compilers to build any source packages containing code which needs to be compiled.
sudo apt-get update sudo apt-get upgrade -y sudo apt-get install -y r-base r-base-dev
Then we launch R.
From this point on everything I tried simply worked. I installed data.table from source. Then slightly more obscurely, I tried out a trick from Jeroen Ooms who said that if an R package only contains R code then its binary version will install under any architecture. I have a few R packages in my r-universe that only contain R code, my example installed as expected (note this was built on x86_64 Ubuntu Linux but contains no source code which needs to be compiled).
install.packages('tmsens', repos="https://remlapmot.r-universe.dev/bin/linux/noble-x86_64/4.5/") #> * installing *binary* package ‘tmsens’ ... #> * DONE (tmsens) #> #> The downloaded source packages are in #> ‘/tmp/RtmpYqwF8W/downloaded_packages’ #> > library(tmsens) #> Warning message: #> package ‘tmsens’ was built under R version 4.5.0 #> > help(package="tmsens") #> #> Information on package ‘tmsens’
I admit I haven’t tried many features here but I am really impressed with Ubuntu packages being available for RISC-V.
Once you are finished using R, exit R as usual and to shutdown the Ubuntu server issue the following.
sudo poweroff
Summary
I have shown how to install and run R on Ubuntu Server for RISC-V under QEMU emulation. Thanks to Canonical’s support for RISC-V and the maintainers of the r-base
and related packages the experience of running R on RISC-V is already excellent.
Related