<Mac> 色んなバージョンのRubyを使い分けるため「rbenv」をインストールする

やりたいこと

バージョンの異なるRubyを、一つの環境で使い分けたい。

インストール方法

Homebrewを使って、rbenvをインストールする。

$ brew install rbenv
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/core).
==> New Formulae
gmic          jsonlint      mfterm        minimesos     treefrog      ttygif        twarc
==> Updated Formulae
advancecomp              generate-json-schema     librdkafka               runit
ammonite-repl            git-annex                mailutils                rust
apache-spark             goaccess                 micropython              sassc
armor                    grails                   mkdocs                   scala
ats2-postiats            graphite2                mksh                     sdl_image
aws-sdk-cpp              gtk-gnutella             mlt                      shellinabox
awscli                   gtkextra                 msitools                 skinny
bash                     hebcal                   node                     stunnel
bitrise                  heroku                   node-build               sysdig
cake                     homebank                 oniguruma                telegraf
calabash                 imagemagick              open-scene-graph         terraform
citus                    ios-sim                  openssl@1.1              tmux
cmake                    jemalloc                 osmosis                  tomcat
conan                    jenkins                  passenger                typesafe-activator
consul                   jmxtrans                 ponyc                    vim
consul-backinator        jsdoc3                   pre-commit               voldemort
corectl                  kapacitor                pulledpork               wartremover
dpkg                     kotlin                   pyenv                    wdc
emscripten               kubernetes-cli           qbs                      webkitgtk
filebeat                 lbdb                     quantlib                 winetricks
flow                     libcec                   quazip                   youtube-dl
freetds                  libexosip                quilt                    z3
fuseki                   liblastfm                qxmpp
fzf                      libosip                  rclone
gdb                      libphonenumber           rhash
==> Deleted Formulae
bzr-explorer        coin                czmqpp              libechonest         libqglviewer

==> Installing dependencies for rbenv: autoconf, pkg-config, openssl, ruby-build
==> Installing rbenv dependency: autoconf
==> Downloading https://homebrew.bintray.com/bottles/autoconf-2.69.sierra.bottle.4.tar.gz
######################################################################## 100.0%
==> Pouring autoconf-2.69.sierra.bottle.4.tar.gz
==> Caveats
Emacs Lisp files have been installed to:
  /usr/local/share/emacs/site-lisp/autoconf
==> Summary
🍺  /usr/local/Cellar/autoconf/2.69: 70 files, 3.0M
==> Installing rbenv dependency: pkg-config
==> Downloading https://homebrew.bintray.com/bottles/pkg-config-0.29.1_2.sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring pkg-config-0.29.1_2.sierra.bottle.tar.gz
🍺  /usr/local/Cellar/pkg-config/0.29.1_2: 10 files, 627.4K
==> Installing rbenv dependency: openssl
==> Downloading https://homebrew.bintray.com/bottles/openssl-1.0.2j.sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring openssl-1.0.2j.sierra.bottle.tar.gz
==> Using the sandbox
==> 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
  /usr/local/etc/openssl/certs

and run
  /usr/local/opt/openssl/bin/c_rehash

This formula is keg-only, which means it was not symlinked into /usr/local.

Apple has deprecated use of OpenSSL in favor of its own TLS and crypto libraries

Generally there are no consequences of this for you. If you build your
own software and it requires this formula, you'll need to add to your
build variables:

    LDFLAGS:  -L/usr/local/opt/openssl/lib
    CPPFLAGS: -I/usr/local/opt/openssl/include
    PKG_CONFIG_PATH: /usr/local/opt/openssl/lib/pkgconfig

==> Summary
🍺  /usr/local/Cellar/openssl/1.0.2j: 1,695 files, 12M
==> Installing rbenv dependency: ruby-build
==> Downloading https://github.com/rbenv/ruby-build/archive/v20160913.tar.gz
==> Downloading from https://codeload.github.com/rbenv/ruby-build/tar.gz/v20160913
######################################################################## 100.0%
==> ./install.sh
🍺  /usr/local/Cellar/ruby-build/20160913: 304 files, 165.1K, built in 10 seconds
==> Installing rbenv
==> Downloading https://homebrew.bintray.com/bottles/rbenv-1.0.0.sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring rbenv-1.0.0.sierra.bottle.tar.gz
==> Caveats
Rbenv stores data under ~/.rbenv by default. If you absolutely need to
store everything under Homebrew's prefix, include this in your profile:
  export RBENV_ROOT=/usr/local/var/rbenv

To enable shims and autocompletion, run this and follow the instructions:
  rbenv init
