Umi Uyuraのブログ

プログラミング関連の作業ログ

WSL2環境構築(Homebrewとasdf)

先日Windows PCのセットアップをおこなって、ひとまずWindowsとWSLの環境を整えるとっかかりはできましたので、次にWSL側の環境構築を進めました。

umi-uyura.hatenablog.com

方針

WSL2で選択できるLinuxディストリビューションはいろいろとありますが、WSLを利用しようとした際にまず挙げられるUbuntuにしています。

Ubuntuには標準でaptというパッケージマネージャが備わっていますが、そういえばMacでお世話になっているHomebrewがLinuxにも対応していることを思い出したので、これも利用してみることにしました。

またプログラミング言語関連はプロジェクトによって異なるバージョンを扱うこともあるため、asdfというツールも導入してみます。

と、いろいろと入れてしまいますが、以下のような使い分けをしていこうかなと思っています。

  • プログラミング言語環境のようにプロジェクトによって使いたいバージョンが変わる(複数のバージョンを切り替えて使う)可能性があるものはasdf
  • 開発ツールのように新しいバージョンが出たら素直にアップデートしていくものはHomebrew
  • 必要に応じてapt

Homebrewインストール

Linux版Homebrewというものが別にあるのかと思っていましたが(当初はLinuxbrewという名前で別物ではあったようですが)、現在ではMac版に統合されているようで、インストールスクリプトは同じものを使うようです。

そのためパッケージによってはMac版はあるものの、Linux版はないというものもあるようです。

また当然といえば当然ですが、Mac用のアプリケーションを管理するHomebrew Caskのパッケージは、そもそもLinuxにはインストールできません。

Homebrew on Linux — Homebrew Documentation

たまに内容が変わっていることがあるようですが、今回実行したときは以下のようなものでした。

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

Windows TerminalでWSLのUbuntuに入って、上記のワンライナーを実行します。すると、

$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
==> Checking for `sudo` access (which may request your password).
==> Select the Homebrew installation directory
- Enter your password to install to /home/linuxbrew/.linuxbrew (recommended)
- Press Control-D to install to /home/<user>/.linuxbrew
- Press Control-C to cancel installation
[sudo] password for <user>:

と表示され、最後のところで sudo のパスワードを要求されました。

インストール後はHomebrewが sudo を必要とすることはないようですが、 sudo が使えるかどうかでインストール先が変わるのと、それによってより多くのバイナリパッケージがインストールできるようです。

そちらが推奨のようなので、 sudo ありで進めました。

==> This script will install:
/home/linuxbrew/.linuxbrew/bin/brew
/home/linuxbrew/.linuxbrew/share/doc/homebrew
/home/linuxbrew/.linuxbrew/share/man/man1/brew.1
/home/linuxbrew/.linuxbrew/share/zsh/site-functions/_brew
/home/linuxbrew/.linuxbrew/etc/bash_completion.d/brew
/home/linuxbrew/.linuxbrew/Homebrew
==> The following new directories will be created:
/home/linuxbrew/.linuxbrew/bin
/home/linuxbrew/.linuxbrew/etc
/home/linuxbrew/.linuxbrew/include
/home/linuxbrew/.linuxbrew/lib
/home/linuxbrew/.linuxbrew/sbin
/home/linuxbrew/.linuxbrew/share
/home/linuxbrew/.linuxbrew/var
/home/linuxbrew/.linuxbrew/opt
/home/linuxbrew/.linuxbrew/share/zsh
/home/linuxbrew/.linuxbrew/share/zsh/site-functions
/home/linuxbrew/.linuxbrew/var/homebrew
/home/linuxbrew/.linuxbrew/var/homebrew/linked
/home/linuxbrew/.linuxbrew/Cellar
/home/linuxbrew/.linuxbrew/Caskroom
/home/linuxbrew/.linuxbrew/Frameworks

Press RETURN to continue or any other key to abort

インストールされるコマンドや作成されるフォルダについて説明が出たあとで、続けるかどうかの確認が出るので、そのままENTER。

...
Warning: /home/linuxbrew/.linuxbrew/bin is not in your PATH.
  Instructions on how to configure your shell for Homebrew
  can be found in the 'Next steps' section below.
...

インストール処理が完了すると、途中で上記のようなWarningが出ているとおり、HomebrewのbinをPATHに追加する必要があります。

それについて、サイトには以下のような手順が示されていましたが、

$ 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

スクリプトの実行結果を見ていると、最後のあたりに Next steps という出力がありました。

それによると、

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

とあったので、まずは以下を実行。

$ echo 'eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"' >> /home/<user>/.profile
$ eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"

そのあとで brew help を実行して、ヘルプが表示されることを確認。

$ brew help
Example usage:
  brew search TEXT|/REGEX/
  brew info [FORMULA|CASK...]
...

あとHomebrewの依存関係で必要らしいので、以下を入れておきます。

$ sudo apt install build-essential

※公式には apt-get と書かれていましたが、今は apt が推奨らしいので、そこだけ変更しています。

※公式には procps curl file git も記載されていましたが、私の環境にはすでに入っていたためか、上記しか要求されませんでした。

あとこれも推奨とのことなので、さっそく brew コマンドで入れておきます。

