LoginSignup
4
4

More than 5 years have passed since last update.

Rubyの環境を作った記録

Last updated at Posted at 2015-08-16

モチベーション

Chef や Ruby on Rails のRubyのアプリケーション環境は、沢山のモジュールから構成されるので、バージョン管理というのか、構成管理が出来なければ、とても効率が悪いという事に気づいたので、rvm や nvm といったツールを利用して構成管理できる環境を作った記録です。

Vagrant の環境構築

個人で利用するLinuxの開発環境を作るなら、クラウドよりパーソナル・コンピューターの中で、利用出来る仮想環境が良い。何よりもお金が掛らないし、手軽に作成できるし、ベースになるパッケージを作っておけば、何度も再利用して効率良く作業ができる。
そこで、自分の Mac にVirtual Box, Vagrantをインストールして、その上で、環境を作ることにした。簡単に進められる様に、Vagrant と VirtualBox のでデフォルトの組み合わせを利用する。

Vagrant の 仮想サーバーのイメージは Box と呼ばれ、自分で作る事もできるが、既に作られたものをダウンロードして利用する方が作業が省略できて望ましい。 この作成済みの Box はhttp://www.vagrantbox.es/ からダウンロードできる。 Ubuntu 14.04 では次のリストがあるが、2番目のISOファイルから導入されただけのものを利用する。 1番目は、Chef-Clientなどが導入された便利な版であるが、内容が良く判らないので避ける。

  • Official Ubuntu 14.04 daily Cloud Image amd64 (Development release, No Guest Additions)
  • Ubuntu 14.04 (based on amd64 server iso file)

以下のコマンドで Box の追加を実施

pxedhcp1:Vagrant maho$ vagrant box add Ubuntu14.04_minimam https://github.com/kraksoft/vagrant-box-ubuntu/releases/download/14.04/ubuntu-14.04-amd64.box
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'Ubuntu14.04_minimam' (v0) for provider: 
    box: Downloading: https://github.com/kraksoft/vagrant-box-ubuntu/releases/download/14.04/ubuntu-14.04-amd64.box
==> box: Successfully added box 'Ubuntu14.04_minimam' (v0) for 'virtualbox'!

Vagrant に追加した BOX から仮想マシンを作成して起動する

pxedhcp1:Vagrant maho$ mkdir RubyDev0
pxedhcp1:Vagrant maho$ cd RubyDev0
pxedhcp1:RubyDev0 maho$ vagrant box list
Ubuntu14.04_minimam (virtualbox, 0)

pxedhcp1:RubyDev0 maho$ vagrant init Ubuntu14.04_minimam

Vagrantfile の設定を編集する。 デフォルトの資源の場合、メモリやCPUが少なく時間がかかるので、メモリを2G、CPUを2個にする。 それからホスト内通信のためのLAN I/Fを追加しておく。

変更箇所とその周辺は次のとおりになる。

Vagrantfile(プライベートLANの追加)
  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  config.vm.network "private_network", ip: "192.168.33.21"
Vagrantfile(メモリの増量、CPU追加)
  config.vm.provider "virtualbox" do |vb|
  #   # Display the VirtualBox GUI when booting the machine
  #   vb.gui = true
  #
  #   # Customize the amount of memory on the VM:
     vb.memory = "2048"
     vb.cpus = 2
  end

一回目の起動

$ vagrant up

一回目の起動では、ホストPCのファイルシステムをマウントできずに、以下のエラーがでるので、ログインしてモジュールを追加して再起動する。

default: /vagrant => /Users/maho/Vagrant/RubyDev0
Failed to mount folders in Linux guest. This is usually because
the "vboxsf" file system is not available. Please verify that
the guest additions are properly installed in the guest and
can work properly. The command attempted was:

mount -t vboxsf -o uid=`id -u vagrant`,gid=`getent group vagrant | cut -d: -f3` vagrant /vagrant
mount -t vboxsf -o uid=`id -u vagrant`,gid=`id -g vagrant` vagrant /vagrant

The error output from the last command was:

stdin: is not a tty
mount: unknown filesystem type 'vboxsf'

マウントに失敗しているが、ログインできない訳ではないので、ログインする。

$ vagrant ssh

virtualbox-guest-utils および他の必要なモジュールをインストールする。

vagrant@vagrant-ubuntu-trusty:~$ sudo apt-get install virtualbox-guest-utils git gnupg2 libbz2-dev 

停止して起動

$ vagrant halt
$ vagrant up

これで、ホストのPCと /vagrant の共有ファイルシステムが定義された状態で起動したので、df コマンドで確認すると、マウントできている事が確認できる。

pxedhcp1:RubyDev0 maho$ vagrant ssh
Welcome to Ubuntu 14.04 LTS (GNU/Linux 3.13.0-24-generic x86_64)

 * Documentation:  https://help.ubuntu.com/