==> Summary
🍺  /usr/local/Cellar/rbenv/1.0.0: 36 files, 62K

色んな記事に、ruby-buildをインストールすること、と書かれていますが、すでにインストールされていて不要でした。

$ brew install ruby-build
Updating Homebrew...
Warning: ruby-build-20160913 already installed

Rubyのインストール先の変更

デフォルトだと ~/.rbenv にインストールされます。ユーザーのホームディレクトリ配下なので特に問題ないのですが(ローカルの共通環境が汚れない)、ここではHomebrewでパッケージを管理するという方針なので、警告(上のCaveats)に従って変更しておきます。
rbenv + ruby-build はどうやって動いているのか - takatoshiono's blog を参考にさせてもらいました。

$ rbenv root
/Users/yamadatakaaki/.rbenv
$ echo 'export RBENV_ROOT=/usr/local/var/rbenv' >> ~/.bashrc
$ echo 'eval "$(rbenv init -)"' >> ~/.bashrc
$ source .bashrc
$ rbenv root
/usr/local/var/rbenv

試しに、Ruby 2.3.1をインストールしてみる

$ rbenv --version # => バージョン確認
rbenv 1.0.0
$ rbenv install --list # => インストール可能なバージョン一覧の表示
(省略)
$ rbenv install 2.3.1 # => rubyのインストール
(省略)
$ rbenv rehash # => rbenv の再読み込み
$ rbenv versions # => インストールあれているすべてのRubyを表示
 * system (set by /usr/local/var/rbenv/version)
  2.3.1
$ rbenv global 2.3.1 # => defaultで使うrubyのバージョン
$ ruby --version
ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-darwin16]

<Mac> パッケージ管理ツール「Homebrew」を入れる

やりたいこと

  • App Storeにないパッケージもインストールしたい
  • ローカル環境をなるべく汚したくない(任意の場所にインストールしたい)
  • 依存関係を自動的に解決してほしい
  • 自分の環境に最適化させたいときはビルドもしたい

Homebrewの特徴

MacportsFinkなどでもできるようですが、今回はHomebrewを使います。
cf) Homebrew — macOS 用パッケージマネージャー

インストール方法

XcodeとCommand Line Tools for Xcodeが必要です。

Xcode

App Storeから「Xcode」で検索してインストールします。
Xcodeのバージョンを確認します。

$ xcodebuild -version
Xcode 8.1
Build version 8B62

Command Line Tools for Xcode

以下にアクセスして、OSのバージョンにあったものを入手してインストールしてください。Apple IDが必要です。私の場合は、macOS Sierra(バージョン10.12.1)だったので、「Command Line Tools (macOS 10.12) for Xcode 8.1」をインストールしました。

https://developer.apple.com/downloads/index.action

Command Line Toolsのバージョンを確認します。

$ pkgutil --pkg-info=com.apple.pkg.CLTools_Executables
package-id: com.apple.pkg.CLTools_Executables
version: 8.1.0.0.1.1476494586
volume: /
location: /
install-time: 1478539344
groups: com.apple.FindSystemFiles.pkg-group 

Homebrew

$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
==> This script will install:
/usr/local/bin/brew
/usr/local/share/doc/homebrew
/usr/local/share/man/man1/brew.1
/usr/local/share/zsh/site-functions/_brew
/usr/local/etc/bash_completion.d/brew
/usr/local/Homebrew
==> The following new directories will be created:
/usr/local/Cellar
/usr/local/Homebrew
/usr/local/Frameworks
/usr/local/bin
/usr/local/etc
/usr/local/include
/usr/local/lib
/usr/local/opt
/usr/local/sbin
/usr/local/share
/usr/local/share/zsh
/usr/local/share/zsh/site-functions
/usr/local/var

