nmtysh.log

Tech系のネタや日々の独り言などを書いています。

MacでRuby on Railsの開発環境を構築する

※この記事はQiitaにも投稿しています。

Ruby on Rails チュートリアル を行うにあたって仮想マシン内では無くMac上に環境を構築することにしました。

0. 前提

手元の環境には昔この辺の手順でインストールされた名残があったのでそのクリーンアップをしておきます。

$ rm -rf ~/.rbenv

1. rbenv, ruby-buildのインストール

$ brew update
$ brew install rbenv ruby-build
==> Installing dependencies for rbenv: autoconf, ruby-build
==> Installing rbenv dependency: autoconf
==> Downloading https://homebrew.bintray.com/bottles/autoconf-2.69.el_capitan.bo
Already downloaded: /Users/*****/Library/Caches/Homebrew/autoconf-2.69.el_capitan.bottle.4.tar.gz
==> Pouring autoconf-2.69.el_capitan.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: ruby-build
==> Downloading https://github.com/rbenv/ruby-build/archive/v20160602.tar.gz
==> Downloading from https://codeload.github.com/rbenv/ruby-build/tar.gz/v201606
######################################################################## 100.0%
==> ./install.sh
🍺  /usr/local/Cellar/ruby-build/20160602: 282 files, 155.5K, built in 6 seconds
==> Installing rbenv
==> Downloading https://homebrew.bintray.com/bottles/rbenv-1.0.0.el_capitan.bott
######################################################################## 100.0%
==> Pouring rbenv-1.0.0.el_capitan.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, 62.0K

$ rbenv init

.bashrcに rbenv init を追記します。

$ echo 'eval "$(rbenv init -)"' >> ~/.bashrc
$ source ~/.bashrc

2. rubyのインストール

現時点で一番最新の 2.3.1 をインストールします。
rbenv install --list で利用できるバージョンの一覧が出力されます。

$ rbenv install 2.3.1
 rbenv versions
* system (set by /Users/*****/.rbenv/version)
  2.3.1

$ rbenv local 2.3.1
$ ruby --version
ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-darwin15]

3. bundlerを使ってrailsのインストール

チュートリアルだと ~/.rbenv の下にgemをインストールすることになるのでbundlerを使ってカレントディレクトリにrailsをインストールします。

$ gem install bundler
Fetching: bundler-1.13.2.gem (100%)
Successfully installed bundler-1.13.2
1 gem installed

$ cat << _EOT_ > Gemfile
source 'https://rubygems.org'
gem 'rails', '4.2.2'
_EOT_

$ bundle install --path vendor/bundle
Fetching gem metadata from https://rubygems.org/..........
Fetching version metadata from https://rubygems.org/..
Fetching dependency metadata from https://rubygems.org/.
Resolving dependencies...
Installing rake 11.3.0
Installing i18n 0.7.0
Using json 1.8.3
Installing minitest 5.9.1
Installing thread_safe 0.3.5
Installing builder 3.2.2
Installing erubis 2.7.0
Installing mini_portile2 2.1.0
Installing rack 1.6.4
Installing mime-types-data 3.2016.0521
Installing arel 6.0.3
Using bundler 1.13.2
Installing concurrent-ruby 1.0.2
Installing thor 0.19.1
Installing tzinfo 1.2.2
Installing nokogiri 1.6.8.1 with native extensions
Installing rack-test 0.6.3
Installing mime-types 3.1
Installing sprockets 3.7.0
Installing activesupport 4.2.2
Installing loofah 2.0.3
Installing mail 2.6.4
Installing rails-deprecated_sanitizer 1.0.3
Installing globalid 0.3.7
Installing activemodel 4.2.2
Installing rails-html-sanitizer 1.0.3
Installing rails-dom-testing 1.0.7
Installing activejob 4.2.2
Installing activerecord 4.2.2
Installing actionview 4.2.2
Installing actionpack 4.2.2
Installing actionmailer 4.2.2
Installing railties 4.2.2
Installing sprockets-rails 3.2.0
Installing rails 4.2.2
Bundle complete! 1 Gemfile dependency, 35 gems now installed.
Bundled gems are installed into ./vendor/bundle.

$ bundle exec rails -v
Rails 4.2.2

後は rails コマンドを適宜 bundle exec rails に読み替えていけば良さそうです。

x. 参考

IAMで請求情報は表示できるが請求先は表示できないようにする

※この記事はQiitaにも投稿しています。

IAMユーザーにも請求情報の表示を許可する

請求の情報およびツールへのアクセス許可 - AWS 請求情報とコスト管理 を参考にルートアカウント認証情報でログインしてアカウントの"IAM User Access to Billing Information"を有効にします。

支払先情報を隠す

IAMユーザーで請求情報を見られるようにした場合、AdministratorAccessポリシーが適用されていると支払先情報までアクセスできてしまいます。
請求金額などは見せたいが支払先情報の表示や変更を許可したくない場合はインラインポリシーで個別にブロックします。

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "Stmt1474516683000",
            "Effect": "Deny",
            "Action": [
                "aws-portal:ModifyAccount",
                "aws-portal:ModifyBilling",
                "aws-portal:ModifyPaymentMethods",
                "aws-portal:ViewAccount",
                "aws-portal:ViewBudget",
                "aws-portal:ViewPaymentMethods"
            ],
            "Resource": [
                "*"
            ]
        }
    ]
}

AdministratorAccessポリシーだったらポリシールールをIAMユーザーが自分で変更できちゃうよね。というツッコミは無しでお願いします。
それは別途ポリシーでブロックしてください。

参考

親知らずを抜歯した話

遂に親知らずの1本目が生えてきました。
素人目には普通に生えてきたのでホッとしていたのですが定期的に通っている歯医者で

  • 他の歯に干渉しているので他の歯がダメになる可能性がある
  • (上下の意味で)対になる歯が無い(生えてない)のでかみ合わせに問題がある

と言われて先日抜歯をしてきました。

麻酔を打って(こぼれた麻酔はかなり苦かったです)、あっさり抜歯。

抜歯の時間よりその後の縫合処置が長かった……
おまけに麻酔を打った辺りから喉にタンがかかり呼吸しづらい状態になってそちらも辛かったです。

口の中で作業しているので、何も言うことができません。
顔に近い所で作業されるのでほとんど目を閉じるのですが音と口の中の感覚はあるので、それも怖さを引き立てる要因だったり。

麻酔が徐々に切れだすとかなりの痛みが出てきました。物事を考えるのが億劫になるぐらいの痛みです。
痛み止めは必須ですね。飲むとかなり楽になりました。

数日痛むのを覚悟していましたが、2日目からは思っていたよりも痛みはなく(時折軽く痛みを感じる程度)助かりました。

縫合した糸の抜糸*1も済んだので後は傷口が治るのを待つだけです。
まだ暫くの間は傷口をかばいながらなので食事に時間がかかる上、歯磨き時に注意が必要ですね。

*1:抜歯と抜糸が紛らわしい……

Time Machineでローカルスナップショットのみを有効化する

※この記事はQiitaにも投稿しています。

(2016/09/30追記)※2016/09/26に公開された TimeMachineEditor 4.5.2 だと下記の操作を行ってもバックアップ実行時にTime Machineの設定が自動的にオフになり、ローカルスナップショットも同時に無効化されてしまいます!

TimeMachineEditorでTime Machineの実行間隔を調整しています。
そのためTime Machine自体のスケジューラは無効にするためTimeMachineをオフにしていました。

ただ、システム環境設定のTime Machine設定からTime Machineをオフにするとローカルスナップショットも無効になってしまいます。

外付けHDDあるいはネットワークドライブ上のTime Machineだけではなくローカルスナップショットも利用したいため有効にする方法を調べました。

OS X 10.11.6 で確認しました。

コマンドでTime Machineを無効化する

システム環境設定のTime Machine設定がオンの状態でコマンドでTime Machineをオフにします。

$ sudo tmutil disable

そうするとローカルスナップショットは有効のままTime Machineがオフになりました。

コマンドでローカルスナップショットを有効化する

あるいはコマンドでローカルスナップショットを有効化します。
(あらかじめTime Machineはオフにしておきます)

$ sudo tmutil enablelocal

こちらも同じようにローカルスナップショットが有効になりました。

注意

システム環境設定のTime Machine設定でTime Machineのオン/オフを切り替えるとオフにした時に再びローカルスナップショットが無効になってしまいます。

これ表示上はローカルスナップショットが有効のように見えるだけで実は動いてないってことはないよね?
→無事にローカルスナップショットが作成されてました

MacにHomebrewでComposerをインストールする(備忘録)

※この記事はQiitaにも投稿しています。

