The world’s leading publication for data science, AI, and ML professionals.

Configuring Jupyter Notebook in Windows Subsystem Linux (WSL2)

I am going to explain how to configure Windows 10 and Miniconda to work with Notebooks using WSL2

We are going to see how to:

  1. Install and configure a WSL2
  2. Install and configure Windows Terminal
  3. Install Miniconda and common libraries
  4. Launch Jupyter Lab/Notebook
  5. Install GUI and Remote Desktop to WSL2 to lunch GUI required like Spyder or Anaconda Navigator
  6. Thanks
  7. Some helpful resources

First, install WSL2

Windows Sub System Linux (WSL2) was available in Windows 10 version 2004 in May 2020. If you don’t have It, then install a Ubuntu distribution following the instructions in https://docs.microsoft.com/en-us/windows/wsl/install-win10

I recommend installing Windows Terminal from the Microsoft Store instead of using the default Ubuntu terminal because that allows you to have multiple terminals in the same window.

To edit Linux files is also more comfortable with Sublime Text or Notepad++ from Windows Explorer using the path: wsl$

You can browse and edit Ubuntu files from your Window:

And change the default Profile to open to Ubuntu terminal opening Settings file and changing the value of defaultProfile with the Ubuntu guide:

settings.json opened with Visual Studio Code
settings.json opened with Visual Studio Code

Update your Linux

Open a new terminal to your Ubuntu and run the following commands:

sudo apt-get update
sudo apt-get upgrade
sudo apt autoremove

I am going to use Ubuntu 20.04.1 LTS to check the version using the following:

lsb_release -a
uname -r

Install Miniconda

It’s better to use Miniconda instead of Anaconda. The latter contains many libraries that you wouldn’t usually use, translating into slower distribution updates and significantly more required disk space.

My friend Ted Petrou wrote a detailed article in https://medium.com/dunder-data/anaconda-is-bloated-set-up-a-lean-robust-data-science-environment-with-miniconda-and-conda-forge-b48e1ac11646

Download the last file from https://repo.anaconda.com/miniconda/ and follow the instructions:

cd ~
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
chmod +x Miniconda3-latest-Linux-x86_64.sh
sh Miniconda3-latest-Linux-x86_64.sh
rm Miniconda3-latest-Linux-x86_64.sh

If you prefer Anaconda, the procedure is similar, and you need to use the last file from https://repo.anaconda.com/archive/

Press ENTER key and read the full license
Press ENTER key and read the full license
Select the location to install, press ENTER to default
Select the location to install, press ENTER to default
I recommend "yes" to set the active environment in your Ubuntu user
I recommend "yes" to set the active environment in your Ubuntu user

Next time you open a terminal, you will notice a (base) text before the user@server prompt, and that means using the environment base as active in conda:

python
conda info
conda info --envs

Next, we are going to do some updates:

conda update conda
conda update --all

Install libraries & tools

Next, we will do some basics installations to get Pandas, Jupyter Lab/Notebook, and Machine Learning:

conda install pandas scikit-learn matplotlib jupyter jupyterlab sqlalchemy seaborn pip git

Then install Jupyter extensions and refresh any update:

conda install -c conda-forge jupyter_contrib_nbextensions
conda update conda
conda update --all

Launching Jupyter Lab and Notebook

I prefer Jupyter Lab to Notebook because it gives you more flexibility to open multiple windows under the same tab browser, allowing you to open multiple files besides a command prompt. And to choose to avoid the message error, run each command with the no-browser parameter

Jupyter Lab

Open a new terminal of Ubuntu with the command:

jupyter lab --no-browser

And copy and paste the full URL, including the token

Jupyter Notebook

Open a new terminal of Ubuntu with the command:

Jupyter Notebook --no-browser

And copy and paste the full URL, including the token.

Problems with network connectivity

If you lost network connectivity and, for example, when doing a ping to Google show you a timeout error, then maybe is related to a change of the servername in the resolv.conf file.

ping google.com

To fix it, you need to remove the file to break the link to the run folder and also create a wsl.conf file for not generate the resolv.conf file again making the following statements:

sudo rm /etc/resolv.conf
sudo bash -c 'echo "nameserver 8.8.8.8" > /etc/resolv.conf'
sudo bash -c 'echo "[network]" > /etc/wsl.conf'
sudo bash -c 'echo "generateResolvConf = false" >> /etc/wsl.conf'
sudo chattr +i /etc/resolv.conf

Problems resolving localhost

Some versions fail to connect using localhost. An update is coming to Windows to fix this problem until you can connect using the IP Address and change the localhost. To know your IP address, you can use the number previous to the slash with the command:

ip addr | grep eth0 | grep inet

Install Graphic Interface and Remote Desktop

The WSL2 does not include a GUI, and any program that requires a user interface will give a similar error like this:

QStandardPaths: XDG_RUNTIME_DIR not set
qt.qpa.screen: QXcbConnection: Could not connect to display
Could not connect to any X display.

The next step is optional if you want to run some graphics interfaces like anaconda-navigator or spyder IDE. Install using the command:

conda install spyder anaconda-navigator

If you launch the program by the terminal is going to fail like this:

Follow the next steps to install the XFCE4 lightdm and Remote Desktop (xrdp) in the port 3390. I changed the port to avoid conflict with local Windows.

Run the next commands and select lightdm as X display manager:

sudo apt update && sudo apt -y upgrade
sudo apt-get purge xrdp
sudo apt-get install -y xfce4 xfce4-goodies
sudo apt-get install xrdp
sudo cp /etc/xrdp/xrdp.ini /etc/xrdp/xrdp.ini.bak
sudo sed -i 's/3389/3390/g' /etc/xrdp/xrdp.ini
sudo sed -i 's/max_bpp=32/#max_bpp=32nmax_bpp=128/g' /etc/xrdp/xrdp.ini
sudo sed -i 's/xserverbpp=24/#xserverbpp=24nxserverbpp=128/g' /etc/xrdp/xrdp.ini
echo xfce4-session > ~/.xsession
sudo systemctl enable dbus
sudo /etc/init.d/dbus start
sudo /etc/init.d/xrdp start
sudo /etc/init.d/xrdp status
Select lightdm
Select lightdm

Use Remote Desktop Connection with your :3388

Open a terminal to run spyder IDE
Open a terminal to run spyder IDE

Thanks

To finish, I am grateful for Ted Petrou and Scott Boston, good and highly knowledgeable people who gave me the guidelines and helped me with Python Pandas when I was beginning, and my brother Michael Saavedra who inspired this post.

Happy Viz!


Helpful resources

Anaconda is bloated – Set up a lean, robust data science environment with Miniconda and Conda-Forge

Using WSL to Build a Python Development Environment on Windows

Developing in the Windows Subsystem for Linux with Visual Studio Code

Work in Windows Subsystem for Linux with Visual Studio Code

Working with Jupyter Notebooks in Visual Studio Code

An overview of the Windows Subsystem for Linux

Craig Loewen, Author at Windows Command Line


Related Articles