Skip to content

Instantly share code, notes, and snippets.

@ScriptAutomate
Last active January 18, 2024 11:56
Show Gist options
  • Star 27 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • Save ScriptAutomate/f94cd44dacd0f420fae65414e717212d to your computer and use it in GitHub Desktop.
Save ScriptAutomate/f94cd44dacd0f420fae65414e717212d to your computer and use it in GitHub Desktop.
Enable WSL and Install Ubuntu 22.04 (or 20.04)
<#
- BIOS of host machine also needs to be configured to allow hardware virtualization
- Windows 10 Pro or otherwise is needed; Windows 10 Home Edition CANNOT get WSL
- This gist WSLv2, but can use WSLv1 instead. I needed v1 as I run Windows 10 in a VM in Virtualbox.
- WSLv2 has been giving me problems in Virtualbox 6.1, but WSLv1 works properly.
- vbox has issues with the GUI settings when it comes to nested virtualization on certain systems,
so run the following if needing to give a VM this enabled setting:
VBoxManage modifyvm <vm-name> --nested-hw-virt on
#>
## IN AN ELEVATED SHELL
## Right-click PowerShell -> Run As Administrator
# Enable Needed Virtualization
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V -All
# Download Ubuntu 20.04 Focal Fosa
# Uncomment if wanting 20.04 instead of latest
# Invoke-WebRequest -Uri https://cloud-images.ubuntu.com/releases/focal/release/ubuntu-20.04-server-cloudimg-amd64-wsl.rootfs.tar.gz -OutFile $ENV:HOMEDRIVE$ENV:HOMEPATH\Downloads\ubuntu-20.04-focal-wsl.tar.gz -UseBasicParsing
# Download Ubuntu 22.04 Jammy Jellyfish
Invoke-WebRequest -Uri https://cloud-images.ubuntu.com/wsl/jammy/current/ubuntu-jammy-wsl-amd64-wsl.rootfs.tar.gz -OutFile $ENV:HOMEDRIVE$ENV:HOMEPATH\Downloads\ubuntu-22.04-jammy-wsl.tar.gz -UseBasicParsing
# Setup reserved directory path for WSL VM
# mkdir c:\UbuntuFocal # If doing Focal
mkdir c:\UbuntuJammy # If doing Jammy
# Configure WSL for incoming VM
# For more information about WSL: https://docs.microsoft.com/en-us/windows/wsl/about
wsl --set-default-version 2 # Change to '1' if not able to support 2
# Import into WSL
# wsl.exe --import UbuntuFocal C:\UbuntuFocal $ENV:HOMEDRIVE$ENV:HOMEPATH\Downloads\ubuntu-20.04-focal-wsl.tar.gz
wsl.exe --import UbuntuJammy C:\UbuntuJammy $ENV:HOMEDRIVE$ENV:HOMEPATH\Downloads\ubuntu-22.04-jammy-wsl.tar.gz
wsl # Drops straight into default VM. Will be UbuntuFocal is WSL alternate VM didn't already exist
## BONUS: Docker Desktop
# Checkout the Install-ChocoStarterPackages gist for setting up choco (and other things):
# - https://gist.github.com/ScriptAutomate/02e0cf33786f869740ee963ed6a913c1
# Once chocolatey / choco is installed, and WSL is already configured with Ubuntu,
# run the following command from PowerShell, and not from within WSL:
# choco install docker-desktop -y
@ScriptAutomate
Copy link
Author

ScriptAutomate commented Aug 21, 2020

For a quick bootstrap when it comes to Python development (and being able to contribute to projects that use Sphinx for docs, which is Python based), the following can be done within the WSL instance. It also adds bash-git-prompt to their shell profile.

# Update all the things
sudo apt update && sudo apt upgrade -y && sudo apt autoremove -y && sudo apt autoclean

# Python and Sphinx-related prereqs
sudo apt install make python3-venv python3-dev -y

# Bash git prompt
git clone https://github.com/magicmonty/bash-git-prompt.git ~/.bash-git-prompt --depth=1
## Adding bash git prompt to user bash profile
echo '
# Adds the git branch status info to the shell prompt
if [ -f "$HOME/.bash-git-prompt/gitprompt.sh" ]; then
    GIT_PROMPT_ONLY_IN_REPO=1
    source $HOME/.bash-git-prompt/gitprompt.sh
