WSL2 for Data Science: Seamless Integration of Ubuntu and Windows 10
If you are the type of person that buys a PC and the first thing they do is partition the hard drive to install the latest Ubuntu, then this article is for you. In the latest version of Windows 10, Microsoft has integrated WSL2 (Windows subsystem for Linux 2) which enables you to have a full installation of Ubuntu running on Windows using Hyper-V virtual machine technology. Note that your computer must support Hyper-V for WSL2 but not for WSL.
After installing WSL2, installing Ubuntu 20.04 LTS is as simple as opening the Microsoft store (yes, Ubuntu is now available on a store as an app) and downloading the latest version. Once installed, you can open the Ubuntu terminal by typing “ubuntu” on the Windows search bar. From the terminal, you are in Linux world: you have git, sudo, ls, cat, apt-get, make, and all those beautiful things that come with Linux. According to Windows:
WSL 2 uses the latest and greatest in virtualization technology to run a Linux kernel inside of a lightweight utility virtual machine (VM). However, WSL 2 is not a traditional VM experience.
From the Windows file explorer you can type
which will take you to your Ubuntu installation. From Ubuntu, type:
and you will be able to access all your Windows files! It is so easy to access and copy files that you can run GIT on Ubuntu and create repositories from you Windows files without having to install GIT on Windows. You could also create a GIT project on Ubuntu, edit your code on Atom (installed in Windows) and running it on your Windows web browser using RStudio server.
Running RStudio server on Ubuntu and Edge
I know the words R, Ubuntu and Edge usually don’t come together in a sentence but let me explain. One of the downsides of WSL2 is that it does not have native support for graphics (you can enable graphics using X-server but that’s different story). On the other hand, RStudio server and Jupyter Notebook run on a web browser so what if we run R and python on Ubuntu, but stream the output on Windows using Edge or Chrome? Sounds great.
First thing would be to install RStudio server in Ubuntu. You want the server version, not the desktop version otherwise you won’t be able to send the output to the web browser. The RStudio team put together a great guide on installation.
Once installed, you can start the server on the Ubuntu terminal:
sudo rstudio-server start
Open your browser and go to http://localhost:8787/ Here you can log in using your Ubuntu credentials. RStudio server runs pretty smooth on Edge and Chrome. Here you can see a 3D scatterplot made with Plotly, which works out-of-the-box on Ubuntu, and now on Windows too!
Something cool about RStudio server is that it automatically saves your session such that whenever you open RStudio you will start from where you left even if you close Ubuntu or turn off your laptop!
Jupyter Notebooks and TensorFlow
In principle, we can do exactly the same thing as we did with RStudio Server and run Jupyter from Ubuntu on Edge (or Chrome).
# Run from Ubuntu WSL2
jupyter lab --no-browser # then access the notebook with the address printed in the terminal
# usually: [<http://localhost:888>](<http://localhost:8889/lab>)8/
However, since Anaconda works pretty well on Windows 10, we can run Python directly on Windows 10. Moreover, we can set up our GPU to work with TensorFlow by leveraging the fact that Nvidia drivers work pretty well on Windows already. Check out my article on how to setup TensorFlow with CUDA on Windows 10 .
Python 3.8 runs pretty well with TensorFlow 2.4 and CUDA 11.0. So far I haven’t got any problems (usually installing python libraries could be a nightmare because of compatibility issues).
This is a very brief description of the setup I am currently using. Being a long-time Ubuntu user, I can’t help but thinking that I should just go ahead and install Linux on a drive partition. However, the Windows 10 is overall good: window management is pretty smooth, the search bar is fast and find what you are looking for most of the times, you can install software such as Adobe, PowerPoint, Ableton Live, Think or Swim, Xbox games etc. And most importantly your laptop simply won’t work as well on Linux because not all of the drivers will be available or optimized. This last part is really important is you have a GPU, HD audio, or some nice — and completely unnecessary — lighting in your keyboard (Razer 15 user here). So while I am still thinking of making a partition for Ubuntu, it seems that you can have your Data Science startup kit (GIT, R, Python, TensorFlow) running on the WSL2 while having the benefits of your laptop running on Windows 10.