VagrantとVirtualBoxを使ってRailsの開発環境を構築したいと思います。
1. 仮想マシンを用意
- Vagrant – 記事作成時点 Ver 2.2.0
- VirtualBox – 記事作成時点 Ver 5.2.20
自分の環境に合ったものをそれぞれダウンロードしてください。
ダウンロードが完了し、インストールを終えたら以下の手順にしたがって仮想マシンを設定していきます。
先ほどのコマンドにより現在のディレクトリに 以下のコマンドでvagrantを起動します。 以下のコマンドでゲストOSに接続することができます。 以下のように接続できると思います。 無事接続できたら必要なツールをインストールします。以下のコマンドを実行してください。 インストールが終われば、仮想環境のセットアップは終了です。次にRubyをインストールします。 Rubyをインストールするのにrbenvというツールを利用することで複数のRubyをインストールしバージョンを切り替えることができるようになります。 以上までで 以下のコマンドでインストール可能なRubyの一覧が表示されます。 この記事を執筆時の最新版は インストールがおわったらOS全体で利用するRubyのバージョンをセットします。 インストールが無事終われば、Railsアプリを作成できるようになります。 Railsが起動したら192.168.33.10にアクセスするとRailsのウェルカムページが表示されるかと思います。 今回はVagrant & VirtualBoxを利用してRailsの開発環境を構築しました。環境の構築方は他にもあるので別の機会に書いて見たいと思います。
$ vagrant init centos/7
Vagrantfileの編集
Vagrantfile
が追加されました。このファイルの35行目あたりの# config.vm.network "private_network", ip: "192.168.33.10"
という記述を見つけて#
を取りコメントアウトを外します。
# config.vm.network "private_network", ip: "192.168.33.10" # 変更前
config.vm.network "private_network", ip: "192.168.33.10" # 変更後
仮想マシンの起動
$ vagrant up
ゲストOSにSSHで接続する
$ vagrant ssh
[vagrant@localhost ~]$
$ curl --silent --location https://rpm.nodesource.com/setup_8.x | sudo bash -
$ sudo yum install -y git gcc openssl-devel readline-devel zlib-devel sqlite-devel gcc-c++ libicu-devel cmake vim libffi-devel.x86_64 nodejs
2. Rubyのインストール
rbenvのインストール
以下の手順に従いrbenv
をインストールしてください。
$ git clone https://github.com/rbenv/rbenv.git ~/.rbenv
$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
$ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
$ source ~/.bash_profile
rbenvのプラグインをインストール
$ git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
rbenv
のセットアップは終了です。Rubyのインストール
$ rbenv install -l
2.5.1
なのでそれをインストールします。
$ rbenv install 2.5.1
$ rbenv global 2.5.1
$ ruby -v
ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux] # Rubyのバージョンが確認できればOK
3. Railsのインストール
Bundlerのインストール
$ rbenv exec gem install bundler
Railsのインストール
$ gem install rails
好きなディレクトリで以下のrails new [アプリ名]
を実行するとRailsアプリのテンプレートが作成されます。
$ rails new sample_app
create
create README.md
create Rakefile
create .ruby-version
create config.ru
create .gitignore
create Gemfile
run git init from "."
Initialized empty Git repository in /home/vagrant/sample_app/.git/
create package.json
create app
create app/assets/config/manifest.js
create app/assets/javascripts/application.js
create app/assets/javascripts/cable.js
create app/assets/stylesheets/application.css
create app/channels/application_cable/channel.rb
create app/channels/application_cable/connection.rb
create app/controllers/application_controller.rb
create app/helpers/application_helper.rb
create app/jobs/application_job.rb
create app/mailers/application_mailer.rb
create app/models/application_record.rb
create app/views/layouts/application.html.erb
create app/views/layouts/mailer.html.erb
create app/views/layouts/mailer.text.erb
create app/assets/images/.keep
create app/assets/javascripts/channels
create app/assets/javascripts/channels/.keep
create app/controllers/concerns/.keep
create app/models/concerns/.keep
create bin
create bin/bundle
create bin/rails
create bin/rake
create bin/setup
create bin/update
create bin/yarn
create config
create config/routes.rb
create config/application.rb
create config/environment.rb
create config/cable.yml
create config/puma.rb
create config/spring.rb
create config/storage.yml
create config/environments
create config/environments/development.rb
create config/environments/production.rb
create config/environments/test.rb
create config/initializers
create config/initializers/application_controller_renderer.rb
create config/initializers/assets.rb
create config/initializers/backtrace_silencers.rb
create config/initializers/content_security_policy.rb
create config/initializers/cookies_serializer.rb
create config/initializers/cors.rb
create config/initializers/filter_parameter_logging.rb
create config/initializers/inflections.rb
create config/initializers/mime_types.rb
create config/initializers/new_framework_defaults_5_2.rb
create config/initializers/wrap_parameters.rb
create config/locales
create config/locales/en.yml
create config/master.key
append .gitignore
create config/boot.rb
create config/database.yml
create db
create db/seeds.rb
create lib
create lib/tasks
create lib/tasks/.keep
create lib/assets
create lib/assets/.keep
create log
create log/.keep
create public
create public/404.html
create public/422.html
create public/500.html
create public/apple-touch-icon-precomposed.png
create public/apple-touch-icon.png
create public/favicon.ico
create public/robots.txt
create tmp
create tmp/.keep
create tmp/cache
create tmp/cache/assets
create vendor
create vendor/.keep
create test/fixtures
create test/fixtures/.keep
create test/fixtures/files
create test/fixtures/files/.keep
create test/controllers
create test/controllers/.keep
create test/mailers
create test/mailers/.keep
create test/models
create test/models/.keep
create test/helpers
create test/helpers/.keep
create test/integration
create test/integration/.keep
create test/test_helper.rb
create test/system
create test/system/.keep
create test/application_system_test_case.rb
create storage
create storage/.keep
create tmp/storage
create tmp/storage/.keep
remove config/initializers/cors.rb
remove config/initializers/new_framework_defaults_5_2.rb
Railsアプリを起動する
$ rails s -b 0.0.0.0
=> Booting Puma
=> Rails 5.2.1 application starting in development
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.12.0 (ruby 2.5.1-p57), codename: Llamas in Pajamas
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://0.0.0.0:3000
Use Ctrl-C to stop
4. まとめ