How to Install Ubuntu on WSL 2 in Windows 11

How to Install Ubuntu on WSL 2 in Windows 11

Overview

  • WSL 2 is an official tool that allows the use of Linux operating systems on Windows 10/11 without the need for a virtual machine or dual boot. Microsoft offers various Linux operating systems that run natively on WSL 2 for free in the Windows Store. This enables developers to set up a native Linux-based development environment without any cumbersome process or performance degradation.

Features

  • The WSL 2 architecture provides the best Linux environment performance within the Windows ecosystem because it is based on a lightweight VM with a Linux kernel directly produced by Microsoft, in official collaboration with various Linux vendors to provide WSL distros.
  • Unlike Cygwin, which mimics Linux as closely as possible, it allows the use of binaries exactly the same way in a real Linux shell. (This is especially welcome for developers, as it allows the use of numerous tools that enhance productivity within the powerful Linux ecosystem. Microsoft officially states that WSL 2 was designed for developers.)
  • File system access between Windows and WSL distros is possible. In Windows, it is accessible via \wsl$ and in WSL distros via the /mnt path. (In any case, accessing the opposite file system reduces I/O performance. The best performance is shown when accessing the ext4 file system within the WSL distro. Therefore, in my case, both the IDE and source code are installed within the WSL distro.)

Prerequisites

Installing Ubuntu on WSL

  • Ubuntu is the most popular Linux operating system providing a desktop environment. It is officially available on the Windows Store, and you can click here to install the latest LTS version as an app. (Upon first launch after installation, it asks for a username and password to use.) Alternatively, it can also be installed from the console as follows.
# Install Ubuntu on WSL
$ wsl --install -d ubuntu

# Switch Ubuntu on WSL version to WSL 2
$ wsl --set-version ubuntu 2

# Uninstall Ubuntu on WSL
$ wsl --unregister Ubuntu

Docker Integration

  • Installing Docker Desktop on Windows allows for the free use of Docker in Ubuntu on WSL.
# Install Docker Desktop (Run as Administrator in PowerShell)
$ choco install docker-desktop -y
  • To use Docker within the installed Ubuntu console, the following steps are needed.
# Run Docker Desktop
→ Settings
→ Resources
→ WSL Integration
→ Enable integration with additional distros: Activate [Ubuntu]
→ Click [Apply & Restart]

# Change the default Terminal in IntelliJ IDEA
Settings
→ Tools
→ Terminal
→ Shell path: Enter [ubuntu run]

Running Ubuntu on WSL Console

  • Running Ubuntu on Windows is very straightforward. Just execute the ubuntu command. If Windows Terminal is installed, it automatically recognizes it, so setting it as the default profile is convenient.
# Run console from home directory
$ ubuntu

# Run console from the current directory
$ ubuntu run

# After console switch, set DNS server
$ sudo nano /etc/wsl.conf
[network]
generateResolvConf = false

$ sudo nano /etc/resolv.conf
nameserver 8.8.8.8
nameserver 4.4.4.4

# After console switch, perform the latest update
$ sudo apt-get update
$ sudo apt-get upgrade -y

Running Windows Applications from Ubuntu on WSL Console

  • Conversely, it's also possible to run Windows applications from the WSL 2 console.
# Run Notepad
$ powershell.exe start notepad foobar.txt

# Register as an alias
$ nano ~/.bash_aliases
alias cmd="powershell.exe start"

# Run using the alias
$ cmd notepad foobar.txt

Increasing WSL Memory Size

  • Increasing the memory size can make the development environment faster and more pleasant.
# Shutdown WSL
$ wsl --shutdown

# Change memory size to 24GB
$ notepad "$env:USERPROFILE/.wslconfig"
[wsl2]
memory=24GB

# Restart WSL
$ Get-Service LxssManager | Restart-Service

(Optional) Installing OpenJDK 21

  • Install OpenJDK 21 in the development environment as follows. (For convenience, SDKMAN was used.)
# Install SDKMAN
$ curl -s "https://get.sdkman.io" | bash
$ source "$HOME/.sdkman/bin/sdkman-init.sh"

# Install Amazon Corretto 21 and set as the default JDK
$ sdk i java 21.0.1-amzn
$ sdk default java 21.0.1-amzn

$ sdk current java
Using java version 21.0.1-amzn

# Check installed version
$ java --version
openjdk 21.0.1 2023-10-17 LTS

(Optional) Installing Node.js

  • In the Ubuntu environment, Node.js can be conveniently managed by installing NVM. [Related Link]
# Install NVM
$ cd ~
$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
$ source ~/.bashrc

# Print list of installable Node.js versions
$ nvm list-remote

# Install Node.js 20.11.0 LTS
$ nvm install 20.11.0

$ node --version
v20.11.0

$ npm --version
10.2.4

(Optional) Installing AWS CLI v2

# Install AWS CLI v2
$ cd ~
$ curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
$ unzip awscliv2.zip
$ sudo ./aws/install

$ nano ~/.bash_aliases
alias aws="/usr/local/aws-cli/v2/current/bin/aws"

$ aws --version
aws-cli/2.14.0 Python/3.11.6 Linux/5.15.133.1-microsoft-standard-WSL2 exe/x86_64.ubuntu.22 prompt/off

# Install awslogs
$ sudo apt install python3-pip -y
$ pip install awslogs

# Configure AWS CLI v2
$ aws configure
AWS Access Key ID [None]: 
AWS Secret Access Key [None]: 
Default region name [None]: 
Default output format [None]:

(Optional) Installing Other Useful Utilities

  • Below are examples of useful utilities when using the Ubuntu console. [Related Link]
# Install zsh, jq, httpie, neofetch, ranger, nmap
$ sudo apt-get install zsh jq httpie neofetch ranger nmap -y

# Install tldr
$ npm install -g tldr

# Install autojump
$ cd ~
$ git clone git://github.com/wting/autojump.git
$ cd autojump
$ ./install.py
$ source /home/jsonobject/.autojump/etc/profile.d/autojump.sh

References

Further Reading