Last login: Sun Aug 16 02:28:43 2015 from 10.0.2.2
vagrant@vagrant-ubuntu-trusty:~$ df
Filesystem     1K-blocks      Used Available Use% Mounted on
/dev/sda1       40636768   1368264  37181236   4% /
none                   4         0         4   0% /sys/fs/cgroup
udev             1014244         4   1014240   1% /dev
tmpfs             205012       356    204656   1% /run
none                5120         0      5120   0% /run/lock
none             1025052         0   1025052   0% /run/shm
none              102400         0    102400   0% /run/user
vagrant        975922976 155508552 820414424  16% /vagrant

Ruby のバージョン管理ツール

Ruby のバージョン管理ツールについてGoogleで検索すると、以下の様な rvm, rbenv がありました。

どれを使おうか、迷ってしまったので、グーグル先生に聞いてみたところ、以下の様に、rvm が圧倒的に多い様です。 ということで、rvm をインストールします。

  • rvm 約 9,400,000 件 (0.31 秒)
  • rbenv 約 378,000 件 (0.26 秒)

rvm 本体をインストールする前に、鍵をインストールする。この時、rootではなく、vagrantユーザーにしておくことがポイントです。 rootでインストールすると、全てのユーザーでrvmの恩恵を受けられるのですが、Ubuntu でインストールされているバージョンも使いたい場合があるので、vagrantユーザーで導入します。

vagrant@vagrant-ubuntu-trusty:~$ curl -sSL https://rvm.io/mpapis.asc | gpg2 --import -
gpg: /home/vagrant/.gnupg/trustdb.gpg: trustdb created
gpg: key D39DC0E3: public key "Michal Papis (RVM signing) <mpapis@gmail.com>" imported
gpg: Total number processed: 1
gpg:               imported: 1  (RSA: 1)

rvm本体のインストールも、vagrant ユーザーで実施する。

vagrant@vagrant-ubuntu-trusty:~$ curl -sSL https://get.rvm.io | bash -s stable
Downloading https://github.com/rvm/rvm/archive/1.26.11.tar.gz
Downloading https://github.com/rvm/rvm/releases/download/1.26.11/1.26.11.tar.gz.asc
gpg: Signature made Mon 30 Mar 2015 09:52:13 PM GMT using RSA key ID BF04FF17
gpg: Good signature from "Michal Papis (RVM signing) <mpapis@gmail.com>"
gpg: WARNING: This key is not certified with a trusted signature!
gpg:          There is no indication that the signature belongs to the owner.
Primary key fingerprint: 409B 6B17 96C2 7546 2A17  0311 3804 BB82 D39D C0E3
     Subkey fingerprint: 62C9 E5F4 DA30 0D94 AC36  166B E206 C29F BF04 FF17
GPG verified '/home/vagrant/.rvm/archives/rvm-1.26.11.tgz'

Installing RVM to /home/vagrant/.rvm/
    Adding rvm PATH line to /home/vagrant/.profile /home/vagrant/.mkshrc /home/vagrant/.bashrc /home/vagrant/.zshrc.
    Adding rvm loading line to /home/vagrant/.profile /home/vagrant/.bash_profile /home/vagrant/.zlogin.
Installation of RVM in /home/vagrant/.rvm/ is almost complete:

  * To start using RVM you need to run `source /home/vagrant/.rvm/scripts/rvm`
    in all your open shell windows, in rare cases you need to reopen all shell windows.

# User,
#
#   Thank you for using RVM!
#   We sincerely hope that RVM helps to make your life easier and more enjoyable!!!
#
# ~Wayne, Michal & team.

In case of problems: http://rvm.io/help and https://twitter.com/rvm_io

ログインスクリプトに、rvmのパスが追加されている事をを確認する

vagrant@vagrant-ubuntu-trusty:~$ tail .bashrc
# sources /etc/bash.bashrc).
if ! shopt -oq posix; then
  if [ -f /usr/share/bash-completion/bash_completion ]; then
    . /usr/share/bash-completion/bash_completion
  elif [ -f /etc/bash_completion ]; then
    . /etc/bash_completion
  fi
fi

export PATH="$PATH:$HOME/.rvm/bin" # Add RVM to PATH for scripting

ログアウトして、再度ログインして、rvmコマンドを実行して動作するか確認する。

vagrant@vagrant-ubuntu-trusty:~$ rvm install 2.2.2
vagrant@vagrant-ubuntu-trusty:~$ rvm install 2.0.0
vagrant@vagrant-ubuntu-trusty:~$ rvm 2.0.0 --default
vagrant@vagrant-ubuntu-trusty:~$ rvm list

rvm rubies

=* ruby-2.0.0-p643 [ x86_64 ]
   ruby-2.2.2 [ x86_64 ]

# => - current
# =* - current && default
#  * - default
vagrant@vagrant-ubuntu-trusty:~$ ruby -v
ruby 2.0.0p643 (2015-02-25 revision 49749) [x86_64-linux]

SQLite3のインストール

デフォルトでインストールされているのですが、一応以下の様に確認しました。

root@vagrant-ubuntu-trusty:~# apt-get install sqlite3
Reading package lists... Done
Building dependency tree       
Reading state information... Done
sqlite3 is already the newest version.

