Ruby
Rails
Ubuntu
vagrant

Railsの開発環境構築メモ

背景

毎回開発環境を立ち上げる度にググってるのがムダだなと思ったので自分用にメモ。
ついでにマークダウン記法にも慣れるのが目的。
かけだしWebエンジニアなので訂正やアドバイス等あったらお願いします、、。

目指す環境:
HighSierra 10.13.4
Virtual Box 5.2.8
Ubuntu 17.10
Ruby 2.5.1
Rails 5.2.0

仮想マシン接続まで

Virtual Boxのインストール

Google検索からダウンロード、インストール

仮想マシン設定と立ち上げ

mkdir Railsでディレクトリ作成
cd Railsでディレクトリ内へ移動
mkdir dataで共有用のフォルダを作成
Vagrant initVagrantfile作成
Vagrantfileを編集
Vagrant upで起動
Vagrant sshで仮想マシンにアクセス

Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
  # The most common configuration options are documented and commented below.
  # For a complete reference, please see the online documentation at
  # https://docs.vagrantup.com.

  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://vagrantcloud.com/search.
  # 仮想マシンのもとなるBOXを指定
  config.vm.box = "ubuntu/artful64" #1

  # Disable automatic box update checking. If you disable this, then
  # boxes will only be checked for updates when the user runs
  # `vagrant box outdated`. This is not recommended.
  # config.vm.box_check_update = false

  # 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: 8000, host: 8000 #2

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine and only allow access
  # via 127.0.0.1 to disable public access
  # config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"

  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  config.vm.network "private_network", ip: "192.168.33.10" #3

  # Create a public network, which generally matched to bridged network.
  # Bridged networks make the machine appear as another physical device on
  # your network.
  # config.vm.network "public_network"

  # Share an additional folder to the guest VM. The first argument is
  # the path on the host to the actual folder. The second argument is
  # the path on the guest to mount the folder. And the optional third
  # argument is a set of non-required options.
  # 共有フォルダの設定
  # config.vm.synced_folder "ホスト側のPATH", "ゲスト側のPATH"
  config.vm.synced_folder "./data", "/vagrant" #4

  # Provider-specific configuration so you can fine-tune various
  # backing providers for Vagrant. These expose provider-specific options.
  # Example for VirtualBox:
  #
  # 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"
  # end
  #
  # View the documentation for the provider you are using for more
  # information on available options.

  # Enable provisioning with a shell script. Additional provisioners such as
  # Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
  # documentation for more information about their specific syntax and use.
  # config.vm.provision "shell", inline: <<-SHELL
  #   apt-get update
  #   apt-get install -y apache2
  # SHELL
end

仮想マシン内の設定(今後は仮想マシン内でコマンドを入力)

①日本語設定に必要なパッケージをインストール

$ sudo apt-get -y install language-pack-ja-base language-pack-ja ibus-mozc

②日本語化

$ sudo localectl set-locale LANG=ja_JP.UTF-8 LANGUAGE="ja_JP:ja"
$ source /etc/default/locale

③時刻設定

$ timedatectl set-timezone Asia/Tokyo

*ここでPWが求められますがよく分からなかったのでRoot権限にログインしてから実行しました。

$ sudo su -

Rubyのインストール

①必要なパッケージをインストール

$ sudo apt -y update
$ sudo apt -y install git curl g++ make zlib1g-dev libssl-dev libreadline-dev libyaml-dev libxml2-dev libxslt-dev sqlite3 libsqlite3-dev

②Rubyのバージョン指定ができるrbenvの導入

$ git clone git://github.com/sstephenson/rbenv.git .rbenv
$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
$ echo 'eval "$(rbenv init -)"' >> ~/.bashrc
$ exec $SHELL
$ mkdir -p ~/.rbenv/plugins
$ cd ~/.rbenv/plugins
$ git clone git://github.com/sstephenson/ruby-build.git

③Rubyのインストールと設定

$ rbenv install 2.5.1
$ rbenv global 2.5.1

最後に$ ruby -vを入力し、

ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux]

と表示されればインストール完了。

Railsのインストール

$ gem install rails

Railsアプリの作成からRails s

①共有ディレクトリへ移動

$ cd /vagrant/data

②アプリの作成

$ Rails new myapp

③アプリのディレクトリまで移動

$ cd myapp

④サーバー起動・アクセス

$ Rails s

http://192.168.33.10:3000/にアクセスして確認

その他

ターミナルで使ったコマンドに関しては気が向いたら調べて追記しよう

参考文献

Ubuntu17.10+Vagrant+Rubyで環境開発(1)
Ubuntu17.10+Vagrant+Rubyで環境開発(2)
Ubuntu17.10+Vagrant+Rubyで環境開発(3)
Vagrant入門 – Vagrant の共有フォルダ機能 (Synced Folder)
Vagrantにおける仮想マシンの設定 (config.vm)