こちらが完了していないなら先に済ませてから
Ruby on Railsの環境を開発していきます
Rubyのインストール
まずは仮想環境に入ります
仮想環境に入るにはVagrantFileを生成したディレクトリに
移動する必要があります
vagrantコマンドが動かない場合は
必ず「mvp/centos7」のディレクトリにいるか確認しましょう
$ cd Documents/mvp/centos7/ $ vagrant up $ vagrant ssh [vagrant@localhost ~]$
仮想環境に入ることができたらrbenvをインストールします
Rubyのバージョンを簡単に変更できるツールです
$ sudo yum install -y git gcc openssl-devel readline-devel zlib-devel sqlite-devel gcc-c++ libicu-devel cmake vim $git clone https://github.com/sstephenson/rbenv.git ~/.rbenv $ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile $ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile $ source ~/.bash_profile
rbenvのインストールが完了したのでターミナルで確認しましょう
$ rbenv --version rbenv 1.1.2-2-g4e92322
無事インストールができています
Rubyをインストールする前にRubyをインストールするためのプラグインを
追加します
$ git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
さて次はインストール可能なRubyをrbenvコマンドで確認してみます
※一部部のみ表示しています
$ rbenv install -l Available versions: 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.3.5 2.3.6 2.3.7 2.3.8 2.4.0-dev 2.4.0-preview1 2.4.0-preview2 2.4.0-preview3 2.4.0-rc1 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.5.0-dev 2.5.0-preview1 2.5.0-rc1 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 2.6.0-dev 2.6.0-preview1 2.6.0-preview2 2.6.0-preview3 2.6.0-rc1 2.6.0-rc2 2.6.0 2.6.1 2.6.2 2.6.3 2.7.0-dev 2.7.0-preview1
このコマンドで確認を行うと
利用可能なRubyのバージョンを確認することができます
今回は2.5.0のRubyを選択します
$ rbenv install 2.5.0
インストールが完了したらRubyのバージョンを仮想環境内で
共通で使えるようにします
$ rbenv global 2.5.0 $ rbenv rehash
Rubyのバージョンを確認します
$ ruby -v ruby 2.5.0p0 (2017-12-25 revision 61468) [x86_64-linux]
問題なく入っています
Ruby on Railsのインストール
bundleはGemというRubyで使うことができるライブラリを
管理するためのツールです
$ gem install bundler $ rbenv rehash $ bundle -v Bundler version 2.0.2
次はrailsをインストールします
※5分ほど時間がかかります
$ gem install rails
次はrailsのプロジェクトを作るために
「/home/vagrant/app」までコマンド操作で移動してください
appディレクトリは作業用のディレクトです
appディレクトリに到達したらこちらのコマンドを実行
$ rails new test_app
プロジェクトを作成できたらrailsを実行します
$ bundle install $ rails s Traceback (most recent call last): 61: from bin/rails:3:in `<main>' 60: from bin/rails:3:in `load' 59: from /home/vagrant/app/test_app/bin/spring:15:in `<top (required)>' 58: from /home/vagrant/.rbenv/versions/2.5.0/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require' 57: from /home/vagrant/.rbenv/versions/2.5.0/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require' 56: from /home/vagrant/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/spring-2.1.0/lib/spring/binstub.rb:11:in `<top (required)>' 55: from /home/vagrant/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/spring-2.1.0/lib/spring/binstub.rb:11:in `load' 54: from /home/vagrant/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/spring-2.1.0/bin/spring:49:in `<top (required)>' 53: from /home/vagrant/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/spring-2.1.0/lib/spring/client.rb:30:in `run' 52: from /home/vagrant/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/spring-2.1.0/lib/spring/client/command.rb:7:in `call' 51: from /home/vagrant/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/spring-2.1.0/lib/spring/client/rails.rb:28:in `call' 50: from /home/vagrant/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/spring-2.1.0/lib/spring/client/rails.rb:28:in `load' 49: from /home/vagrant/app/test_app/bin/rails:9:in `<top (required)>' 48: from /home/vagrant/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:291:in `require' 47: from /home/vagrant/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:257:in `load_dependency' 46: from /home/vagrant/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:291:in `block in require' 45: from /home/vagrant/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require' 44: from /home/vagrant/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `require_with_bootsnap_lfi' 43: from /home/vagrant/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register' 42: from /home/vagrant/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `block in require_with_bootsnap_lfi' 41: from /home/vagrant/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require' 40: from /home/vagrant/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/railties-5.2.3/lib/rails/commands.rb:18:in `<top (required)>' 39: from /home/vagrant/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/railties-5.2.3/lib/rails/command.rb:46:in `invoke' 38: from /home/vagrant/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/railties-5.2.3/lib/rails/command/base.rb:65:in `perform' 37: from /home/vagrant/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/thor-0.20.3/lib/thor.rb:387:in `dispatch' 36: from /home/vagrant/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/thor-0.20.3/lib/thor/invocation.rb:126:in `invoke_command' 35: from /home/vagrant/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/thor-0.20.3/lib/thor/command.rb:27:in `run' 34: from /home/vagrant/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/railties-5.2.3/lib/rails/commands/server/server_command.rb:142:in `perform' 33: from /home/vagrant/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/railties-5.2.3/lib/rails/commands/server/server_command.rb:142:in `tap' 32: from /home/vagrant/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/railties-5.2.3/lib/rails/commands/server/server_command.rb:145:in `block in perform' 31: from /home/vagrant/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:291:in `require' 30: from /home/vagrant/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:257:in `load_dependency' 29: from /home/vagrant/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:291:in `block in require' 28: from /home/vagrant/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require' 27: from /home/vagrant/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `require_with_bootsnap_lfi' 26: from /home/vagrant/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register' 25: from /home/vagrant/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `block in require_with_bootsnap_lfi' 24: from /home/vagrant/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require' 23: from /home/vagrant/app/test_app/config/application.rb:7:in `<top (required)>' 22: from /home/vagrant/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bundler-2.0.2/lib/bundler.rb:114:in `require' 21: from /home/vagrant/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bundler-2.0.2/lib/bundler/runtime.rb:65:in `require' 20: from /home/vagrant/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bundler-2.0.2/lib/bundler/runtime.rb:65:in `each' 19: from /home/vagrant/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bundler-2.0.2/lib/bundler/runtime.rb:76:in `block in require' 18: from /home/vagrant/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bundler-2.0.2/lib/bundler/runtime.rb:76:in `each' 17: from /home/vagrant/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bundler-2.0.2/lib/bundler/runtime.rb:81:in `block (2 levels) in require' 16: from /home/vagrant/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require' 15: from /home/vagrant/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `require_with_bootsnap_lfi' 14: from /home/vagrant/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register' 13: from /home/vagrant/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `block in require_with_bootsnap_lfi' 12: from /home/vagrant/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require' 11: from /home/vagrant/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/uglifier-4.1.20/lib/uglifier.rb:5:in `<top (required)>' 10: from /home/vagrant/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:291:in `require' 9: from /home/vagrant/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:257:in `load_dependency' 8: from /home/vagrant/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:291:in `block in require' 7: from /home/vagrant/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require' 6: from /home/vagrant/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `require_with_bootsnap_lfi' 5: from /home/vagrant/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register' 4: from /home/vagrant/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `block in require_with_bootsnap_lfi' 3: from /home/vagrant/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require' 2: from /home/vagrant/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/execjs-2.7.0/lib/execjs.rb:4:in `<top (required)>' 1: from /home/vagrant/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/execjs-2.7.0/lib/execjs.rb:5:in `<module:ExecJS>' /home/vagrant/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/execjs-2.7.0/lib/execjs/runtimes.rb:58:in `autodetect': Could not find a JavaScript runtime. See https://github.com/rails/execjs for a list of available runtimes. (ExecJS::RuntimeUnavailable)
実行したところ「 Could not find a JavaScript runtime. 」という
エラーが発生したのでGemfileをこのように修正します
source 'https://rubygems.org' git_source(:github) { |repo| "https://github.com/#{repo}.git" } ruby '2.5.0' gem 'rails', '~> 5.2.3' gem 'sqlite3' gem 'puma', '~> 3.11' gem 'sass-rails', '~> 5.0' gem 'uglifier', '>= 1.3.0' gem 'coffee-rails', '~> 4.2' gem 'turbolinks', '~> 5' gem 'jbuilder', '~> 2.5' gem 'therubyracer' # 追加 gem 'bootsnap', '>= 1.1.0', require: false group :development, :test do gem 'byebug', platforms: [:mri, :mingw, :x64_mingw] end group :development do gem 'web-console', '>= 3.3.0' gem 'listen', '>= 3.0.5', '< 3.2' gem 'spring' gem 'spring-watcher-listen', '~> 2.0.0' end group :test do gem 'capybara', '>= 2.15' gem 'selenium-webdriver' gem 'chromedriver-helper' end gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
もう一度こちらのコードを走らせます
$ bundle install $ rails s => Booting Puma => Rails 5.2.3 application starting in development => Run `rails server -h` for more startup options Puma starting in single mode... * Version 3.12.1 (ruby 2.5.0-p0), codename: Llamas in Pajamas * Min threads: 5, max threads: 5 * Environment: development * Listening on tcp://localhost:3000 Use Ctrl-C to stop
問題なくサーバーが起動したのでlocalhost:3000にアクセスします
アクセスを試みましたが、表示がされていません
仮想環境(Windowsの中で走らせているCentOS)からブラウザへ接続ができていないため
ブラウザでlocalhost:3000とうっても何も起こらないという現象です
それではこちらを直していきます
まずは仮想環境からでます
$ exit
次はVagrantFileにこちらを追加します
# Create a forwarded port mapping which allows access to a specific port # within the machine from a port on the host machine. In the example below, # accessing "localhost:8080" will access port 80 on the guest machine. # NOTE: This will enable public access to the opened port # config.vm.network "forwarded_port", guest: 80, host: 8080 config.vm.network :forwarded_port, guest: 3000, host: 3000
Ctrl + S で保存してからvagrantに設定を反映させるためにこちらを実行
$ vagrant reload
実行が完了したら再度仮想環境に入ります
$ vagrant ssh
仮想環境へ入ったらこちらのコマンドを実行していきます
$ sudo systemctl stop firewalld.service $ sudo systemctl mask firewalld.service $ sudo systemctl list-unit-files | grep firewalld
実行が完了したら先ほど作成したプロジェクトに移動して
railsを起動してみます
※rails s ではなくrails s -b 0.0.0.0でこれからは起動してください
$ cd app/test_app/ $ rails s -b 0.0.0.0
起動後localhost:3000を確認してみます
無事つながりました!
お疲れさまでした、これでRailsの環境設定は完了です!
いろいろと細かい説明は省きましたが、気になるようであれば「参考記事」
で調べるか自分でgoogleで調べてみてください
参考記事
rails sコマンド実行時に「Could not find a JavaScript runtime.」とエラーが出る場合の対処法
初心者でもかんたん!Ruby on Rails の開発環境の構築手順(Mac/Windows 両対応)
Vagrant(CentOS7) + Rails環境でホストのブラウザからサーバ接続できなかった時の対処
コメントを残す