Skip to content

Installing ANTsR on Windows 10 (using WSL2)

dorianps edited this page Aug 23, 2020 · 32 revisions

What is WSL2?

Windows Subsystem for Linux (WSL) is a Linux kernel running on Windows 10. Thus, you can use a full Linux environment while being on Windows. WSL2 was released in May 2020 and is the next generation of WSL with much better performance than WSL1, which was released in 2016.

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 the Home, Pro, or Server editions of Windows but not Windows 10 S.

Get Linux (Ubuntu 20.04)

You can search Microsoft Store for Linux distributions. This Wiki example uses Ubuntu is 20.04 LTS (the most recent stable Ubuntu). Once you install Ubuntu from the store and launch it, you will be asked for a username and password. Then you are good to go.

Make sure you are using WSL2, not WSL1

If you had WSL1 installed, you can convert a Linux installation to WSL2: First open a command line terminal in Windows and check the installed Linux distros wsl -l -v

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

Set the Distribution you want to version 2: wsl --set-version Ubuntu-20.04 2.
Then check again the version shows 2. 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.

Get MobaXterm

This is not required, but if you are on Windows, chances are you need a decent terminal handler with X11 server. MobaXterm has everything you need for free, including the X11 server. I have used it for years and its a great gem. You can use it in commercial environments as well, 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 the system has up to date packages:

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 (if you need it)

RStudio is useful for interactive sessions and graphic visualization of R task. 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 open 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). The following commands worked for me (you can see here more details):

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.

RStudio Server

RStudio Server does not need an X11 server. You can see the RStudio itnerface from a browser in Windows.

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 if you are using it), first install devtools:

install.packages('devtools')

Then install ANTsR. This will install in series ITKR->ANTsRCore->ANTsR and will take a long time:

devtools::install_github('ANTsX/ANTsR')

You can simply try loading ANTsR to make sure is installed library(ANTsR).
Finally, install a couple of packages that will allow you to display brain images using ANTsR:

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

Faster R processing

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

And here are a couple of tests on my MS Surface Pro 5 i7 (Intel CPU): Using OpenBLAS:

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

Using system BLAS:

> 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.