Windows Subsystem for Linux 2 (WSL2)で静的サイトジェネレーターHugoの環境構築をする方法
Ubuntu on WSL2
PowerShellを「管理者として実行」で起動し、以下のコマンドを実行します。
wsl --install
以下のエラーが発生してしまった場合は、次の手順でWSLをインストールし直します。
Installing, this may take a few minutes...
WslRegisterDistribution failed with error: 0x80370114
Error: 0x80370114 ???????????????????????????????????
Press any key to continue...
スタートボタンを押したときに表示されるメニューから最近追加したUbuntuやLinuxをすべてアンインストールします。その後、PowerShellを「管理者として実行」で起動し、 wsl --install
を実行してください。
wsl --install
実行後にPCを再起動すると、ターミナルが自動で立ち上がります。あとは指示に従って、ユーザー名とパスワードを設定すると、晴れて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: wbm
New password:
Retype new password:
passwd: password updated successfully
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.79.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/wbm/.hushlogin file.
wbm@DESKTOP-BOUNK44:~$
Setup Ubuntu
リポジトリを日本のサーバーに切り替えます。
sudo sed -i.bak -e "s/http:\/\/archive\.ubuntu\.com/http:\/\/jp\.archive\.ubuntu\.com/g" /etc/apt/sources.list
Ubuntuをアップデートします。
sudo apt -o Acquire::http::AllowRedirect=false update
sudo apt -o Acquire::http::AllowRedirect=false upgrade
日本語環境を整えます。
sudo apt-get install language-pack-ja
sudo update-locale LANG=ja_JP.UTF8
sudo apt-get install -y manpages-ja manpages-ja-dev
sudo apt-get install -y translate-shell # man hogehoge | trans -b en:ja で man表示の日本語化
Setup VSCode
Visual Studio Code(以下、VSCode)に拡張機能 WSL - Visual Studio Marketplace をインストールします。
インストール完了後に左下に出てくる「><」をクリックします。
以下のウィンドウが表示されるので、「New WSL Window」をクリックします。
接続できると左下に「WSL: Ubuntu」が表示されます。この手順以降、VSCodeのターミナルからUbuntuを操作できます。
Setup Git
Install Git & Setup
UbuntuにGitをインストールします。
sudo apt-get install -y git-all
完了したら次にGitの初期設定を実施します。
git config --global user.name ユーザー名
git config --global user.email メールアドレス
git config --global core.quotepath false
Generate SSH Key
GitHubに登録するためのSSH鍵を生成します。
ssh-keygen -t ecdsa -b 384 -C "コメント"
生成する場所やパスフレーズは必要に応じて設定してください。僕はデフォルトで進めました。
Set SSH Key
生成したSSH鍵をGitHubに登録します。
以下のコマンドで出力した文字列をGitHubの SSH and GPG keys に登録します。
cat ~/.ssh/id_ecdsa.pub
接続確認のために以下のコマンドを実行します。
ssh -T git@github.com
今までに接続を試みたことがないが接続を続けるかというメッセージが表示されるので yes
と入力して続けます。以下のようなメッセージが表示されれば接続成功です。
Hi (account名)! You've successfully authenticated, but GitHub does not provide shell access.
Clone Git Repository
すでにリモートリポジトリが存在する場合は以下のコマンドでクローンします。
git clone [リモートリポジトリURL]
Install Hugo
Ubuntuで以下のコマンドを実行し、Hugoをインストールします。
sudo apt install hugo -y
インストールできたことを確認します。
hugo version
以下のように表示されれば成功です。
hugo v0.92.2+extended linux/amd64 BuildDate=2022-02-23T16:47:50Z VendorInfo=ubuntu:0.92.2-1
参考
VScode - WSL2 - hugo 環境構築(2021年4月25日版) - チラシのすきま
https://monoworks.co.jp/post/2021-04-25-set-up-vscode-wsl2-hugo/
GitHubでssh接続する手順公開鍵・秘密鍵の生成から - Qiita
https://qiita.com/shizuma/items/2b2f873a0034839e47ce