Webサイト制作

Windows Subsystem for Linux(Bash on Windows)にHomebrewをインストールする

LinuxbrewがHomebrewに統合されてWindows Subsystem for Linux(Bash on Windows)に対応したということなので、HomebrewをWSL上にインストールして使ってみました。

公開日: 2019.2.19

システム環境&WSLバージョン

cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=18.04
DISTRIB_CODENAME=bionic
DISTRIB_DESCRIPTION="Ubuntu 18.04.1 LTS"

uname -a
Linux DESKTOP-48859VI 4.4.0-17763-Microsoft #253-Microsoft Mon Dec 31 17:49:00 PST 2018 x86_64 x86_64 x86_64 GNU/Linux

インストール

ネット上ではいろいろなサイトでいろんなやり方が書かれていますが、それぞれ書いているHomebrewのバージョンが違ったりでうまく出来ませんでした。

最終的に公式サイトのやり方でインストールができました(当たり前)。本記事も、バージョンによっては対応しないかもしれないので、ご留意ください。

Linuxbrew | Homebrew Documentation

sh -c "$(curl -fsSL https://raw.githubusercontent.com/Linuxbrew/install/master/install.sh)"

==> 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/c0d3man/.cache/Homebrew/
/home/linuxbrew/.linuxbrew/Homebrew

Press RETURN to continue or any other key to abort

この画面でエンターキーを押せばインストールが進みます。

最後にPATHについかしてねというワーニングが出てインストール完了です。

PATHを設定

そのままではbrewコマンドが認識されないので、こちらも公式ガイダンス通りに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

一度、WSLコンソールからログアウトすると、brewコマンドが使えるようになります。(各ファイルをsourceで読み直してもOK)

brew doctor

環境テストをしてみましょう。

$ brew doctor
Please note that these warnings are just used to help the Homebrew maintainers
with debugging if you file an issue. If everything you use Homebrew for is
working fine: please don't worry or file an issue; just ignore this. Thanks!

Warning: umask is currently set to 000. Directories created by Homebrew cannot
be world-writable. This issue can be resolved by adding umask 002 to
your ~/.bash_profile
  echo 'umask 002' >> ~/.bash_profile

なんかワーニングが出ました。

言われたとおりに実行します。

echo 'umask 002' >> ~/.bash_profile

source ~/.bash_profile
$ brew doctor
Your system is ready to brew.

OKですね。

実際に使ってみる

それでは、実際に使ってみましょう。

Rubyのバージョン管理アプリrbenvを入れてみます。

==> Installing dependencies for rbenv: m4, gdbm, openssl, berkeley-db, libbsd, expat, perl, autoconf, pkg-config, ruby-build, libyaml, ncurses, readline, zlib and ruby
==> Installing rbenv dependency: m4
==> Downloading https://linuxbrew.bintray.com/bottles/m4-1.4.18.x86_64_linux.bottle.tar.gz
######################################################################## 100.0%
==> Pouring m4-1.4.18.x86_64_linux.bottle.tar.gz
   /home/linuxbrew/.linuxbrew/Cellar/m4/1.4.18: 13 files, 1.1MB
==> Installing rbenv dependency: gdbm
==> Downloading https://linuxbrew.bintray.com/bottles/gdbm-1.18.1.x86_64_linux.bottle.1.tar.gz
######################################################################## 100.0%
==> Pouring gdbm-1.18.1.x86_64_linux.bottle.1.tar.gz
   /home/linuxbrew/.linuxbrew/Cellar/gdbm/1.18.1: 40 files, 1MB
==> Installing rbenv dependency: openssl
==> Downloading https://linuxbrew.bintray.com/bottles/openssl-1.0.2q_2.x86_64_linux.bottle.tar.gz
######################################################################## 100.0%
==> Pouring openssl-1.0.2q_2.x86_64_linux.bottle.tar.gz
==> Downloading https://curl.haxx.se/ca/cacert-2019-01-23.pem
######################################################################## 100.0%
==> Caveats
A CA file has been bootstrapped using certificates from the SystemRoots
keychain. To add additional certificates (e.g. the certificates added in
the System keychain), place .pem files in
  /home/linuxbrew/.linuxbrew/etc/openssl/certs
.
.
.
.
.
.
By default non-brewed cpan modules are installed to the Cellar. If you wish
for your modules to persist across updates we recommend using `local::lib`.

You can set that up like this:
  PERL_MM_OPT="INSTALL_BASE=$HOME/perl5" cpan local::lib
  echo 'eval "$(perl -I$HOME/perl5/lib/perl5 -Mlocal::lib=$HOME/perl5)"' >> ~/.bash_profile
==> ruby
Emacs Lisp files have been installed to:
  /home/linuxbrew/.linuxbrew/share/emacs/site-lisp/ruby

インストールできました。

確認してみます。

rbenv -v

rbenv 1.1.1

入ってますね。

ついでに、Rubyも確認します。

ruby -v
ruby 2.6.1p33 (2019-01-30 revision 66950) [x86_64-linux]

入ってました。

irbでRubyが動くか試してみます。

irb

irb(main):001:0> puts 'hoge'
hoge
=> nil

動きましたね。


WSLにHomebrewをインストールして、rubyを動かしてみました。

Homebrewが使えるとなると、開発環境としてのWindows端末の魅力があがりますね。個人的にはmacOSの美しさと使いやすさは何ものにも変えられないですけどね。