LoginSignup
0
2

More than 1 year has passed since last update.

WSL セットアップに関する備忘録

Last updated at Posted at 2023-02-13

はじめに

今まで何回か WSL のセットアップをしてきたのですが、この度 Windows 11 の再インストールを行ったのでいい機会と思ってセットアップの流れをメモしておこうかと思います。この記事を読む奇特な読者の方がいたら適当に参考にしてくれるとうれしいです。

実行環境

OS: Windows 11 22H2 (OS Build 22623.1255)

Terminal: Windows Terminal

Editor: VSCode (Remote WSL インストール済み)

ちなみに Windows Insider Program に参加して Beta 版を利用しています。また、言語は英語にしています。

インストール

まず、WSL をインストールしていきます。WSL はどんどんアップデートされていくので数年前の資料を見てもいろいろ変わっていたりするので注意が必要ですね。というわけで最新情報の手に入りやすい公式ドキュメントをチェック。

とりあえずインストールできるディストリビューションを確認してみます。

PowerShell
PS > wsl --list --online
The following is a list of valid distributions that can be installed.
The default distribution is denoted by '*'.
Install using 'wsl --install -d <Distro>'.

  NAME               FRIENDLY NAME
* Ubuntu             Ubuntu
  Debian             Debian GNU/Linux
  kali-linux         Kali Linux Rolling
  SLES-12            SUSE Linux Enterprise Server v12
  SLES-15            SUSE Linux Enterprise Server v15
  Ubuntu-18.04       Ubuntu 18.04 LTS
  Ubuntu-20.04       Ubuntu 20.04 LTS
  Ubuntu-22.04       Ubuntu 22.04 LTS
  OracleLinux_8_5    Oracle Linux 8.5
  OracleLinux_7_9    Oracle Linux 7.9

このリスト上のディストリビューションがストアからインストールできるようです。デフォルトでは Ubuntu のバージョン指定なしのものがインストールされます。多分 22.04 LTS ではないでしょうか。

PowerShell
PS > wsl --install <Distribution Name>

を実行すると指定したディストリビューションがインストールされます。<Distribution Name>を記入しないとデフォルトのものがインストールされます。私は特にこだわりもないのでこれでいきます。

PowerShell
PS > wsl --install

入力してからしばらく待機。何度か管理者権限を求められますが、OK します。

PowerShell
The requested operation requires elevation.
Installing: Virtual Machine Platform
Virtual Machine Platform has been installed.
Installing: Windows Subsystem for Linux
Windows Subsystem for Linux has been installed.
Installing: Ubuntu
Ubuntu has been installed.
The requested operation is successful. Changes will not be effective until the system is rebooted.

最終的にこのような表示が出るので指示に従い PC を再起動。すると勝手にターミナルが立ち上がってこのような表示が出ます。

PowerShell
Ubuntu is already installed.
Launching Ubuntu...
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:

画面の指示通りユーザー名を入力。その後このようにパスワード入力を求められるので 2 回入力します(パスワードは入力しても画面に表示されないことに注意)。

PowerShell
New password:
Retype new password:

パスワードの設定が完了するとこのような表示が出てインストールが完了します。

PowerShell
Installation successful!
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.