Atomからphpmdを呼び出して自動チェックさせたりするついでにComposerも導入して仮想マシンを経由せずにMac上で直接使える様にしてみました。

0. 環境

1. Install

$ brew install Composer
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/core).
==> Updated Formulae
infer
Error: No available formula with the name "composer"
==> Searching for similarly named formulae...
Error: No similarly named formulae found.
==> Searching taps...
These formulae were found in taps:
homebrew/completions/composer-completion  homebrew/php/composer
To install one of them, run (for example):
  brew install homebrew/completions/composer-completion

パッケージ名が違ってた。 composer-completion もあるようなのでついでにインストールする。

$ brew install homebrew/php/composer homebrew/completions/composer-completion
==> Tapping homebrew/php
Cloning into '/usr/local/Library/Taps/homebrew/homebrew-php'...
remote: Counting objects: 642, done.
remote: Compressing objects: 100% (395/395), done.
remote: Total 642 (delta 345), reused 499 (delta 245), pack-reused 0
Receiving objects: 100% (642/642), 303.89 KiB | 213.00 KiB/s, done.
Resolving deltas: 100% (345/345), done.
Checking connectivity... done.
Tapped 617 formulae (657 files, 1.2M)
==> Tapping homebrew/completions
Cloning into '/usr/local/Library/Taps/homebrew/homebrew-completions'...
remote: Counting objects: 41, done.
remote: Compressing objects: 100% (36/36), done.
remote: Total 41 (delta 8), reused 21 (delta 5), pack-reused 0
Unpacking objects: 100% (41/41), done.
Checking connectivity... done.
Tapped 36 formulae (101 files, 67.9K)
==> Installing composer from homebrew/php
==> Installing dependencies for homebrew/php/composer: libpng, freetype, icu4c, jpeg, libxml2, unixodbc, readline, php70
==> Installing homebrew/php/composer dependency: libpng
==> Downloading https://homebrew.bintray.com/bottles/libpng-1.6.24.el_capitan.bottle.tar.gz
######################################################################## 100.0%
==> Pouring libpng-1.6.24.el_capitan.bottle.tar.gz
🍺  /usr/local/Cellar/libpng/1.6.24: 26 files, 1.2M
==> Installing homebrew/php/composer dependency: freetype
==> Downloading https://homebrew.bintray.com/bottles/freetype-2.6.5.el_capitan.bottle.tar.gz
######################################################################## 100.0%
==> Pouring freetype-2.6.5.el_capitan.bottle.tar.gz
🍺  /usr/local/Cellar/freetype/2.6.5: 61 files, 2.5M
==> Installing homebrew/php/composer dependency: icu4c
==> Downloading https://homebrew.bintray.com/bottles/icu4c-57.1.el_capitan.bottle.tar.gz
######################################################################## 100.0%
==> Pouring icu4c-57.1.el_capitan.bottle.tar.gz
==> Caveats
This formula is keg-only, which means it was not symlinked into /usr/local.

OS X provides libicucore.dylib (but nothing else).

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

==> Summary
🍺  /usr/local/Cellar/icu4c/57.1: 265 files, 65.0M
==> Installing homebrew/php/composer dependency: jpeg
==> Downloading https://homebrew.bintray.com/bottles/jpeg-8d.el_capitan.bottle.2.tar.gz
######################################################################## 100.0%
==> Pouring jpeg-8d.el_capitan.bottle.2.tar.gz
🍺  /usr/local/Cellar/jpeg/8d: 19 files, 713.8K
==> Installing homebrew/php/composer dependency: libxml2
==> Downloading https://homebrew.bintray.com/bottles/libxml2-2.9.4.el_capitan.bottle.tar.gz
######################################################################## 100.0%
==> Pouring libxml2-2.9.4.el_capitan.bottle.tar.gz
==> Caveats
This formula is keg-only, which means it was not symlinked into /usr/local.

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

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

