Skip to content

Installing ANTsR on Windows 10 (using WSL2)

dorianps edited this page Mar 14, 2021 · 32 revisions

What is WSL2?

Windows Subsystem for Linux (WSL) is a Linux kernel running on Windows 10 which enables a full Linux environment. The first WSL (WSL1) was released in 2016 and runs on an emulated Linux kernel. WSL2 is the next generation of WSL1, runs on a real Linux kernel, and performs much better then WSL1. WSL2 was released in 2020.


Enable WSL2

WSL2 is available from Windows 10 2004 (aka Version 10.0.19041 Build 19041). See the official Microsoft instructions on how to install the WSL2 feature. WSL2 is compatible with Windows Home, Pro, or Server editions but not Windows 10 S.


Install Linux (Ubuntu 20.04)

You can search the Microsoft Store for Linux distributions. This Wiki shows how to run the installations on a fresh Ubuntu is 20.04 LTS (the most recent, stable version of Ubuntu). Once you install Ubuntu from Microsoft Store you can open it. You will be presented with a Linux command line. At first you will be asked to set a username and password, then you will just see the Linux shell prompt.

Make sure you are using WSL2, not WSL1

Check the list of installed Linux distros in Windows with wsl -l -v (run in Windows CMD, not Linux):

  NAME                   STATE           VERSION
* Ubuntu-20.04           Running         1
  docker-desktop         Stopped         2
  docker-desktop-data    Stopped         2

If your Linux is running version 1 (as shown above), you can change it to version 2:
wsl --set-version Ubuntu-20.04 2 and check again wsl -l -v:

  NAME                   STATE           VERSION
* Ubuntu-20.04           Running         2
  docker-desktop         Stopped         2
  docker-desktop-data    Stopped         2

If you see the message WSL 2 requires an update to its kernel component you will need to download the latest WSL2 kernel here. You may want to set WSL2 as default version with wsl --set-default-version 2.
More examples on installing WSL2 here, here, and here.


Get MobaXterm (optional)

This is not required, but if you are on Windows, chances are you need a decent terminal handler with an X11 server. MobaXterm is a complete package with many tools included, and has the X11 server you may need. I use MobaXterm regularly for many years now; it is free for personal use even if you use it in a commercial setting (as long as you install your own copy, no admin should install it for you). You can get MobaXterm at https://mobaxterm.mobatek.net/.


Install R

First, make sure Ubuntu packages are up to date:

sudo apt update
sudo apt upgrade -y

Then, use the these commands to install R and several dependencies:

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/'
sudo apt install -y r-base r-base-core r-recommended r-base-dev gdebi-core build-essential libcurl4-gnutls-dev libxml2-dev libssl-dev

Install RStudio (optional)

RStudio is useful for interactive sessions and graphic visualization of R tasks. If you don't need a graphic interface skip this and continue with installing ANTsR.

Rstudio comes in two flavors: RStudio Desktop and RStudio Server. You can install whichever you like, even both of them.

RStudio Desktop

RStudio Desktop opens a full window of RStudio, but for this to happen you need an X11 server on Windows, and proper settings in Linux to send the signal towards the Windows' X11 server. MobaXterm has an integrated X11, so you don't need anything else (just make sure X11 server in MobaXterm is enabled, and start RStudio from the MobaXterm terminal).
The following commands will install RStudio Desktop in Ubuntu 20.04 (more comprehensive instructions here):

sudo apt install -y libnss3
echo "export DISPLAY=\"\$(/sbin/ip route | awk '/default/ { print \$3 }'):0\"" >> ~/.bashrc
wget https://s3.amazonaws.com/rstudio-ide-build/desktop/bionic/amd64/rstudio-1.3.1075-amd64.deb
sudo apt install -y ./rstudio-1.3.1075-amd64.deb

Now type rstudio in the Linux terminal and you should see the GUI popping up.

RStudio Server

RStudio Server does not need an X11 server, you can see the interface from a browser in Windows. Copy-paste functions work very well with the browser solutions (perhaps better than in RStudio Desktop), but there might be a small lag in the display of plots and graphics.

wget https://rstudio.org/download/latest/stable/server/bionic/rstudio-server-latest-amd64.deb
sudo gdebi rstudio-server-latest-amd64.deb
sudo rstudio-server start

Now you can just open your browser and go to http://localhost:8787/. The username and password are your Linux username and password. More detailed instructions on how to install RStudio Server here.


Install ANTsR

From the R terminal (or RStudio), install devtools first:

install.packages('devtools')

then install ANTsR:

devtools::install_github('ANTsX/ANTsR')

This will install in series ITKR->ANTsRCore->ANTsR and will take a long time. To verify the installation, you can load the package with library(ANTsR).
Finally, install the two packages below if you want to plot brain images in R using the plot function:

install.packages(c('pixmap','misc3d'))

Faster R processing (optional)

R uses BLAS and LAPACK libraries to do math operations. Turns out there are better variants of these libraries than those coming by default in our system. For example, OpenBLAS is freely available and can do math operations much faster. I have not tested real ANTsR pipelines, but the tests below show a lot of speed improvement.

Install OpenBLAS:

sudo apt-get install libopenblas-base

To switch between OpenBLAS and existing system libraries you select among the options that appear with:
sudo update-alternatives --config libblas.so.3-x86_64-linux-gnu

  Selection    Path                                                     Priority   Status
------------------------------------------------------------
* 0            /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3   100       auto mode
  1            /usr/lib/x86_64-linux-gnu/blas/libblas.so.3               10        manual mode
  2            /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3   100       manual mode

Below is a performance test on a MS Surface Pro 5 i7 (Intel CPU)
Using OpenBLAS (option 2 above):

> system.time({ x <- replicate(5e3, rnorm(5e3)); tcrossprod(x) })
   user  system elapsed
 11.183   0.642   4.892

Using system BLAS (option 1 above):

> system.time({ x <- replicate(5e3, rnorm(5e3)); tcrossprod(x) })
   user  system elapsed
 76.573   0.459  77.052

OpenBLAS is 10x-15x faster on my computer. Seems there is also better parallelization going on. More information on BLAS optimization can be found here, here, and here.


What about Freesurfer, FSL, ANTs?

You can check the original Wiki page which has some links. In principle, you should simply follow the instructions given by each software. WSL2 can be thought of as a regular linux machine.