Python, Conda, WSL 2 Ubuntu Setup on Windows 10


This article will cover in a nutshell my journey back into Linux for Python development. I was running Python on my Win10, but when Chris Moffitt over at Practical Business Python (excellent site!) wrote about Using WSL to Build a Python Development Environment on Windows – Practical Business Python, I had to give it a crack. So I have to give credit to Chris for his post and please check it out as he goes into much more depth than what I will cover.

This article will be 1 of 3 setup articles I plan to write. This was my first setup and while I was running some deep neural network models, my Surface Pro 3 was grinding to a halt. So I set up a newer Surface Laptop 3 on WSL 1 (WSL 2 not available on the newer Surface machines!) with Debian and pip python management. I found the dual setup cumbersome, so I moved over to a Mac for my third setup.

Some things may be dated as I performed the setup some months ago. Be sure to check the docs for the latest commands.

Install Windows Subsystem for Linux

I will be closely following Microsoft’s Guide and for screenshots and in-depth explanations refer to Chris’s article.

WSL 1 or 2

Refer to the docs for a comparison of WSL 2 and WSL 1. They claim WSL 2 has “increase file system performance and support full system call compatibility.”

WSL should be stock standard in 2020, but at the time of writing, I still could not upgrade to the 2020 version of Windows 10 on my new Surface laptop 3. So I will copy and paste the criteria for info. Hopefully, MS will have rolled it out and we can ignore this blurb.

To update to WSL 2, you must meet the following criteria:

– Running Windows 10, updated to version 1903 or higher, Build 18362 or higher.

– Check your Windows version by selecting the Windows logo key + R, type winver, select OK. (Or enter the ver command in Windows Command Prompt). Please update to the latest Windows version if your build is lower than 18361. Get Windows Update Assistant.

For this setup I went with WSL 2. Since MS hasn’t yet released WSL 2 on stock Windows 10, you must sign over your details to Microsoft to enable the Windows Insider Programme. Once you agree to send them all your data, they will invite you into the program and under Settings > Windows Insider Programme you have the option to pick your channel, which will install the required version of Windows 10 to enable WSL 2.

Install your Linux Distribution of Choice

Open the Microsoft Store and choose your favourite Linux distribution.

In the Microsoft store, a search for WSL will bring up common distros. I choose Ubuntu 18.04 LTS at the time and later upgraded to version 20.04 LTS once it was available.

For my Surface Laptop 3 setup, I tried Debian (78 MB), as it is more minimalist compared with Ubuntu (444 MB).

Initializing a newly installed distro

Click the “launch” button in the Microsoft Store app, or launch from the distro from the Start menu.

Set up a new Linux user account and password.

You can choose any username and password you wish – they have no bearing on your Windows username.

Update and upgrade your distro’s packages with:

   sudo apt update && sudo apt upgrade

Windows does not automatically update or upgrade your Linux distro(s): This is a task that the Linux users prefer to control themselves.

Enable the optional components

Enable both Windows Subsystem for Linux, and Virtual Machine Platform optional features before installing WSL 2.

Open PowerShell as Administrator and run:

dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart

dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

Please restart your machine to finish installing both components.

Confirm the features were enabled by clicking the Start or Windows icon and typing in Turn Windows features on or off and ensure the following are checked:

☑︎ Virtual Machine Platform
☑︎ Windows Subsystem for Linux

Set a distro to WSL 2

Run the following to get your <Distro> name in PowerShell:

wsl --list --verbose or wsl -l -v

Then set:

wsl --set-version <Distro> 2

Here is my output after running the commands in PowerShell as an Administrator:

PS C:\WINDOWS\system32> wsl -l -v

  NAME            STATE           VERSION
* Ubuntu-18.04    Stopped         1

PS C:\WINDOWS\system32> wsl --set-version Ubuntu-18.04 2

Conversion in progress, this may take a few minutes...
WSL 2 requires an update to its kernel component. For information please visit https://aka.ms/wsl2kernel

PS C:\WINDOWS\system32>

From the first command it confirms that my Ubuntu-18.04 is on WSL 1. When I try to set it to WSL 2, I get the notice to update its kernel component. Go to https://aka.ms/wsl2kernel to download and install.

Note it takes over 1 minute with no status or progress bars. So don’t quit early cause you think nothing is happening (like I did)

Then back to PowerShell and try again:

PS C:\WINDOWS\system32> wsl --set-version Ubuntu-18.04 2

Conversion in progress, this may take a few minutes...
For information on key differences with WSL 2 please visit https://aka.ms/wsl2
Conversion complete.                                                                               
PS C:\WINDOWS\system32>         

💯Success! Set to WSL 2.

If you want to make WSL 2 your default architecture or set any new distribution installed to WSL 2, run the following command:

wsl --set-default-version 2

Now Linux should run on your Windows machine. Next section will cover some Extras worth considering.

Extras

The following are some notes I took while going through Scott Hanselman’s video on converting a diehard machead to WSL.

Windows terminal (preview)

Install the Windows Terminal Preview by Microsoft Corporation in the Windows store.

Some key bindings set in the defaults.json for reference. Refer to the docs for more details.