fi' >> ~/.bashrc

Things like git and vim are already installed on base Ubuntu 20.04 WSL instances.

Projects like salt require pre-commit and nox, which can be setup via pipx:

sudo apt update
sudo apt install -y pipx

echo '
# Adding path to access pipx and misc packages
export PATH="/root/.local/bin:$PATH"
' >> ~/.bashrc

pipx install pre-commit
pipx install nox

pipx installs packages into their own, isolated virtualenvs using the system Python 3, and then has them accessible in your PATH. pyenv is also highly recommended, and may require additional packages as dependencies (which may be needed when working with other Python-based projects):

sudo apt update
sudo apt install -y make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev xz-utils tk-dev libxml2-dev libffi-dev liblzma-dev python-is-python3 libxmlsec1-dev libcurl4-openssl-dev

If wanting to learn more about Python virtualenvs, refer to the RealPython article Python Virtual Environments: A Primer

@xeacott
Copy link

xeacott commented Feb 3, 2021

libcurl4-openssl-dev libssl-dev Potentially adding to your list, sir!

@ScriptAutomate
Copy link
Author

Something else that may be useful for folks installing WSL on their Windows systems:

@andrestoga
Copy link

andrestoga commented Nov 3, 2022

PS C:\Users\torresan\Downloads> Invoke-WebRequest -Uri https://cloud-images.ubuntu.com/releases/jammy/release/ubuntu-22.04-server-cloudimg-amd64-wsl.rootfs.tar.gz -OutFile $ENV:HOMEDRIVE$ENV:HOMEPATH\Downloads\ubuntu-22.04-jammy-wsl.tar.gz -UseBasicParsing
Invoke-WebRequest :
404 Not Found
Not Found
The requested URL was not found on this server.
Apache/2.4.29 (Ubuntu) Server at cloud-images.ubuntu.com Port 443
At line:1 char:1
+ Invoke-WebRequest -Uri https://cloud-images.ubuntu.com/releases/jammy ...
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebExc
   eption
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

Guess they removed the image...

@ScriptAutomate
Copy link
Author

ScriptAutomate commented Nov 8, 2022

PS C:\Users\torresan\Downloads> Invoke-WebRequest -Uri https://cloud-images.ubuntu.com/releases/jammy/release/ubuntu-22.04-server-cloudimg-amd64-wsl.rootfs.tar.gz -OutFile $ENV:HOMEDRIVE$ENV:HOMEPATH\Downloads\ubuntu-22.04-jammy-wsl.tar.gz -UseBasicParsing
Invoke-WebRequest :
404 Not Found
Not Found
The requested URL was not found on this server.
Apache/2.4.29 (Ubuntu) Server at cloud-images.ubuntu.com Port 443
At line:1 char:1
+ Invoke-WebRequest -Uri https://cloud-images.ubuntu.com/releases/jammy ...
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebExc
   eption
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

Guess they removed the image...

Whoa! It does look like it is removed. They look like they don't have any *wsl* files listed for 22.04.

They do still have 20.04:

Invoke-WebRequest -Uri https://cloud-images.ubuntu.com/releases/focal/release/ubuntu-20.04-server-cloudimg-amd64-wsl.rootfs.tar.gz -OutFile $ENV:HOMEDRIVE$ENV:HOMEPATH\Downloads\ubuntu-20.04-focal-wsl.tar.gz -UseBasicParsing

I'm not sure why Canonical has done that. They seem to have a dedicated landing page for WSL, but all the directions they provide (just like in places like the MSFT docs) are for how to manually do so via the GUI:

I'll ask around and see what I can figure out. May to be manually done in the meantime by following the above Ubuntu documentation of the Microsoft Docs on WSL

@ScriptAutomate
Copy link
Author

ScriptAutomate commented Nov 8, 2022

@andrestoga Jammy and newer WSL images have now been moved here:

I've updated the gist to work with latest Jammy location!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment