homebrewなど/usr/localの中身を再インストール

homebrew がうまく動かなくなり、思わず

/usr/local $ git clean -f -d
Removing Cellar/
Removing Frameworks/
Removing Library/
Removing bin/
Removing clamXav/
Removing etc/
Removing foreman/
Removing heroku/
Removing include/
Removing lib/
Removing man/
Removing opt/
Removing share/
Removing var/

などとしてしまい…にっちもさっちもいかなくなりましたorz
ちなみに git clean -f -d ってのは…

GIT-CLEAN(1)                      Git Manual                      GIT-CLEAN(1)

NAME
       git-clean - Remove untracked files from the working tree

OPTIONS
       -d
           Remove untracked directories in addition to untracked files. If an
           untracked directory is managed by a different Git repository, it is
           not removed by default. Use -f option twice if you really want to
           remove such a directory.

       -f, --force
           If the Git configuration variable clean.requireForce is not set to
           false, git clean will refuse to run unless given -f, -n or -i.

全部消しちゃいますよねorz
で残ったのは…

/usr/local $ ls
README.md	index.html

ほとんどない。いろいろ消えたよorz
もちろん rbenv も消えて Ruby のパスがおかしくなったので homebrew インストールのコマンドが動かない。

homebrewインストール

システムの Ruby を直接指定して使います。

$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
==> This script will install:
/usr/local/bin/brew
/usr/local/Library/...
/usr/local/share/man/man1/brew.1

Press RETURN to continue or any other key to abort
==> /usr/bin/sudo /bin/chmod g+rwx /Library/Caches/Homebrew
Password:
==> Downloading and installing Homebrew...
remote: Counting objects: 199592, done.
remote: Compressing objects: 100% (54014/54014), done.
remote: Total 199592 (delta 144445), reused 199527 (delta 144396)
Receiving objects: 100% (199592/199592), 41.30 MiB | 2.05 MiB/s, done.
Resolving deltas: 100% (144445/144445), done.
From https://github.com/Homebrew/homebrew
 * [new branch]      master     -> origin/master
HEAD is now at 5fb2fb8 dnscrypt-proxy 1.4.1
==> Installation successful!
==> Next steps
Run `brew doctor` before you install anything
Run `brew help` to get started

rbenvとruby-buildをインストール

$ brew install rbenv
==> Downloading https://github.com/sstephenson/rbenv/archive/v0.4.0.tar.gz
Already downloaded: /Library/Caches/Homebrew/rbenv-0.4.0.tar.gz
==> Caveats
To use Homebrew's directories rather than ~/.rbenv add to your profile:
  export RBENV_ROOT=/usr/local/var/rbenv

To enable shims and autocompletion add to your profile:
  if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi
==> Summary
🍺  /usr/local/Cellar/rbenv/0.4.0: 31 files, 152K, built in 2 seconds
$ brew install ruby-build
==> Installing dependencies for ruby-build: autoconf, pkg-config, openssl
==> Installing ruby-build dependency: autoconf
==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/autoconf-2.
######################################################################## 100.0%
==> Pouring autoconf-2.69.mavericks.bottle.1.tar.gz
🍺  /usr/local/Cellar/autoconf/2.69: 69 files, 2.0M
==> Installing ruby-build dependency: pkg-config
==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/pkg-config-
Already downloaded: /Library/Caches/Homebrew/pkg-config-0.28.mavericks.bottle.2.tar.gz
==> Pouring pkg-config-0.28.mavericks.bottle.2.tar.gz
🍺  /usr/local/Cellar/pkg-config/0.28: 10 files, 604K
==> Installing ruby-build dependency: openssl
==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/openssl-1.0
######################################################################## 100.0%
==> Pouring openssl-1.0.1i.mavericks.bottle.3.tar.gz
==> Caveats
A CA file has been bootstrapped using certificates from the system
keychain. To add additional certificates, 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.

Mac OS X already provides this software and installing another version in
parallel can cause all kinds of trouble.

The OpenSSL provided by OS X is too old for some software.

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

