ホーム AI・IT [AI Basics] What is WSL2? Creating the same Linux environment on Windows and Mac

[AI Basics] What is WSL2? Creating the same Linux environment on Windows and Mac

雨宮 千夏 雨宮 千夏 • 更新: • 公開: • 読了約 29 分
目次

Before the Docker series: Why is a Linux environment necessary?

nc3b427d9dd39

Before starting the Docker series, I need to talk about something super important.

Developers can access the power of both Windows and Linux simultaneously on a Windows machine.

Why is Linux necessary? The answer is simple.

99% of production servers are Linux.

  • AWS EC2 → Linux

  • Google Cloud Compute Engine → Linux

  • Azure VM → Linux (there is Windows, but it's the minority)

  • Shared hosting → Linux

  • Docker → Linux

  • Kubernetes → Linux

In other words, if you want to match your development environment with your production environment, you should develop on Linux.

But there is a problem:

  • Developers using Windows → The OS is different

  • Developers using Mac → It's UNIX-based, but strictly speaking, it's not Linux

The solution to this problem is WSL2 (on the Windows side) and the corresponding approach for Mac.

What is WSL2? The magic of running Linux on Windows

The Windows Subsystem for Linux (WSL) lets developers install a Linux distribution (such as Ubuntu, OpenSUSE, Kali, Debian, Arch Linux, etc.) and use Linux applications, utilities, and Bash command-line tools directly on Windows, unmodified, without the overhead of a traditional virtual machine or dual-boot setup.

To put it simply:

WSL2 = Real Linux running inside Windows. And it's super lightweight.

WSL1 vs WSL2: What's the difference?

WSL 1 operated as a compatibility layer that emulated Linux system calls, whereas WSL 2 introduced an actual Linux kernel.

WSL1 (Legacy):

  • Translates Linux system calls to run on Windows

  • Compatibility issues

  • Slow

  • Released in 2016

WSL2 (Current):

  • Equipped with a real Linux kernel

  • Full Linux compatibility

  • File access up to 20x faster

  • Runs on a lightweight virtual machine (Hyper-V)

  • Released in 2020

Conclusion: WSL2 is the only choice. Forget WSL1.

How WSL2 works
Windows 11/10
  └─ Hyper-V(軽量仮想マシン)
      └─ 本物のLinuxカーネル
          └─ Ubuntuなどのディストリビューション
              └─ あなたのアプリ

It's a virtual machine, but unlike traditional VMs:

  • Super fast startup (a few seconds)

  • Dynamic memory usage (uses only what is needed)

  • Easy file sharing between Windows and Linux

  • Network sharing between Windows and Linux

[For Windows Users] WSL2 Installation Steps

Prerequisites

You must be running Windows 10 version 2004 or higher (build 19041 or higher) or Windows 11.

How to check:

  1. Win + R → type winver → Enter

  2. Check the version number

If it is an older version, update to the latest via Windows Update.

Step 1: WSL2 Installation (Super Easy)

Open PowerShell with administrator privileges:

  1. Start menu

  2. Type "PowerShell"

  3. Right-click → "Run as administrator"

Complete in just one line:

wsl --install

This command enables all features necessary to run WSL and installs a Linux distribution (Ubuntu).

When executed:

  1. WSL2 features are enabled

  2. Virtual Machine Platform is enabled

  3. Ubuntu is installed

  4. Restart is required

Restart your computer.

Step 2: Initial Setup

After restarting, launch "Ubuntu" from the Start menu.

On first launch:

Installing, this may take a few minutes...
Please create a default UNIX user account. The username does not need to match your Windows username.
For more information visit: https://aka.ms/wslusers
Enter new UNIX username: yourname
New password: 
Retype new password:

Set your username and password.

Note: Nothing will be displayed on the screen when entering your password. This is normal behavior. Type it and press Enter.

Once complete:

yourname@DESKTOP-XXXXX:~$

Congratulations, Linux is running.

Step 3: Version Check

In a separate PowerShell window:

# インストール済みディストリビューション確認
wsl --list --verbose
出力例
  NAME      STATE           VERSION
* Ubuntu    Running         2

If VERSION is 2, you are good to go.

Step 4: Update Ubuntu to the latest version

Inside Ubuntu:

# パッケージリスト更新
sudo apt update
パッケージアップグレード
sudo apt upgrade -y

Now your latest Ubuntu environment is ready.

Frequently used WSL2 commands

# WSL2起動(デフォルトディストリビューション)
wsl
特定のディストリビューション起動
wsl -d Ubuntu
インストール済み一覧
wsl --list --verbose
WSL2シャットダウン
wsl --shutdown
特定のディストリビューション停止
wsl --terminate Ubuntu
ディストリビューション削除
wsl --unregister Ubuntu
他のディストリビューションをインストール
wsl --install -d Debian
wsl --install -d Kali-Linux

File sharing with Windows

This is extremely convenient.

Accessing Linux files from Windows

In the File Explorer address bar, type:

\\wsl$\Ubuntu\home\yourname

You can see your Linux home directory.

Alternatively, "Linux" appears in the File Explorer sidebar. Just click it.

Accessing Windows files from Linux

Inside Ubuntu:

# Cドライブ
cd /mnt/c/
ユーザーフォルダ
cd /mnt/c/Users/YourWindowsUsername/
デスクトップ
cd /mnt/c/Users/YourWindowsUsername/Desktop/

All Windows drives are mounted under /mnt/.

Integration of VS Code and WSL2

This is seriously convenient.

Install extension

Search for "WSL" in VS Code → Install "Remote - WSL".

Launch VS Code within WSL2

Inside Ubuntu:

cd ~/myproject
code .

VS Code launches in WSL2 mode. The editor is Windows, the execution environment is Linux. Perfect.

If "WSL: Ubuntu" is displayed in the bottom left, you have succeeded.

[For Mac users] Creating an environment equivalent to WSL2

Mac users cannot use WSL2. But that is okay.

Current state of Mac: Already a Unix environment

Mac is a Unix-based OS (BSD-based). If you open the terminal:

# Mac標準でUnixコマンドが使える
ls
pwd
grep
awk

However, strictly speaking, it is not Linux.

Mac (BSD-based) vs Linux:

  • Package managers are different (Homebrew and apt)

  • Some command options are different

  • System libraries are different

  • The kernels are different

It's sufficient as a development environment, but if you want it to match your production server exactly, you need Linux.

Option 1: Use Docker (Recommended)

This is the best solution.

If you use Docker, Linux containers will run on your Mac.

# Ubuntu 22.04コンテナ起動
docker run -it ubuntu:22.04 /bin/bash
これでLinux環境
root@abc123:/# apt update
root@abc123:/# apt install python3

I will explain this in detail in the Docker series.

Option 2: Lima (Lightweight VM)

Lighter than Docker and provides an experience close to WSL2.

Installation:

brew install lima

Starting Ubuntu:

# デフォルトVM作成・起動
limactl start
VM内に入る
lima
これでLinux環境
yourname@lima-default:~$

Lima features:

  • Runs a Linux kernel

  • Easy file sharing

  • Supports Docker and Kubernetes

  • Lighter than WSL2

Option 3: Multipass (by Canonical)

Created by the developers of Ubuntu.

Installation:

brew install --cask multipass

Starting Ubuntu:

# インスタンス作成
multipass launch --name dev
接続
multipass shell dev
これでUbuntu環境
ubuntu@dev:~$
Option 4: UTM (for M1/M2 Macs)

For Apple Silicon (M1/M2/M3), UTM is lightweight.

Install "UTM" from the App Store. You can create VMs with a GUI.

Which one should I use?

Use Case Recommendation Planning to use Docker Docker is the only choice Lightweight development environment Lima Prefer official Ubuntu Multipass Want to manage with GUI UTM Mac terminal is enough Use it as is

My recommendation: Lima.

Reasons:

  • Lightweight

  • Close to WSL2

  • Runs Docker too

  • One-shot install with Homebrew

Creating the same environment on Windows (WSL2) and Mac

This is the goal.

Scenario: Team development
  • Person A: Windows + WSL2 (Ubuntu 22.04)

  • Person B: Mac + Lima (Ubuntu 22.04)

  • Production server: AWS EC2 (Ubuntu 22.04)

Everyone develops on Ubuntu 22.04.

Steps for environment unification

1. Use the same Ubuntu version

Windows (WSL2):

wsl --install -d Ubuntu-22.04

Mac (Lima):

# lima.yaml作成
limactl start --name=dev template://ubuntu-22.04
接続
lima dev

2. Install the same packages

Create setup.sh:

#!/bin/bash
setup.sh - 環境セットアップスクリプト
システム更新
sudo apt update
sudo apt upgrade -y
基本ツール
sudo apt install -y \
    build-essential \
    git \
    curl \
    wget \
    vim
Python 3.11
sudo apt install -y python3.11 python3.11-venv python3-pip
Node.js
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt install -y nodejs
Docker
curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh
sudo usermod -aG docker $USER

echo "セットアップ完了!"

How to use:

# Windows(WSL2)でも、Mac(Lima)でも同じ
bash setup.sh

With this, everyone has the same environment.

3. Complete unification with Docker (The best way)

If you use Docker, you don't even need to worry about the OS.

# Dockerfile
FROM ubuntu:22.04

RUN apt update && apt install -y \
    python3.11 \
    python3-pip \
    nodejs \
    npm

WORKDIR /app
COPY . /app

CMD ["bash"]
# Windows・Mac・Linux、どこでも同じ
docker build -t myenv .
docker run -it myenv

In this case, neither WSL2 nor Lima is necessary. You can unify everything using only Docker.

Common pitfalls and solutions

[Windows] Case 1: "WSL2 is not enabled"

Cause: Virtualization is disabled, or BIOS settings

Solution:

  1. Enable virtualization (VT-x/AMD-V) in BIOS

  2. Run again in PowerShell:

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

[Windows] Case 2: "Consumes too much memory"

Limit WSL2 memory usage.

Solution: Create C:\Users\YourName\.wslconfig:

[wsl2]
memory=4GB
processors=2
swap=2GB

Restart WSL2:

wsl --shutdown
[Windows] Case 3: "File operations are slow"

Cause: It is slow to cross between the Windows file system and the Linux file system

Solution: Place projects on the Linux side (~/). Avoid the Windows side (/mnt/c/).

# ❌ 遅い
cd /mnt/c/Users/YourName/project
✅ 速い
cd ~/project
[Mac] Case 1: "Lima won't start"

Cause: Homebrew version is outdated

Solution:

brew update
brew upgrade lima
[Mac] Case 2: "Docker cannot be used"

If using Docker within Lima:

# Docker対応テンプレートで起動
limactl start --name=docker template://docker
これでDocker使える
lima docker ps
[Common] Case 3: "Packages cannot be installed"

Cause: Package list is outdated

Solution:

sudo apt update
sudo apt install <package>

Next time: Docker Practice

The Linux environment is now set up with WSL2 (or Lima).

Next time is the second installment of the Docker series: Docker Practice

  • Installing Docker Desktop (supports both Windows/Mac)

  • Integrating WSL2/Lima with Docker

  • Starting your first container

  • How to write a Dockerfile

  • Running an actual app with Docker

Get hands-on and learn by doing.

Summary: The same Linux environment on Windows and Mac

Read this far, just remember this:

✅ Production servers are Linux → Development environments should also be unified with Linux
✅ WSL2 = Real Linux running inside Windows
✅ Mac → You can create a Linux environment with Lima, Multipass, or UTM
✅ The ultimate solution is Docker (details next time)
✅ Both WSL2 and Lima work perfectly with Docker

The 3 Principles of Environment Unification:

  1. Use the same OS version (e.g., Ubuntu 22.04)

  2. Automate with setup scripts

  3. Complete unification with Docker is the best

"It works on my machine..." "The behavior is different between Windows and Mac..." "It doesn't work in production..."

This nightmare is over.

Unify your development environment with WSL2 (or Lima). Furthermore, if you use Docker, it's perfect.

Next time, we will dive into Docker in practice.

Are you ready?

Reference links:

  • WSL Official Documentation: https://learn.microsoft.com/ja-jp/windows/wsl/

  • WSL Installation: https://learn.microsoft.com/ja-jp/windows/wsl/install

  • Lima GitHub: https://github.com/lima-vm/lima

  • Multipass Official: https://multipass.run/

  • UTM: https://mac.getutm.app/

  • Docker Official: https://www.docker.com/

Recommended Magazine




同じカテゴリの記事

雨宮 千夏
デジタルマーケター 雨宮 千夏
プロフィールを見る →

アイドルと音楽シーンの取材を担当。公式サイトの公演情報、リリース告知、公開チャートを照合してから構成しています。

おすすめの関連記事