==> Summary
🍺  /usr/local/Cellar/libxml2/2.9.4: 276 files, 9.8M
==> Installing homebrew/php/composer dependency: unixodbc
==> Downloading https://homebrew.bintray.com/bottles/unixodbc-2.3.4.el_capitan.bottle.tar.gz
######################################################################## 100.0%
==> Pouring unixodbc-2.3.4.el_capitan.bottle.tar.gz
🍺  /usr/local/Cellar/unixodbc/2.3.4: 39 files, 952.4K
==> Installing homebrew/php/composer dependency: readline
==> Downloading https://homebrew.bintray.com/bottles/readline-6.3.8.el_capitan.bottle.tar.gz
######################################################################## 100.0%
==> Pouring readline-6.3.8.el_capitan.bottle.tar.gz
==> Caveats
This formula is keg-only, which means it was not symlinked into /usr/local.

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
🍺  /usr/local/Cellar/readline/6.3.8: 46 files, 2.0M
==> Installing homebrew/php/composer dependency: php70
==> Downloading https://homebrew.bintray.com/bottles-php/php70-7.0.10_1.el_capitan.bottle.1.tar.gz
######################################################################## 100.0%
==> Pouring php70-7.0.10_1.el_capitan.bottle.1.tar.gz
==> Caveats
To enable PHP in Apache add the following to httpd.conf and restart Apache:
    LoadModule php7_module    /usr/local/opt/php70/libexec/apache2/libphp7.so

    <FilesMatch .php$>
        SetHandler application/x-httpd-php
    </FilesMatch>

Finally, check DirectoryIndex includes index.php
    DirectoryIndex index.php index.html

The php.ini file can be found in:
    /usr/local/etc/php/7.0/php.ini

✩✩✩✩ Extensions ✩✩✩✩

If you are having issues with custom extension compiling, ensure that
you are using the brew version, by placing /usr/local/bin before /usr/sbin in your PATH:

      PATH="/usr/local/bin:$PATH"

PHP70 Extensions will always be compiled against this PHP. Please install them
using --without-homebrew-php to enable compiling against system PHP.

✩✩✩✩ PHP CLI ✩✩✩✩

If you wish to swap the PHP you use on the command line, you should add the following to ~/.bashrc,
~/.zshrc, ~/.profile or your shell's equivalent configuration file:

      export PATH="$(brew --prefix homebrew/php/php70)/bin:$PATH"

✩✩✩✩ FPM ✩✩✩✩

To launch php-fpm on startup:
    mkdir -p ~/Library/LaunchAgents
    cp /usr/local/opt/php70/homebrew.mxcl.php70.plist ~/Library/LaunchAgents/
    launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php70.plist

The control script is located at /usr/local/opt/php70/sbin/php70-fpm

OS X 10.8 and newer come with php-fpm pre-installed, to ensure you are using the brew version you need to make sure /usr/local/sbin is before /usr/sbin in your PATH:

  PATH="/usr/local/sbin:$PATH"

You may also need to edit the plist to use the correct "UserName".

Please note that the plist was called 'homebrew-php.josegonzalez.php70.plist' in old versions
of this formula.

To have launchd start homebrew/php/php70 now and restart at login:
  brew services start homebrew/php/php70
==> Summary
🍺  /usr/local/Cellar/php70/7.0.10_1: 332 files, 49.5M
==> Installing homebrew/php/composer
==> Downloading https://homebrew.bintray.com/bottles-php/composer-1.2.0.el_capitan.bottle.tar.gz
######################################################################## 100.0%
==> Pouring composer-1.2.0.el_capitan.bottle.tar.gz
🍺  /usr/local/Cellar/composer/1.2.0: 4 files, 1.6M
==> Installing composer-completion from homebrew/completions
==> Downloading https://gist.githubusercontent.com/tonglil/753d59d6d649f85600fe/raw/854fcb2f5a4cae824119d6c5c40a2bde4e541689/composer-completion.sh
######################################################################## 100.0%
==> Caveats
Bash completion has been installed to:
  /usr/local/etc/bash_completion.d
==> Summary
🍺  /usr/local/Cellar/composer-completion/1: 2 files, 1.1K, built in 3 seconds

2. 依存関係

手元の環境だと composer の依存関係で下記まで入ってきた……(phpもなのか……)

  • libpng
  • freetype
  • icu4c
  • jpeg
  • libxml2
  • unixodbc
  • readline
  • php70

依存関係はこんな感じ。

$ brew deps --tree composer
homebrew/php/composer (required dependencies)
└── :php70

$ brew deps --tree php70
homebrew/php/php70 (required dependencies)
└── :postgresql
├── freetype
│   └── libpng
│       └── xz
├── gettext
│   └── xz
├── icu4c
├── jpeg
├── libpng
│   └── xz
├── libxml2
│   └── :python
├── unixodbc
├── readline
└── openssl
    └── makedepend
        └── pkg-config