Welcome to Ubuntu 22.04.1 LTS (GNU/Linux 5.15.83.1-microsoft-standard-WSL2 x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

This message is shown once a day. To disable it please create the
/home/<ubuntu_username>/.hushlogin file.

やっぱりバージョンは 22.04 LTS でした。この時点で Windows Terminal に Ubuntu が登録されているので、ここから作業できます。

初期設定

次に初期設定を行っていきます。私はよくこちらのページを参考にしています。

Ubuntu 環境のアップデート

まずは Ubuntu 環境のアップデートを行っていきます。以下のコマンドを実行します。

bash
$ sudo apt update && sudo apt upgrade -y

これでパッケージが更新できたはずです。

GUI アプリのインストール

現在の WSL では別途アプリを入れることなく Linux の GUI アプリが動かせます。これが正常に動作するか確認するため以下のコマンドを実行します。

bash
$ sudo apt install x11-apps -y

その後 xeyes を実行し、実際にアプリを起動してみます。私の環境では一瞬で表示されました。前は初回起動にもっと時間がかかっていたんですが、いろいろ改善されてるんでしょうか。

pacup のインストール

ここで、パッケージアップデート用のコマンドである pacup をインストールします。これによって sudo apt update && sudo apt full-upgrade && sudo apt autoremove && sudo snap refresh && flatpak update という一連のコマンドを sudo pacup 一つで実行できるようになります。このコマンドの詳細はこちらに記載してあります。

まず、インストール用シェルスクリプトをダウンロードします。ふつうは Windows 側のダウンロードフォルダにダウンロードされると思うので、今回はその前提で進めます。

次に、Ubuntu 側からWindows 側のダウンロードフォルダに移動します。以下のコマンドを実行します。<windows_username> のところは適当に読み替えてください。

bash
$ cd /mnt/c/Users/<windows_username>/Downloads

ls コマンドを実行して pacup-installer.sh があることを確認します。問題がなければ以下のコマンドを実行します。

bash
$ sudo bash pacup-installer.sh

[sudo] password for <ubuntu_username> という表示が出たらパスワードを入力します。以下のような表示が出ます。

bash
[sudo] password for <ubuntu_username>:
rm: cannot remove '../Desktop/pacup_Manual.txt': No such file or directory
Cloning into 'pacup'...
remote: Enumerating objects: 175, done.
remote: Counting objects: 100% (63/63), done.
remote: Compressing objects: 100% (30/30), done.
remote: Total 175 (delta 38), reused 56 (delta 33), pack-reused 112
Receiving objects: 100% (175/175), 56.72 KiB | 1.13 MiB/s, done.
Resolving deltas: 100% (93/93), done.
Success!

ここで実際にコマンドが動くか確かめたところ、このようなエラーが出ました。

bash
$ sudo pacup
Flatpakパッケージをアップデートします
Flatpakパッケージをアップデートしました

Snapパッケージをアップデートします
error: cannot communicate with server: Post "http://localhost/v2/snaps": dial unix /run/snapd.socket: connect: no such file or directory

Root(管理者)権限を要求しています。
もう一度お試しください。
error: cannot communicate with server: Post "http://localhost/v2/snaps": dial unix /run/snapd.socket: connect: no such file or directory

権限の昇格に失敗したため、 実行を終了しました。

どうやら snap 関連で問題が起きているよう。色々調べた結果、以下のような手順を踏めばよさそうです。

まず、以下のコマンドを実行して wsl.conf ファイルを作成します(参考)。

bash
$ cd /etc
$ sudo touch wsl.conf

その後、以下のコマンドを実行して wsl.conf ファイルの編集画面を開きます(ここでは vi を用いて編集します。使い方はこちらを参照)。

bash
$ sudo vi wsl.conf

wsl.conf を以下のように設定します(参考)。

/etc/wsl.conf
[boot]
systemd=true

その後、PowerShell 側から WSL を再起動します。

PowerShell
PS > wsl --shutdown

これで WSL が終了するので、もう一度立ち上げます。その後、再度 sudo pacup を実行した結果、うまくいきました。

Homebrew のインストール

パッケージマネージャーの Homebrew をインストールします。Homebrew に関するネット上の説明は Mac OS 用のものが多いですが、Linux ユーザーでも十分参考にできるので積極的に活用していきたいです。とはいえ基本は公式サイトの説明に従うのが一番手っ取り早いですね。

まず、以下のスクリプトを実行します。

bash
$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

途中、以下のような表示が出るので Enter キーを押します。

bash
Press RETURN/ENTER to continue or any other key to abort:

しばらく待つと、以下のような表示が出ます。

bash
...

==> Installation successful!

==> Homebrew has enabled anonymous aggregate formulae and cask analytics.
Read the analytics documentation (and how to opt-out) here:
  https://docs.brew.sh/Analytics
No analytics data has been sent yet (nor will any be during this install run).

==> Homebrew is run entirely by unpaid volunteers. Please consider donating:
  https://github.com/Homebrew/brew#donations

==> Next steps:
- Run these two commands in your terminal to add Homebrew to your PATH:
    (echo; echo 'eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"') >> /home/<ubuntu_username>/.profile
    eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
- Install Homebrew's dependencies if you have sudo access:
    sudo apt-get install build-essential
  For more information, see:
    https://docs.brew.sh/Homebrew-on-Linux
- We recommend that you install GCC:
    brew install gcc
- Run brew help to get started
- Further documentation:
    https://docs.brew.sh

あとは ==> Next steps: 以下に表示された指示に従います。ここでは以下の操作を行います。実際に表示されたのとは異なる操作を行っている部分もあります。

まず、1 つ目です。ここでは、Homebrew の PATH を通すことを要求されているのですが、.bash_profile に書き込むとうまくいかなかったので書き込み先を .bashrc にします。ここでは vi を用いて直接書き込みます。

bash
$ sudo vi ~/.bashrc
~/.bashrc
export PATH="/home/linuxbrew/.linuxbrew/bin:$PATH"

書き込み後、以下のコマンドを実行して変更を反映させます。

bash
$ source ~/.bashrc

次に、以下のコマンドを実行して Homebrew の依存関係をインストールします。

bash
$ sudo apt install build-essential -y

最後に推奨されたように GCC をインストールします。

bash
$ brew install gcc

ここで、brew doctor コマンドを実行して以下のようになれば設定完了です。

bash
$ brew doctor
Your system is ready to brew.

最後に再起動しても brew が動くことを確認しておきます。

fish のインストール ~ 設定

個人的にシェルは fish が好みなので Homebrew を使ってインストールしていきます。

インストール

公式ページにある通り、コマンド一発でインストールできます。以下のコマンドを実行します。

bash
brew install fish

インストールが完了したら fish と入力して fish が立ち上がったら成功です。

Homebrew の PATH を通す

bash の時と同じ要領で PATH を通します。fish コマンドを一度でも実施していると config.fish が生成されます。

PATH を通すため、config.fish を開きます。

bash
$ sudo vi ~/.config/fish/config.fish
~/.config/fish/config.fish
fish_add_path /home/linuxbrew/.linuxbrew/bin

書き込み後、以下のコマンドを実行して変更を反映させます。

bash
$ source ~/.config/fish/config.fish

デフォルトシェルの変更

fish をデフォルトシェルに変更します(参考)。まず、現在選択可能なシェルを確認します。以下のコマンドを実行します。

bash
$ cat /etc/shells
# /etc/shells: valid login shells
/bin/sh
/bin/bash
/usr/bin/bash
/bin/rbash
/usr/bin/rbash
/usr/bin/sh
/bin/dash
/usr/bin/dash
/usr/bin/tmux
/usr/bin/screen

この時点では fish はデフォルトシェルとして選択できません。そこで、まず fish の実行コマンドの場所を確認します。

bash
$ which fish
/home/linuxbrew/.linuxbrew/bin/fish

これを踏まえて以下のコマンドを実行します。これによってデフォルトシェルを変更できます。

bash
$ echo /home/linuxbrew/.linuxbrew/bin/fish | sudo tee -a /etc/shells
$ chsh -s /home/linuxbrew/.linuxbrew/bin/fish

再起動して fish が立ち上がることを確かめます。

カスタマイズ

シェルのデザインをカスタマイズしていきます。前提条件として、Nerd Font のインストールが必要ですが、私はすでに白源をインストールし、Windows Terminal のデフォルトフォントに設定済みです(正確には白源のシリーズのうち、HackGen Console NF を設定しています)。

では、カスタマイズを行っていきます。ここでは Starship を用いることにします。まず、Starship を Homebrew でインストールします。以下のコマンドを実行します。

fish
$ brew install starship

~/.config/fish/config.fish の最後に以下を追記します。

~/.config/fish/config.fish
starship init fish | source

設定内容を記載するファイルを作成します。

fish
$ mkdir -p ~/.config && touch ~/.config/starship.toml

~/.config/starship.toml に設定内容を記載します。設定自体は 公式ドキュメント に従えば簡単にできますが、項目が多いので Pastel Powerline プリセットをベースに若干の調整を加えました。以下にその内容を掲載します。

starship.toml
format = """
[](#601c52)\
$os\
$shell\
[](fg:#601c52 bg:#9A348E)\
$username\
[](fg:#9A348E bg:#DA627D)\
$directory\
[](fg:#DA627D bg:#FCA17D)\
$git_branch\
$git_status\
[](fg:#FCA17D bg:#86BBD8)\
$c\
$elixir\
$elm\
$golang\
$haskell\
$java\
$julia\
$lua\
$nodejs\
$nim\
$python\
$rlang\
$ruby\
$rust\
$scala\
[](fg:#86BBD8 bg:#06969A)\
$conda\
$docker_context\
[](fg:#06969A bg:#33658A)\
$time\
[ ](fg:#33658A)\
"""

# Disable the blank line at the start of the prompt
# add_newline = false

# You can also replace your username with a neat symbol like   or disable this
# and use the os module below
[username]
show_always = true
style_user = "bg:#9A348E"
style_root = "bg:#9A348E"
format = '[ $user ]($style)'
disabled = false

# An alternative to the username module which displays a symbol that
# represents the current operating system
[os]
style = "bg:#601c52"
disabled = false # Disabled by default

[os.symbols]
Alpine = " "
Amazon = " "
Android = " "
Arch = " "
CentOS = " "
Debian = " "
DragonFly = " "
Emscripten = " "
EndeavourOS = " "
Fedora = " "
FreeBSD = " "
Garuda = "﯑ "
Gentoo = " "
HardenedBSD = "ﲊ "
Illumos = " "
Linux = " "
Macos = " "
Manjaro = " "
Mariner = " "
MidnightBSD = " "
Mint = " "
NetBSD = " "
NixOS = " "
OpenBSD = " "
openSUSE = " "
OracleLinux = " "
Pop = " "
Raspbian = " "
Redhat = " "
RedHatEnterprise = " "
Redox = " "
Solus = "ﴱ "
SUSE = " "
Ubuntu = " "
Unknown = " "
Windows = " "

[shell]
bash_indicator = ''
fish_indicator = ''
zsh_indicator = '%_'
powershell_indicator = ''
ion_indicator = ''
elvish_indicator = 'ﬦ'
tcsh_indicator = 'T'
xonsh_indicator = ' '
cmd_indicator = ''
nu_indicator = 'nu'
unknown_indicator = ''
disabled = false
style = "bg:#601c52"
format = '[$indicator ]($style)'

[directory]
style = "bg:#DA627D"
format = "[ $path ]($style)"
truncation_length = 3
truncation_symbol = "…/"

# Here is how you can shorten some long paths by text replacement
# similar to mapped_locations in Oh My Posh:
[directory.substitutions]
"Documents" = " "
"Downloads" = " "
"Music" = " "
"Pictures" = " "
# Keep in mind that the order matters. For example:
# "Important Documents" = "  "
# will not be replaced, because "Documents" was already substituted before.
# So either put "Important Documents" before "Documents" or use the substituted version:
# "Important  " = "  "

[c]
symbol = " "
style = "bg:#86BBD8"
format = '[ $symbol ($version) ]($style)'

[conda]
symbol = "  "
style = "bg:#06969A"
format = '[ $symbol $context ]($style) $path'

[docker_context]
symbol = " "
style = "bg:#06969A"
format = '[ $symbol $context ]($style) $path'

[elixir]
symbol = " "
style = "bg:#86BBD8"
format = '[ $symbol ($version) ]($style)'

[elm]
symbol = " "
style = "bg:#86BBD8"
format = '[ $symbol ($version) ]($style)'

[git_branch]
symbol = ""
style = "bg:#FCA17D"
format = '[ $symbol $branch ]($style)'

[git_status]
style = "bg:#FCA17D"
format = '[$all_status$ahead_behind ]($style)'

[golang]
symbol = " "
style = "bg:#86BBD8"
format = '[ $symbol ($version) ]($style)'

[haskell]
symbol = " "
style = "bg:#86BBD8"
format = '[ $symbol ($version) ]($style)'

[java]
symbol = " "
style = "bg:#86BBD8"
format = '[ $symbol ($version) ]($style)'

[julia]
symbol = " "
style = "bg:#86BBD8"
format = '[ $symbol ($version) ]($style)'

[lua]
symbol = " "
style = "bg:#86BBD8"
format = '[ $symbol ($version) ]($style)'

[nodejs]
symbol = " "
style = "bg:#86BBD8"
format = '[ $symbol ($version) ]($style)'

[nim]
symbol = " "
style = "bg:#86BBD8"
format = '[ $symbol ($version) ]($style)'

[python]
symbol = "  "
style = "bg:#86BBD8"
format = '[ $symbol ($version) ]($style)'

[rlang]
symbol = "ﳒ  "
style = "bg:#86BBD8"
format = '[ $symbol ($version) ]($style)'

[ruby]
symbol = " "
style = "bg:#86BBD8"
format = '[ $symbol ($version) ]($style)'

[rust]
symbol = " "
style = "bg:#86BBD8"
format = '[ $symbol ($version) ]($style)'

[scala]
symbol = " "
style = "bg:#86BBD8"
format = '[ $symbol ($version) ]($style)'

[time]
disabled = false
time_format = "%R" # Hour:Minute Format
style = "bg:#33658A"
format = '[ ♥ $time ]($style)'

ここでは省略しますが、同じ要領で bash や PowerShell などのほかのシェルでも同様にカスタマイズできます。また、

bashrc
export STARSHIP_CONFIG=<path_of_starship.toml>/starship.toml
config.fish
export STARSHIP_CONFIG=<path_of_starship.toml>/starship.toml
Microsoft.PowerShell_profile.ps1
$ENV:STARSHIP_CONFIG = "<path_of_starship.toml>/starship.toml"

のように書き込むことで starship.toml をデフォルト以外の場所に指定できたり、複数のシェルで同じ設定ファイルを共有できます。

Python の環境構築

ここでは Python の環境構築を行っていきます。今まで anyenv から pyenv を入れてそこから Python を入れていたんですが、どうやら asdf を使うといいという記事を見かけたので、試してみようと思います。

……と言いたかったんですが、どうやら Starship を入れていると表示が遅くなるとのこと。そこで asdf の Rust クローンである rtx を使っていこうと思います(参考)。

rtx のインストール

公式ドキュメントに従ってインストールしていきます。Homebrew を使ってインストールできるようです。以下のコマンドを実行します。

fish
$ brew install jdxcode/tap/rtx

その後、config.fish に以下のように書き込みます。

config.fish
rtx activate fish | source
rtx hook-env -s fish | source
rtx complete -s fish | source

ここで、依存関係をインストールします。以下のコマンドを実行します。

fish
$ brew install coreutils git

続けて、以下のコマンドを実行します。ここでインストールするパッケージを Homebrew でインストールするとエラーが出たので、apt でインストールします(参考)。

fish
$ sudo apt update && sudo apt install build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev curl libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev

Python のインストール

Python をインストールします。公式ドキュメントを参考にします。まず、以下のコマンドを実行します(この操作は必須ではありません)。

fish
$ rtx plugins ls -a

すると、以下のようにインストール可能なプラグインの一覧が表示されます。

fish
1password-cli
R
act

...

zola
zoxide
zprint

rtx におけるランタイムのインストールには rtx install [OPTIONS] [RUNTIME]... のような形式で行います。例として、Python 最新バージョンと、Python ディストリビューションの Anaconda をそれぞれインストールしてみます。

まず、Python 最新バージョンをインストールします。以下のコマンドを実行します。

fish
$ rtx install python@latest

latest と入力することで最新バージョン(執筆時点では Python 3.11.2)をインストールできます。

次に、Anaconda3-2022.05 をインストールします。以下のコマンドを実行します。

fish
$ rtx install python@anaconda3-2022.05

ランタイムが 2 つともインストール出来たところで、実際にインストールされたランタイムの一覧を取得します。以下のコマンドを実行します。

fish
$ rtx ls

すると、以下のように一覧が表示されます。

fish
   python anaconda3-2022.05
   python 3.11.2

ここで、インストールしたランタイムをデフォルトの Python として設定します。以下のコマンドを実行します。

fish
$ rtx global python@3.11.2

これで python コマンドを実行した際に Python 3.11.2 が起動します。

ちなみに、rtx ls-remote python のようにコマンドを入力すると、インストール可能なランタイムの一覧が出力されます。ただし、これはインストール済みのプラグインにのみ適用されます。また、プラグインのみをインストールする場合は rtx install -p python のようにするとよいです。気が向いたら rtx についていろいろ調べてまとめてみようかなと思います。

LaTeX のインストール

続いて LaTeX のインストールを行っていきます。やり方にはいろいろありますが、ここでは TeX Live を直接インストールしていきます(以前 Homebrew を使っていたのですが、関連するパッケージのバージョンが合わない的なエラーが出てしまったので必要なものを一括でインストールできるかつ最新版の使えるこの手法で行きます)。TeX Wiki のインストールガイドに従います。

経験則的に作業するディレクトリは Ubuntu のもののほうが高速でインストールできるので、Windows 側で作業していた場合は一度 cd コマンドを実行して Ubuntu 側のホームディレクトリに移動しておきます。

まず、ミラーサイトからインストーラをダウンロードします。

fish
$ curl -OL http://mirror.ctan.org/systems/texlive/tlnet/install-tl-unx.tar.gz

ダウンロードしたインストーラを展開します。

fish
$ tar xvf install-tl-unx.tar.gz

展開したインストーラのディレクトリに移動します。

fish
$ cd install-tl-2*

インストーラを実行します。

fish
$ sudo ./install-tl -no-gui -repository http://mirror.ctan.org/systems/texlive/tlnet/

しばらくすると以下のような表示が出るので、I を入力してインストールを開始します。

fish
...

Actions:
 <I> start installation to hard disk
 <P> save installation profile to 'texlive.profile' and exit
 <Q> quit

Enter command:

インストールが終了したら /usr/local/bin ディレクトリ配下にシンボリックリンクを追加します。

fish
$ sudo /usr/local/texlive/????/bin/*/tlmgr path add

最後に、latexindent が動くように以下のコマンドを実行します(参考)。

fish
$ brew install perl
$ brew install cpanm
$ cpanm Log::Log4perl Log::Dispatch::File YAML::Tiny File::HomeDir Unicode::GCString

おわりに

ここまで WSL のセットアップの手順をまとめてきましたが、今まで気づいたら何とかなっていたエラーの解決策などをまとめられたのでいい機会でした。今後も必要に応じて追記できればと思います(想像以上に長文になって疲れた...)。

参考文献

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2