$ brew install gcc

Next steps にはありませんでしたが、公式の手順にやっておくと良いよと書いてあったので、 brew doctor を実行。

$ brew doctor
Your system is ready to brew.

問題なし。

無事にインストールできたようです。

asdfインストール

続いて、asdfをインストールします。

asdf

公式のインストール手順は以下のリンク先にあります。

Getting Started | asdf

ですが、実はHomebrewでも提供されているので、せっかくなので今回はHomebrewで入れてみました。

$ brew install asdf

...
==> Summary
🍺  /home/linuxbrew/.linuxbrew/Cellar/asdf/0.8.1_1: 122 files, 364.4KB
==> Caveats
==> asdf
To use asdf, add the following line to your ~/.profile:
  . /home/linuxbrew/.linuxbrew/opt/asdf/libexec/asdf.sh

Restart your terminal for the settings to take effect.

Bash completion has been installed to:
  /home/linuxbrew/.linuxbrew/etc/bash_completion.d

.profileに . /home/linuxbrew/.linuxbrew/opt/asdf/libexec/asdf.sh を追記しておけとあるので、そのとおりにしておきます。

その後、ターミナルを開き直して、試しに以下のコマンドを実行してみます。

$ asdf plugin list all
1password-cli                 https://github.com/NeoHsu/asdf-1password-cli.git
R                             https://github.com/asdf-community/asdf-r.git
act                           https://github.com/grimoh/asdf-act.git
adr-tools                     https://gitlab.com/td7x/asdf/adr-tools.git
ag                            https://github.com/koketani/asdf-ag.git
age                           https://github.com/threkk/asdf-age
aks-engine                    https://github.com/robsonpeixoto/asdf-aks-engine.git
...

するとプラグインの一覧が出力されました。

これは asdf-vm/asdf-plugins リポジトリに集められているプラグインの一覧のようで、ここに掲載されているプラグインについてはインストール時にPlugin ListのLanguage欄の名前で指定することができるようです。(基本的にはリポジトリのURLを指定する)

これでasdfもインストールできました。

asdfを使ったPythonのインストール

さっそくasdfを使って何かインストールしてみることにしました。

公式のチュートリアルでは続けてNode.jsをインストールしていますが、Node.jsは別途専用の管理ツールを使いたいと考えていたので、代わりにPythonを入れてみます。

さきほどのPlugin Listの中にPythonも含まれていましたので、必要な依存関係を確認するためにそのリポジトリを見てみます。

danhper/asdf-python

Pythonプラグインは内部的にpyenvを使っているようで、以下のページを参照して必要な依存関係をインストールします。

Suggested build environment | pyenv/pyenv Wiki

Ubuntuだと以下のものをインストールするようです。

$ sudo apt install make build-essential libssl-dev zlib1g-dev \
libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm \
libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev

※今回asdfはHomebrewで入れていたこともあり、最初はLinuxberwと指定されている方をインストールしてみたのですが、Pythonをインストールする際にエラーになったため、Ubuntuで指定されているものをインストールしました。

もろもろのインストールが終わったら、Pythonプラグインを追加します。

$ asdf plugin add python
$ asdf plugin list
python

次に、提供されているPythonのバージョンを確認します。

$ asdf list all python
Downloading python-build...
Cloning into '/home/<user>/.asdf/plugins/python/pyenv'...
remote: Enumerating objects: 20296, done.
remote: Counting objects: 100% (1208/1208), done.
remote: Compressing objects: 100% (533/533), done.
remote: Total 20296 (delta 718), reused 950 (delta 566), pack-reused 19088
Receiving objects: 100% (20296/20296), 4.14 MiB | 769.00 KiB/s, done.
Resolving deltas: 100% (13653/13653), done.
2.1.3
2.2.3
2.3.7
2.4.0
2.4.1
2.4.2
2.4.3
2.4.4
...

初回だからか、まずは裏で使われているpyenvのインストールが走ったあとで、バージョンの一覧が表示されました。

この時点の3.9系の最新版を入れてみます。

$ asdf install python 3.9.7
remote: Enumerating objects: 17, done.
remote: Counting objects: 100% (17/17), done.
remote: Compressing objects: 100% (6/6), done.
remote: Total 17 (delta 10), reused 10 (delta 10), pack-reused 0
Unpacking objects: 100% (17/17), 15.13 KiB | 3.03 MiB/s, done.
From https://github.com/pyenv/pyenv
   58e20879..90d0d205  master     -> origin/master
python-build 3.9.7 /home/<user>/.asdf/installs/python/3.9.7
Downloading Python-3.9.7.tar.xz...
-> https://www.python.org/ftp/python/3.9.7/Python-3.9.7.tar.xz
Installing Python-3.9.7...
python-build: use readline from homebrew
Installed Python-3.9.7 to /home/<user>/.asdf/installs/python/3.9.7

グローバルで利用するバージョンをセットしておきます。

$ asdf global python 3.9.7
$ python --version
Python 3.9.7
$ cat $HOME/.tool-versions
python 3.9.7

asdfで使用するバージョンは .tool-versions というファイルで管理されるようなので、HOMEディレクトリにそのファイルが生成されていることも確認できました。