LoginSignup
10
7

More than 3 years have passed since last update.

Rails6対応のCentOS7をvagrant up一発で構築するVagrantfile

Last updated at Posted at 2019-11-04

結論

Vagrantfile
# -*- coding: utf-8 -*-

use_ruby_version = "2.6.6"
use_node_version = "12.13.0"

Vagrant.configure("2") do |config|
  config.vm.box = "centos/7"
  config.vm.network "forwarded_port", guest: 3000, host: 3000, host_ip: "127.0.0.1"
  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 = "1024"
  #   vb.customize ["modifyvm", :id, "--memory", "2048", "--cpus", "4", "--ioapic", "on"]
  end

  # システムのアップデート&必要パッケージのインストール
  config.vm.provision "shell", inline: <<-SHELL
    yum -y update &>/dev/null
    yum -y install gcc gcc-c++ make git openssl-devel readline-devel zlib-devel
    yum -y install automake libtool bzip2-devel sqlite-devel

    # install sqlite3.29
    curl https://www.sqlite.org/2019/sqlite-autoconf-3290000.tar.gz | tar zx -C /opt
    cd /opt/sqlite-autoconf-3290000 && ./configure --prefix=/opt/sqlite/sqlite3 && make -j4 && make install
  SHELL
  config.vm.provision "shell", privileged: false, inline: <<-SHELL
    if ! type anyenv &>/dev/null;then
      git clone https://github.com/anyenv/anyenv ~/.anyenv
      echo 'export PATH="$HOME/.anyenv/bin:$PATH"' >> ~/.bash_profile
      echo 'eval "$(anyenv init -)"' >> ~/.bash_profile
      source ~/.bash_profile
      mkdir -p $(anyenv root)/plugins
      git clone https://github.com/znz/anyenv-update.git $(anyenv root)/plugins/anyenv-update
      anyenv install --force-init
    fi
    if ! type rbenv nodenv &> /dev/null;then
      anyenv install rbenv --skip-existing
      anyenv install nodenv --skip-existing
      source ~/.bash_profile
      touch $(nodenv root)/default-packages
    fi
    if ! type ruby &> /dev/null || ruby -e "exit RUBY_VERSION != '#{use_ruby_version}'";then
      anyenv update rbenv
      rbenv install #{use_ruby_version} --skip-existing
      rbenv global #{use_ruby_version}
    fi
    if ! type node &> /dev/null || [ $(node -v) != "v#{use_node_version}" ];then
      anyenv update nodenv
      touch $(nodenv root)/default-packages
      nodenv install #{use_node_version} --skip-existing
      nodenv global #{use_node_version}
    fi
    gem update --system --no-document
    gem install --no-document --force bundler
    gem install --no-document rails pry

    # install yarn for rails6
    curl -o- -L https://yarnpkg.com/install.sh | bash

    # configuration
    echo 'export BINDING=0.0.0.0' >> ~/.bash_profile
    bundle config build.sqlite3 --with-sqlite3-lib=/opt/sqlite/sqlite3/lib
    rbenv rehash
  SHELL
end

nodeやsqlite3のバージョンは詳しくなく適当なので気に食わない場合は各自で調整してください

参考:
【Rails 6.0.0.rc2】rails s したら RAILS_ENV=development environment is not defined in config/webpacker.yml, falling back to production environment Exiting で怒られた。
CentOS環境でRails6.0をSQLite3 (>=3.8)で動かす

10
7
18

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
10
7