Help us understand the problem. What is going on with this article?

Vagrant + VirtualBoxでつくるRails 6系の環境構築

ここ数年Rails触ってなかったので、以前のこちらで書いたRails5系の記事を6系に更新したいと思います。

バージョンは

Vagrant 2.2.6
VirtualBox 6.0.14

です。

1. VagrantとVirtualBoxをインストール

Vagrant

以下のリンクより、利用されている端末のOSを選択して、インストーラーをダウンロードし、Vagrantをインストールしてください。
https://www.vagrantup.com/downloads.html

VirtualBox

以下のリンクより、利用されている端末のOSを選択して、インストーラーをダウンロードし、VirtualBoxをインストールしてください。
https://www.virtualbox.org/wiki/Download_Old_Builds_6_0

※ 2019/12/24現在、VirtualBox 6.1だと、 vagrant up を実行時に、以下エラーが発生するので、ご注意ください

$ vagrant up
No usable default provider could be found for your system.

Vagrant relies on interactions with 3rd party systems, known as
"providers", to provide Vagrant with resources to run development
environments. Examples are VirtualBox, VMware, Hyper-V.

The easiest solution to this message is to install VirtualBox, which
is available for free on all major platforms.

If you believe you already have a provider available, make sure it
is properly installed and configured. You can see more details about
why a particular provider isn't working by forcing usage with
`vagrant up --provider=PROVIDER`, which should give you a more specific
error message for that particular provider.

2. Vagrantを初期セットアップ

プロジェクトのディレクトリを作成して、そのディレクトリで、Vagrantの設定ファイルを生成します。

$ mkdir your_project_directory && cd your_project_directory
$ vagrant init

Vagrantfileが生成されるので、編集します

Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/xenial64" # Ubuntu 16.04 を使用
  config.vm.network "forwarded_port", guest: 3000, host: 3000 # Railsで利用するポートを設定
  config.vm.provider "virtualbox" do |vb|
    vb.memory = "2048" # メモリを多めに設定
  end
end

以下のコマンドで、VirtualBoxを起動します

$ vagrant up # ubuntu/xenial64 のboxがダウンロードされていない場合は、しばらく時間がかかります

起動が済んだら、ログインします

$ vagrant ssh

3. Ubuntuのパッケージをセットアップ

パッケージをアップデートして、必要なパッケージをインストールします

$ sudo apt-get -y update
$ sudo apt-get -y install git-core curl zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev python-software-properties libffi-dev pkg-config

4. Rubyのセットアップ

rbenvでRubyをセットアップします

$ git clone git://github.com/sstephenson/rbenv.git .rbenv
$ git clone git://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
$ echo 'eval "$(rbenv init -)"' >> ~/.bashrc
$ echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bashrc
$ echo 'gem: --no-ri --no-rdoc' > ~/.gemrc
$ exec $SHELL

Rubyとbundlerをインストール

$ rbenv install 2.6.5 # ※結構時間がかかります
$ rbenv global 2.6.5
$ rbenv rehash
$ gem install bundler

5. Railsプロジェクトの追加

$ cd /vagrant
$ mkdir blog && cd blog
$ bundle init

生成されたGemfileのrailsをコメントインします。

Gemfile
# frozen_string_literal: true

source "https://rubygems.org"

git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }

gem "rails"

Railsをプロジェクトのgemとしてインストールします

A. ホストがMacの場合

$ bundle config path vendor/bundle
$ bundle install

B. ホストがWindowsの場合

※ 共有フォルダの/vagrant配下にnative extensionsを利用するgemをインストールすると、エラーなるための処置です。/vagrant配下以外なら問題ないはず。

$ sudo mkdir /usr/local/src/bundles
$ sudo chown -R vagrant:vagrant /usr/local/src/bundles
$ bundle config path /usr/local/src/bundles/blog
$ bundle install

6. アプリケーションを作成する

以下のコマンドでカレントディレクトリにRailsアプリケーションを作成します

$ bundle exec rails new . --skip-spring --skip-webpack-install
       exist
      create  README.md
      create  Rakefile
      create  .ruby-version
      create  config.ru
      create  .gitignore
    conflict  Gemfile
Overwrite /vagrant/blog/Gemfile? (enter "h" for help) [Ynaqdhm] Y # Gemfileは上書きするの Y を入力

...(省略)...

Installing webpacker 4.2.2
Bundle complete! 15 Gemfile dependencies, 73 gems now installed.
Bundled gems are installed into `./vendor/bundle`
         run  bundle binstubs bundler
The dependency tzinfo-data (>= 0) will be unused by any of the platforms Bundler is installing for. Bundler is installing for ruby but the dependency is only for x86-mingw32, x86-mswin32, x64-mingw32, java. To add those platforms to the bundle, run `bundle lock --add-platform x86-mingw32 x86-mswin32 x64-mingw32 java`.

なにやらWarningがでているが、Warningなので、このまま進む。

続いて、以下の各コマンド実行

$ bin/rails db:create
$ bin/rails g scaffold post title:string content:text
$ bin/rails db:migrate

ローカルサーバー立ち上げ

$ bin/rails s -b 0.0.0.0

するとエラー

/vagrant/blog/vendor/bundle/ruby/2.6.0/gems/webpacker-4.2.2/lib/webpacker/configuration.rb:95:in `rescue in load': Webpacker configuration file not found /vagrant/blog/config/webpacker.yml. Please run rails webpacker:install Error: No such file or directory @ rb_sysopen - /vagrant/blog/config/webpacker.yml (RuntimeError)

Webpackerは今回入れないので、webpackerをGemfileからコメントアウト

Gemfile
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '2.6.5'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 6.0.2', '>= 6.0.2.1'
# Use sqlite3 as the database for Active Record
gem 'sqlite3', '~> 1.4'
# Use Puma as the app server
gem 'puma', '~> 4.1'
# Use SCSS for stylesheets
gem 'sass-rails', '>= 6'
# Transpile app-like JavaScript. Read more: https://github.com/rails/webpacker
# gem 'webpacker', '~> 4.0'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.7'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 4.0'
# Use Active Model has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use Active Storage variant
# gem 'image_processing', '~> 1.2'

# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.4.2', require: false

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
end

group :development do
  # Access an interactive console on exception pages or by calling 'console' anywhere in the code.
  gem 'web-console', '>= 3.3.0'
  gem 'listen', '>= 3.0.5', '< 3.2'
end


group :test do
  # Adds support for Capybara system testing and selenium driver
  gem 'capybara', '>= 2.15'
  gem 'selenium-webdriver'
  # Easy installation and use of web drivers to run system tests with browsers
  gem 'webdrivers'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

改めて、ローカルサーバー立ち上げ

$ bin/rails s -b 0.0.0.0
=> Booting Puma
=> Rails 6.0.2.1 application starting in development
=> Run `rails server --help` for more startup options
Puma starting in single mode...
* Version 4.3.1 (ruby 2.6.5-p114), codename: Mysterious Traveller
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://0.0.0.0:3000
Use Ctrl-C to stop

localhost:3000 にブラウザでアクセス

rails6.png

無事立ち上がりました 🙌


以上、参考になれば、幸いです :elephant:

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした