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.
$ wsl --install -d ubuntu
$ wsl --set-version ubuntu 2
$ wsl --unregister Ubuntu
Docker Integration
- Installing
Docker Desktop
on Windows allows for the free use of Docker in Ubuntu on WSL.
$ choco install docker-desktop -y
- To use Docker within the installed Ubuntu console, the following steps are needed.
→ Settings
→ Resources
→ WSL Integration
→ Enable integration with additional distros: Activate [Ubuntu]
→ Click [Apply & Restart]
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.
$ ubuntu
$ ubuntu run
$ sudo nano /etc/wsl.conf
[network]
generateResolvConf = false
$ sudo nano /etc/resolv.conf
nameserver 8.8.8.8
nameserver 4.4.4.4
$ 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.
$ powershell.exe start notepad foobar.txt
$ nano ~/.bash_aliases
alias cmd="powershell.exe start"
$ cmd notepad foobar.txt
Increasing WSL Memory Size
- Increasing the memory size can make the development environment faster and more pleasant.
$ wsl --shutdown
$ notepad "$env:USERPROFILE/.wslconfig"
[wsl2]
memory=24GB
$ Get-Service LxssManager | Restart-Service
(Optional) Installing OpenJDK 21
- Install
OpenJDK 21
in the development environment as follows. (For convenience, SDKMAN
was used.)
$ curl -s "https://get.sdkman.io" | bash
$ source "$HOME/.sdkman/bin/sdkman-init.sh"
$ sdk i java 21.0.1-amzn
$ sdk default java 21.0.1-amzn
$ sdk current java
Using java version 21.0.1-amzn
$ 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]
$ cd ~
$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
$ source ~/.bashrc
$ nvm list-remote
$ nvm install 20.11.0
$ node --version
v20.11.0
$ npm --version
10.2.4
(Optional) Installing 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
$ sudo apt install python3-pip -y
$ pip install awslogs
$ 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]
$ sudo apt-get install zsh jq httpie neofetch ranger nmap -y
$ npm install -g tldr
$ 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