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

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



Vagrant 2.2.6
VirtualBox 6.0.14


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





※ 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を初期セットアップ


$ mkdir your_project_directory && cd your_project_directory
$ vagrant init


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


$ 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のセットアップ


$ git clone git:// .rbenv
$ git clone 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


$ 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


# frozen_string_literal: true

source ""

git_source(:github) {|repo_name| "{repo_name}" }

gem "rails"


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. アプリケーションを作成する


$ bundle exec rails new . --skip-spring --skip-webpack-install
      create  Rakefile
      create  .ruby-version
      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`.



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


$ bin/rails s -b


/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)


source ''
git_source(:github) { |repo| "{repo}.git" }

ruby '2.6.5'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 6.0.2', '>='
# 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:
# gem 'webpacker', '~> 4.0'
# Turbolinks makes navigating your web application faster. Read more:
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more:
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]

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'

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'

# 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
=> Booting Puma
=> Rails 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://
Use Ctrl-C to stop

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


無事立ち上がりました 🙌

以上、参考になれば、幸いです :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
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