==> Summary
🍺  /usr/local/Cellar/openssl/1.0.1i: 430 files, 15M
==> Installing ruby-build
==> Downloading https://github.com/sstephenson/ruby-build/archive/v20140919.tar.
######################################################################## 100.0%
==> ./install.sh
🍺  /usr/local/Cellar/ruby-build/20140919: 128 files, 548K, built in 3 seconds

ruby動作確認

$ . .bash_profile 
$ which ruby
/Users/riocampos/.rbenv/shims/ruby
$ ruby -v
ruby 1.9.3p484 (2013-11-22 revision 43786) [x86_64-darwin13.1.0]

しかし

$ gem list
/Users/riocampos/.rbenv/versions/1.9.3-p484/lib/ruby/1.9.1/yaml.rb:84:in `<top (required)>':
It seems your ruby installation is missing psych (for YAML output).
To eliminate this warning, please install libyaml and reinstall your ruby.

とか出てくるし

$ pry
You're running a version of ruby with no Readline support
Please `gem install rb-readline` or recompile ruby --with-readline.

とか言ってきます。

libyamlインストール
$ brew search libyaml
libyaml
$ brew install libyaml
==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/libyaml-0.1
Already downloaded: /Library/Caches/Homebrew/libyaml-0.1.6.mavericks.bottle.tar.gz
==> Pouring libyaml-0.1.6.mavericks.bottle.tar.gz
Error: The `brew link` step did not complete successfully
The formula built, but is not symlinked into /usr/local
Could not symlink include/yaml.h
/usr/local/include is not writable.

You can try again using:
  brew link libyaml
==> Summary
&#127866;  /usr/local/Cellar/libyaml/0.1.6: 7 files, 348K
$ brew link libyaml
Linking /usr/local/Cellar/libyaml/0.1.6... 
Error: Could not symlink include/yaml.h
/usr/local/include is not writable.
$ sudo chown -R $USER /usr/local/include
Password:
chown: /usr/local/include: No such file or directory
$ mkdir /usr/local/include
mkdir: /usr/local/include: Permission denied
$ sudo mkdir /usr/local/include
$ sudo chown -R $USER /usr/local/include
$ brew link libyaml
Linking /usr/local/Cellar/libyaml/0.1.6... 
Error: Could not symlink lib/libyaml-0.2.dylib
/usr/local/lib is not writable.

ここで brew doctor してみると /usr/local のパーミッションが悪いと返されたので

$ sudo chown -R $USER /usr/local
$ brew link libyaml
Linking /usr/local/Cellar/libyaml/0.1.6... 5 symlinks created

としました。ということで libyaml に関するエラーメッセージは出なくなりました。
でも…どうやら Ruby は入れ直した方が良さそう。

Ruby再インストール

まず、自分で書いたこれに従って rbenv-gem-rehash を入れておきます。
Ruby - rbenv を使っているなら rbenv-gem-rehash を使おう - Qiita

$ brew install rbenv-gem-rehash
==> Downloading https://github.com/sstephenson/rbenv-gem-rehash/archive/v1.0.0.t
Already downloaded: /Library/Caches/Homebrew/rbenv-gem-rehash-1.0.0.tar.gz
==> Downloading https://github.com/sstephenson/rbenv-gem-rehash/commit/0756890cf
Already downloaded: /Library/Caches/Homebrew/rbenv-gem-rehash--patch-17877b728eda6eaddc199bcd5935fcea757a1c63.diff
==> Patching
patching file README.md
Hunk #1 succeeded at 23 (offset -2 lines).
patching file etc/rbenv.d/exec/~gem-rehash.bash
patching file gems/rbenv-gem-rehash-1.0.0/lib/rubygems_plugin.rb
patching file rubygems_plugin.rb
patching file specifications/rbenv-gem-rehash-1.0.0.gemspec
==> Caveats
If the GEM_PATH environment variable is undefined, rbenv-gem-rehash must
first execute the gem env gempath command to retrieve RubyGems' default path
so that it can can append to the path rather than override it. This can take
a while--from a few hundred milliseconds on MRI to several seconds on
JRuby--so the default path for the current Ruby version is cached to the
filesystem the first time it is retrieved.
==> Summary
&#127866;  /usr/local/Cellar/rbenv-gem-rehash/1.0.0: 7 files, 24K, built in 2 seconds

また homebrew から readline をインストール*1

$ brew install readline
==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/readline-6.
######################################################################## 100.0%
==> Pouring readline-6.3.8.mavericks.bottle.tar.gz
==> Caveats
This formula is keg-only, which means it was not symlinked into /usr/local.

Mac OS X provides similar software, and installing this software in
parallel can cause all kinds of trouble.

OS X provides the BSD libedit library, which shadows libreadline.
In order to prevent conflicts when programs look for libreadline we are
defaulting this GNU Readline installation to keg-only.

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/readline/lib
    CPPFLAGS: -I/usr/local/opt/readline/include

==> Summary
&#127866;  /usr/local/Cellar/readline/6.3.8: 40 files, 2.1M

上書きなので -f オプションを付けて実行。また先ほどの readline はkeg-only なので --with-readline-dir でパスを指定します。

$ CONFIGURE_OPTS="--enable-shared --with-readline-dir=`brew --prefix readline` --with-openssl-dir=`brew --prefix openssl`" rbenv install -f 1.9.3-p484

clamXavインストール

ClamXav
からダウンロードしたインストーラをインストールして実行。

heroku toolbeltインストール

Heroku Toolbelt
からダウンロードしたインストーラをインストールして実行。

OSXFUSEインストール

MacFusionは入ったままだがOSXFUSEが消えたのでインストール。
Home - FUSE for OS X
からダウンロードしたインストーラをインストールして実行。

ffmpegインストール

$ brew install ffmpeg --with-openssl --with-rtmpdump --with-libvorbis
==> Installing dependencies for ffmpeg: texi2html, yasm, x264, faac, lame
==> Installing ffmpeg dependency: texi2html
==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/texi2html-1
######################################################################## 100.0%
==> Pouring texi2html-1.82.mavericks.bottle.tar.gz
&#127866;  /usr/local/Cellar/texi2html/1.82: 107 files, 2.2M
==> Installing ffmpeg dependency: yasm
==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/yasm-1.3.0.
######################################################################## 100.0%
==> Pouring yasm-1.3.0.mavericks.bottle.tar.gz
&#127866;  /usr/local/Cellar/yasm/1.3.0: 44 files, 3.4M
==> Installing ffmpeg dependency: x264
==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/x264-r2455.
######################################################################## 100.0%
==> Pouring x264-r2455.mavericks.bottle.tar.gz
&#127866;  /usr/local/Cellar/x264/r2455: 9 files, 3.4M
==> Installing ffmpeg dependency: faac
==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/faac-1.28.m
######################################################################## 100.0%
==> Pouring faac-1.28.mavericks.bottle.tar.gz
&#127866;  /usr/local/Cellar/faac/1.28: 13 files, 720K
==> Installing ffmpeg dependency: lame
==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/lame-3.99.5
######################################################################## 100.0%
==> Pouring lame-3.99.5.mavericks.bottle.tar.gz
&#127866;  /usr/local/Cellar/lame/3.99.5: 25 files, 2.1M
==> Installing ffmpeg dependency: xvid
==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/xvid-1.3.2.
######################################################################## 100.0%
==> Pouring xvid-1.3.2.mavericks.bottle.tar.gz
&#127866;  /usr/local/Cellar/xvid/1.3.2: 9 files, 1.3M
==> Installing ffmpeg dependency: libogg
==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/libogg-1.3.
Already downloaded: /Library/Caches/Homebrew/libogg-1.3.2.mavericks.bottle.tar.gz
==> Pouring libogg-1.3.2.mavericks.bottle.tar.gz
&#127866;  /usr/local/Cellar/libogg/1.3.2: 95 files, 672K
==> Installing ffmpeg dependency: libvorbis
==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/libvorbis-1
######################################################################## 100.0%
==> Pouring libvorbis-1.3.4.mavericks.bottle.tar.gz
&#127866;  /usr/local/Cellar/libvorbis/1.3.4: 162 files, 2.7M
==> Installing ffmpeg dependency: rtmpdump
==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/rtmpdump-2.
######################################################################## 100.0%
==> Pouring rtmpdump-2.4+20131018_1.mavericks.bottle.1.tar.gz
&#127866;  /usr/local/Cellar/rtmpdump/2.4+20131018_1: 18 files, 488K
==> Installing ffmpeg
==> Downloading https://ffmpeg.org/releases/ffmpeg-2.3.3.tar.bz2
######################################################################## 100.0%
==> ./configure --prefix=/usr/local/Cellar/ffmpeg/2.3.3 --enable-shared --enable
==> make install
&#127866;  /usr/local/Cellar/ffmpeg/2.3.3: 196 files, 40M, built in 3.8 minutes

PhantomJSインストール

$ brew install phantomjs
==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/phantomjs-1
######################################################################## 100.0%
==> Pouring phantomjs-1.9.7_1.mavericks.bottle.tar.gz
&#127866;  /usr/local/Cellar/phantomjs/1.9.7_1: 104 files, 34M

Python再インストール

あとはPythonとかだけど、ひとまず後回しにしよう。
git の表示に Python を使っていたので入れざるを得ない…。

pip コマンドでインストールします。

$ sudo pip install diff-highlight

~/.gitconfig の diff-highlight 設定部分↓

[pager]
	log  = diff-highlight | less -+S
	show = diff-highlight | less -+S
	diff = diff-highlight | less -+S
$ brew install pyenv
==> Downloading https://github.com/yyuu/pyenv/archive/v20140924.tar.gz
######################################################################## 100.0%
==> Caveats
To enable shims and autocompletion add to your profile:
  if which pyenv > /dev/null; then eval "$(pyenv init -)"; fi

To use Homebrew's directories rather than ~/.pyenv add to your profile:
  export PYENV_ROOT=/usr/local/opt/pyenv
==> Summary
&#127866;  /usr/local/Cellar/pyenv/20140924: 292 files, 2.6M, built in 4 seconds
$ brew install pyenv-pip-rehash
==> Downloading https://github.com/yyuu/pyenv-pip-rehash/archive/v0.0.4.tar.gz
######################################################################## 100.0%
==> ./install.sh
&#127866;  /usr/local/Cellar/pyenv-pip-rehash/0.0.4: 6 files, 24K, built in 4 seconds
$ pyenv install 2.7.6
pyenv: /Users/riocampos/.pyenv/versions/2.7.6 already exists
continue with installation? (y/N) y
Downloading Python-2.7.6.tgz...
-> http://yyuu.github.io/pythons/99c6860b70977befa1590029fae092ddb18db1d69ae67e8b9385b66ed104ba58
Installing Python-2.7.6...
patching file ./Modules/readline.c
Installing setuptools from https://bootstrap.pypa.io/ez_setup.py...
Installing pip from https://bootstrap.pypa.io/get-pip.py...
Installed Python-2.7.6 to /Users/riocampos/.pyenv/versions/2.7.6

$ pyenv install 3.4.0
pyenv: /Users/riocampos/.pyenv/versions/3.4.0 already exists
continue with installation? (y/N) y
Downloading Python-3.4.0.tgz...
-> http://yyuu.github.io/pythons/d2c83ea0217769a73e8b1ee33ffbca814903f8568e30f8d13e68e3d1f743449c
Installing Python-3.4.0...
Installed Python-3.4.0 to /Users/riocampos/.pyenv/versions/3.4.0

$ pyenv global 2.7.6 3.4.0
$ pyenv versions
  system
* 2.7.6 (set by /Users/riocampos/.pyenv/version)
* 3.4.0 (set by /Users/riocampos/.pyenv/version)

*1:実は忘れていましたw