Configuring Jupyter Notebook in Windows Subsystem Linux (WSL2) | by Cristian Saavedra Desmoineaux | Towards Data Science

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:
Install and configure a WSL2Install and configure Windows TerminalInstall Miniconda and common librariesLaunch Jupyter Lab/NotebookInstall GUI and Remote Desktop to WSL2 to lunch GUI required like Spyder or Anaconda NavigatorSome helpful resources
Install and configure a WSL2
Install and configure Windows Terminal
Install Miniconda and common libraries
Launch Jupyter Lab/Notebook
Install GUI and Remote Desktop to WSL2 to lunch GUI required like Spyder or Anaconda Navigator
Some helpful resources
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:
Open a new terminal to your Ubuntu and run the following commands:
sudo apt-get updatesudo apt-get upgradesudo apt autoremove
I am going to use Ubuntu 20.04.1 LTS to check the version using the following:
lsb_release -auname -r
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.shchmod +x Miniconda3-latest-Linux-x86_64.shsh Miniconda3-latest-Linux-x86_64.shrm 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/
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:
pythonconda infoconda info --envs
Next, we are going to do some updates:
conda update condaconda update --all
Next, we will do some basics installations to get Pandas, Jupyter Lab/Notebook, 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_nbextensionsconda update condaconda update --all
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
Open a new terminal of Ubuntu with the command:
jupyter lab --no-browser
And copy and paste the full URL including the token
Open a new terminal of Ubuntu with the command:
jupyter notebook --no-browser
And copy and paste the full URL, including the token.
If you lost network connectivity and for example, when do 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 doing the following statements:
sudo rm /etc/resolv.confsudo 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
Some versions fail to connect using localhost. There is an update 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
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 setqt.qpa.screen: QXcbConnection: Could not connect to displayCould 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 the conflict with local Windows.
Run the next commands and select lightdm as X display manager:
sudo apt update && sudo apt -y upgradesudo apt-get purge xrdpsudo apt-get install -y xfce4 xfce4-goodiessudo apt-get install xrdpsudo cp /etc/xrdp/xrdp.ini /etc/xrdp/xrdp.ini.baksudo sed -i 's/3389/3390/g' /etc/xrdp/xrdp.inisudo sed -i 's/max_bpp=32/#max_bpp=32\nmax_bpp=128/g' /etc/xrdp/xrdp.inisudo sed -i 's/xserverbpp=24/#xserverbpp=24\nxserverbpp=128/g' /etc/xrdp/xrdp.iniecho xfce4-session > ~/.xsessionsudo systemctl enable dbussudo /etc/init.d/dbus startsudo /etc/init.d/xrdp startsudo /etc/init.d/xrdp status
Use Remote Desktop Connection with your <IP Address>:3388
medium.com
pbpython.com
code.visualstudio.com
code.visualstudio.com
code.visualstudio.com
docs.microsoft.com
devblogs.microsoft.com
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.