Press RETURN to continue or any other key to abort
==> /usr/bin/sudo /bin/mkdir -p /usr/local/Cellar /usr/local/Homebrew /usr/local/Frameworks /usr/local/bin /usr/local/etc /usr/local/include /usr/local/lib /usr/local/opt /usr/local/sbin /usr/local/share /usr/local/share/zsh /usr/local/share/zsh/site-functions /usr/local/var
Password:
==> /usr/bin/sudo /bin/chmod g+rwx /usr/local/Cellar /usr/local/Homebrew /usr/local/Frameworks /usr/local/bin /usr/local/etc /usr/local/include /usr/local/lib /usr/local/opt /usr/local/sbin /usr/local/share /usr/local/share/zsh /usr/local/share/zsh/site-functions /usr/local/var
==> /usr/bin/sudo /bin/chmod 755 /usr/local/share/zsh /usr/local/share/zsh/site-functions
==> /usr/bin/sudo /usr/sbin/chown yamadatakaaki /usr/local/Cellar /usr/local/Homebrew /usr/local/Frameworks /usr/local/bin /usr/local/etc /usr/local/include /usr/local/lib /usr/local/opt /usr/local/sbin /usr/local/share /usr/local/share/zsh /usr/local/share/zsh/site-functions /usr/local/var
==> /usr/bin/sudo /usr/bin/chgrp admin /usr/local/Cellar /usr/local/Homebrew /usr/local/Frameworks /usr/local/bin /usr/local/etc /usr/local/include /usr/local/lib /usr/local/opt /usr/local/sbin /usr/local/share /usr/local/share/zsh /usr/local/share/zsh/site-functions /usr/local/var
==> /usr/bin/sudo /bin/mkdir -p /Users/yamadatakaaki/Library/Caches/Homebrew
==> /usr/bin/sudo /bin/chmod g+rwx /Users/yamadatakaaki/Library/Caches/Homebrew
==> /usr/bin/sudo /usr/sbin/chown yamadatakaaki /Users/yamadatakaaki/Library/Caches/Homebrew
==> Downloading and installing Homebrew...
HEAD is now at 42bb0f8 Merge pull request #1453 from ddbeck/revise-troubleshooting
==> Homebrew has enabled anonymous aggregate user behaviour analytics
Read the analytics documentation (and how to opt-out) here:
  https://git.io/brew-analytics
==> Tapping homebrew/core
Tapped 3627 formulae (3,775 files, 9.3M)
Already up-to-date.
==> Installation successful!
==> Next steps
Run `brew help` to get started
Further documentation: https://git.io/brew-docs

正常にインストールされたことを確認します。

$ brew doctor
Your system is ready to brew.

設定を調べてみます。

$ brew --config
HOMEBREW_VERSION: 1.1.0
ORIGIN: https://github.com/Homebrew/brew
HEAD: b7abd33475b98598b414f9c4ee2e20249855437b
Last commit: 24 hours ago
Core tap ORIGIN: https://github.com/Homebrew/homebrew-core
Core tap HEAD: 347ff4c58b374b7bc641838067772d2a87c1506d
Core tap last commit: 37 minutes ago
HOMEBREW_PREFIX: /usr/local
HOMEBREW_REPOSITORY: /usr/local/Homebrew
HOMEBREW_CELLAR: /usr/local/Cellar
HOMEBREW_BOTTLE_DOMAIN: https://homebrew.bintray.com
CPU: octa-core 64-bit haswell
Homebrew Ruby: 2.0.0-p648
Clang: 8.0 build 800
Git: 2.9.3 => /Applications/Xcode.app/Contents/Developer/usr/bin/git
Perl: /usr/bin/perl
Python: /usr/bin/python
Ruby: /usr/bin/ruby => /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby
Java: N/A
macOS: 10.12.1-x86_64
Xcode: 8.1
CLT: 8.1.0.0.1.1476494586
X11: N/A

<Mac> パスワード付きでファイルやフォルダを圧縮する

やりたいこと

ファイルやフォルダをパスワード付きでzip化する。

やり方

パスワードの入力を求められます。

ファイルの場合

$ zip -e [zip化後のファイル名] [zip化したいファイル名]

フォルダの場合

$ zip -e -r [zip化後のファイル名] [zip化したいフォルダ名]

解凍

解凍するにはunzipコマンドを使います。パスワードを求められます。

$ unzip [zip化後のファイル名]

<Mac> 起動時のジャーンという音を消す

やりたいこと

Macを起動するときの「ジャーン」という音を消す。

やり方

ファームウェアの変数を操作するnvramコマンドを使います。
Macを再起動すると反映されます。

現在の値を確認する

$ nvram -p | grep SystemAudioVolume
SystemAudioVolume	G

変更する

sudoを使います。パスワードが求められます。

$ sudo nvram SystemAudioVolume=%80

<Mac> Terminalのプロンプトを変更する

やりたいこと

Terminalのプロンプトをシンプルにする。

before

yamadataro-no-MacBook-Pro:~ yamadataro$ 

after

~ $ 

やり方

Terminal起動時に、.bash_profileが読み込まれ、そこから.bashrcを読み込む流れになります。
ただ、デフォルトの状態では、Macには、.bash_profileも.bashrcもないので、ファイルを作りながら進めていきます。

.bashrc

$ vi .bashrc

中身は、例えばこのように書きます。

export PS1='\W $ '

.bash_profile

$ vi .bash_profile

中身はこのように書きます。

if [ -f ~/.bashrc ] ; then
. ~/.bashrc
fi