個人的WSL2環境構築tips
今は2024年5月ですが、以下の2020年12月のメモが残っていたので公開します。
普段はMacを利用している私。サブ機としてWindowsPCが欲しくなり、Ryzen CPUが搭載されたLenovo ThinkPad X13 Gen1 AMDを購入しました。そして最近WSL2を使って開発をしてみようと思い環境を構築しています。
こちらの記事を大変参考にさせていただきました。ありがとうございます。
以下では上記を踏まえ、個人的につまづいた点に焦点を置いてメモします。
WSL2をインストール
こちらからWSL2をインストールします。こちらに従っていただいたら最低限の準備が整います。
ディストリビューション、私はUbuntu20.04を使っています。
パッケージ更新、zsh導入
ubuntuのパッケージを更新するには以下コマンドを実行します。
sudo apt update
sudoa apt upgrade
デフォルトシェルがbashです。zshに変えます。
私はプラグインマネージャーとしてzinitを愛用しています。
zshrcにはひとまず以下のスクリプトを記入しておくとよいです。詳しくは下記リンク参照。
# Two regular plugins loaded without investigating.
zinit light zsh-users/zsh-autosuggestions
zinit light zdharma/fast-syntax-highlighting
# Plugin history-search-multi-word loaded with investigating.
zinit load zdharma/history-search-multi-word
# Load the pure theme, with zsh-async library that's bundled with it.
zinit ice pick"async.zsh" src"pure.zsh"
zinit light sindresorhus/pure
# A glance at the new for-syntax – load all of the above
# plugins with a single command. For more information see:
# https://zdharma.org/zinit/wiki/For-Syntax/
zinit for \
light-mode zsh-users/zsh-autosuggestions \
light-mode zdharma/fast-syntax-highlighting \
zdharma/history-search-multi-word \
light-mode pick"async.zsh" src"pure.zsh" \
sindresorhus/pure
# Binary release in archive, from GitHub-releases page.
# After automatic unpacking it provides program "fzf".
zinit ice from"gh-r" as"program"
zinit load junegunn/fzf-bin
# One other binary release, it needs renaming from `docker-compose-Linux-x86_64`.
# This is done by ice-mod `mv'{from} -> {to}'. There are multiple packages per
# single version, for OS X, Linux and Windows – so ice-mod `bpick' is used to
# select Linux package – in this case this is actually not needed, Zinit will
# grep operating system name and architecture automatically when there's no `bpick'.
zinit ice from"gh-r" as"program" mv"docker* -> docker-compose" bpick"*linux*"
zinit load docker/compose
# Vim repository on GitHub – a typical source code that needs compilation – Zinit
# can manage it for you if you like, run `./configure` and other `make`, etc. stuff.
# Ice-mod `pick` selects a binary program to add to $PATH. You could also install the
# package under the path $ZPFX, see: http://zdharma.org/zinit/wiki/Compiling-programs
zinit ice as"program" atclone"rm -f src/auto/config.cache; ./configure" \
atpull"%atclone" make pick"src/vim"
zinit light vim/vim
# Scripts that are built at install (there's single default make target, "install",
# and it constructs scripts by `cat'ing a few files). The make'' ice could also be:
# `make"install PREFIX=$ZPFX"`, if "install" wouldn't be the only, default target.
zinit ice as"program" pick"$ZPFX/bin/git-*" make"PREFIX=$ZPFX"
zinit light tj/git-extras
# Handle completions without loading any plugin, see "clist" command.
# This one is to be ran just once, in interactive session.
zinit creinstall %HOME/my_completions
WindowsTerminalのsetting.json
windowsTerminalのsetting.jsonは以下のpathに格納されています。ターミナルのテーマを変えたいときに触る必要があります。ターミナル上でctrl+,
すれば開けるのですが、自分の場合IEで操作する設定が変えられなくて直接パスを参照してVSCodeで編集してます。
# setting.jsonがあるpathの名前
pwd
/mnt/c/Users/{YOUR_NAME}/LocalSettings/Packages/Microsoft.WindowsTerminal_8wekyb3d8bbwe/LocalState
# VSCodeで編集
code .
Get themeでターミナルのテーマをコピーします。
例えばこんな感じで
"schemes": [
{
"name" : "Campbell",
"cursorColor": "#FFFFFF",
"selectionBackground": "#FFFFFF",
"background" : "#0C0C0C",
"foreground" : "#CCCCCC",
"black" : "#0C0C0C",
"blue" : "#0037DA",
"cyan" : "#3A96DD",
"green" : "#13A10E",
"purple" : "#881798",
"red" : "#C50F1F",
"white" : "#CCCCCC",
"yellow" : "#C19C00",
"brightBlack" : "#767676",
"brightBlue" : "#3B78FF",
"brightCyan" : "#61D6D6",
"brightGreen" : "#16C60C",
"brightPurple" : "#B4009E",
"brightRed" : "#E74856",
"brightWhite" : "#F2F2F2",
"brightYellow" : "#F9F1A5"
},
こちらも参考に
brew install
パッケージマネージャーbrewを使えるようにします。
test -d ~/.linuxbrew && eval $(~/.linuxbrew/bin/brew shellenv)
test -d /home/linuxbrew/.linuxbrew && eval $(/home/linuxbrew/.linuxbrew/bin/brew shellenv)
test -r ~/.bash_profile && echo "eval \$($(brew --prefix)/bin/brew shellenv)" >>~/.bash_profile
echo "eval \$($(brew --prefix)/bin/brew shellenv)" >>~/.profile
brew install hello
hugo
個人ブログに適用している静的ブログジェネレーターHugo。バイナリを入手してインストールすることも可能ですが、brewを使うほうが早いです。
brew install hugo
cd /path/to/hugoのプロジェクト
hugo server
Railsアプリ作成環境をつくる
# node.jsをインストール
sudo apt-get install npm
# node.jsのバージョン管理をするn
sudo npm install -g n
sudo n stable
# rbenvをインストール
git clone https://github.com/sstephenson/rbenv.git ~/.rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
~/.rbenv/bin/rbenv init
echo eval "$(rbenv init -)" << ~/.zshrc
source ~/.zshrc
# ruby-buildをインストール
git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
# ruby2.7をインストール
rbenv install 2.7.1
rbenv rehash
rbenv local 2.7.1
# rails6.0.0をインストール
gem install rails -v 6.0.0
# bundlerをインストール
gem install bundler
新規プロジェクトを作成する場合
rails new foo
cd foo
bin/rails server
これは私特有の問題かもしれないが、既存のRailsプロジェクトをcloneしてきたとき、
An error occurred while installing pg (1.2.3), and Bundler cannot continue.
というエラーが出た。
人によってはbrew install postgresql
or sudo apt-get install postgresql
するなり、sudo yum install postgresql-devel
を実行すれば解決する場面、わたしは以下のコマンドを打ってエラー解決した。
sudo apt install postgresql-contrib libpq-dev
小ネタ集
随時更新するつもりです。
open
macOSだとデフォルトで使えるopenコマンドのエイリアスをあてる。
alias open='powershell.exe /c start'
クリップボードにコピー
たとえばssh-genで公開鍵を作成してGitHubに貼り付けたいとき。macだったらpbcopyコマンドを使いますが、linuxでは以下コマンドを使います
cat /path/to/file | clip.exe
Discussion