nvmのインストール

Rails の実行環境に必要な Node.js の環境は、Node.js のバージョン管理ツール nvm の環境下でインストールしておきます。インストールは、 https://github.com/creationix/nvm に沿って実施しました。

vagrant@vagrant-ubuntu-trusty:~$ curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.26.0/install.sh | bash
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  7728  100  7728    0     0   1343      0  0:00:05  0:00:05 --:--:--  1823
=> Downloading nvm as script to '/home/vagrant/.nvm'

=> Appending source string to /home/vagrant/.bashrc
=> You can now start using nvm

ログイン・シェルに設定が追加された事を確認しておきます。

vagrant@vagrant-ubuntu-trusty:~$ tail .bashrc
    . /usr/share/bash-completion/bash_completion
  elif [ -f /etc/bash_completion ]; then
    . /etc/bash_completion
  fi
fi

export PATH="$PATH:$HOME/.rvm/bin" # Add RVM to PATH for scripting

export NVM_DIR="/home/vagrant/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"  # This loads nvm

初期状態では、N/A となっており、node.js が選択されていない事が判ります。


vagrant@vagrant-ubuntu-trusty:~$ nvm list
            N/A
node -> stable (-> N/A) (default)
iojs -> iojs- (-> N/A) (default)

最新バージョンと一つ前のバージョンの最終版を導入しておきます。

vagrant@vagrant-ubuntu-trusty:~$ nvm install v0.12.7
vagrant@vagrant-ubuntu-trusty:~$ nvm install v0.11.16
vagrant@vagrant-ubuntu-trusty:~$ nvm list
->     v0.11.16
        v0.12.7
default -> 0.12.7 (-> v0.12.7)
node -> stable (-> v0.12.7) (default)
stable -> 0.12 (-> v0.12.7) (default)
unstable -> 0.11 (-> v0.11.16) (default)
iojs -> iojs- (-> N/A) (default)

最後にインストールした方がデフォルトになっているので、最新版の0.12.7に変更しておきます。

vagrant@vagrant-ubuntu-trusty:~$ nvm alias default 0.12.7
default -> 0.12.7 (-> v0.12.7)

変更が反映されている事を確認するために、ログアウトして、ログインし直す。

pxedhcp1:RubyDev0 maho$ vagrant ssh
Welcome to Ubuntu 14.04 LTS (GNU/Linux 3.13.0-24-generic x86_64)

 * Documentation:  https://help.ubuntu.com/
Last login: Sun Aug 16 03:55:15 2015 from 10.0.2.2
vagrant@vagrant-ubuntu-trusty:~$ node -v
v0.12.7

これで Node.js のインストールは完了です。

Ruby on Rails のインストール

Railsのインストールは簡単です。 経験上、メモリサイズが1Gバイトの場合は、途中で止まった様に遅くなるので、メモリが2Gバイト程度割当られている事を確認しましょう。 インストールした後に戻しても問題ありません。

vagrant@vagrant-ubuntu-trusty:~$ gem install rails

Python pyenvのインストール

Python も利用する事が多いので、pvenvの管理下で、Python インスールしておきます。 コードとインストール方法は、https://github.com/yyuu/pyenv にあります。

vagrant@vagrant-ubuntu-trusty:~$ git clone https://github.com/yyuu/pyenv.git ~/.pyenv

ログインシェルに設定を追加します。

$ echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
$ echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
$ echo 'eval "$(pyenv init -)"' >> ~/.bash_profile

一旦ログアウトして、再ログインしてインストールを開始します。

vagrant@vagrant-ubuntu-trusty:~$ pyenv install 2.7.10
vagrant@vagrant-ubuntu-trusty:~$ pyenv versions
* system (set by /home/vagrant/.pyenv/version)
  2.7.10
vagrant@vagrant-ubuntu-trusty:~$ pyenv local 2.7.10
vagrant@vagrant-ubuntu-trusty:~$ pyenv versions
  system
* 2.7.10 (set by /home/vagrant/.python-version)

Vagrant の パッケージを作成とBOX追加

vagrant を 止めてパッケージを作って、BOX に加えておきます。

pxedhcp1:RubyDev0 maho$ vagrant halt
==> default: Attempting graceful shutdown of VM...
pxedhcp1:RubyDev0 maho$ vagrant package
==> default: Clearing any previously set forwarded ports...
==> default: Exporting VM...
==> default: Compressing package to: /Users/maho/Vagrant/RubyDev0/package.box
pxedhcp1:RubyDev0 maho$ ls
Vagrantfile package.box

作ったパッケージをRoRDevとして追加します。

pxedhcp1:RubyDev0 maho$ vagrant box add RoRDev_Ubuntu14 package.box 

追加されている事を確認します。

pxedhcp1:RubyDev0 maho$ vagrant box list
RoRDev_Ubuntu14     (virtualbox, 0)
Ubuntu14.04_minimam (virtualbox, 0)

これで、開発環境のベースが出来上がりました。

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4