// In defaults.json
{ "command": "closePane", "keys": "ctrl+shift+w" }
{ "command": { "action": "splitPane", "split": "horizontal"}, "keys": "alt+shift+-" },
{ "command": { "action": "splitPane", "split": "vertical"}, "keys": "alt+shift+plus" }

The Windows terminal will come setup with the Windows PowerShell, cmd and Azure Clould Shell. To add your WSL distro to the list, open the settings and copy one of the other shells and amend to suit. Just remember to make the guid value unique by changing a few letters around.

To replace PowerShell with Ubuntu at terminal startup, update the settings.json file by copying the Ubuntu guid value and pasting it into the defaultProfile attribute towards the top of the file.

Note: If you like to set a tooltip in the tabTitle attribute, shells like bash may ignore it. Hence, just update the name attribute to suit. Usage: "name": "Ubuntu-18.04" changed to "name": "Ubuntu-20.04" after updating. Tried the tabTitle without success.

PowerToys

I had previously used PowerToys many years ago to resize images and was surprised to see that it was still alive. Microsoft has made PowerToys open-source and available on GitHub

The Shortcut Guide and File Explorer (Preview Panes) for SVG and Markdown previews are worth having a look into.

WSL Python Installation

Python package management is a bit of a basket case as outline in this article Overview of python dependency management tools.

For this setup I went with Conda, but will probably go the Unix setup of: pyenv + pip + venv + pip-tools discussed in the article.

Conda

Download the installer. Move from Windows downloads folder to \\wsl$\Ubuntu-18.04\home\bradley\downloads\

Change to the cd ~/bradley/downloads/ directory in the terminal and verify hashes with:

sha256sum Miniconda3-latest-Linux-x86_64.sh

Run the following and accept the defaults:

bash Miniconda3-latest-Linux-x86_64.sh

Test the installed with:

conda list

Run an update:

conda update conda

Add an environment:

conda create --name work
conda activate work
conda info --env
conda install -c anaconda python
conda list

See the docs on managing Conda environments.

To activate this environment by default, add source activate work to .bashrc.

vim ~/.bashrc
# >>> conda initialize >>>
...
source activate work
# <<< conda initialize <<<

Jupyter Notebooks

Install Jupyter into the newly created work environment and then run with the --no-browser flag.

$ conda install -c anaconda jupyter
$ jupyter notebook --no-browser

Add Virtual Environment to Jupyter Notebook

I installed my work environment per this site with the following codes:

pip install --user ipykernel
python -m ipykernel install --user --name=work
>>> Installed kernelspec myenv in /home/user/.local/share/jupyter/kernels/work

I created a JupyterLab configuration file and installed nb_conda_kernels per this site.

jupyter notebook --generate-config
conda install nb_conda_kernels

Updated the following in the created jupyter_notebook_config.py

c.NotebookApp.notebook_dir = 'C:/Users/<user>/<folder>'

This created the Python [conda env:root] and Python [conda env:work]* environments as shown in the tutorials. I don’t know why the * is showing after the work and discovered that when the computer sleeps it spins up the default Python kernel??

Launch Windows Apps within WSL

Open windows explorer into a WSL folder by entering explorer.exe . into the shell.

explorer.exe . 

Windows Package Manager

Linux has aptAdvanced Package Manager. Mac has brewThe Missing Package Manager for macOS (or Linux). Windows has chocoChocolatey Software The Sane Way to Manage Software on Windows.

Let’s setup Python on the Windows side using Chocolately.

Windows Python Installation

I followed this article How To Install Python 3 and Set Up a Local Programming Environment on Windows 10 and Chocolately’s install docs.

Before starting, it might be a good idea to open up the Command Reference docs in a new tab.

Start PowerShell and “Run as Administrator” and run the following commands:

# Steps 1 & 2 - Go to  https://chocolatey.org/install and copy script.
# Run powershell as administrator and paste these commands

cd ~

Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))

choco upgrade chocolatey

# Step 3 - Installing the Text Editor nano (Optional)
choco install -y nano

# Step 4 - Installing Python 3
choco install -y python3
python -V
>>> Python 3.8.5
python -m pip install --upgrade pip

# Step 5 - Setting Up a Virtual Environment
mkdir python-projects\py38
cd python-projects\py38
python -m venv .venv
ls .venv
>>> Outputs 3 directories and pyvenv.cfg

# Step 6 - Set ExecutionPolicy to allow scripts to run
Set-ExecutionPolicy -Scope CurrentUser
>> RemoteSigned
cd ~
.\python-projects\py38\.venv\Scripts\activate

Now set the virtual environment within the Windows Environment Variables: (Stack overflow)

Windows Start button > type: View advanced system settings > Advanced tab > Environment Variables

Edit the Path System variable by putting the virtual environment paths first as the system will look at the start and move along until it finds an active python environment to use.

Set C:\Users\berno\python-projects\py38\.venv\Scripts\ then C:\Users\berno\python-projects\py38\.venv\Scripts\python.exe before the system Python.

Finished

This was my first setup with WSL and Python. As mentioned at the top, I have changed things as I moved from one device to another, but I hope this was generic enough to get anyone interested up and coding quickly.

If you have any queries or comments, please drop them below. You are a legend if you hung in until this point!