LoginSignup
3
3

More than 3 years have passed since last update.

ゼロから始める開発環境構築(Rails MySQL) その1

Last updated at Posted at 2019-11-02

この記事の最終目標

・Vagrant × VirtualBox で開発環境を構築する
・Rails × MySQLのアプリケーションの立ち上げ その2の記事
・MySQL WorkbenchによるDBの操作  その3の記事

※この記事作成にいたった経緯
日々の勉学のために仮想環境を立ち上げることが多く回を重ねるごとに「次はこうしよう・・ああしよう・・」となるうちにネットで参照する記事が増えてしまい作業が煩雑になってしまったため作業手順をまとめる意図で作成しました。
っと同時に同じように自分で仮想環境を立ち上げてみたい方などにも役に立ってくれれば幸いです。

上記経緯であることと自分自身もそこまで詳しいわけではないので概要レベルの解説のみで主に必要となるコマンドを中心に記事が構成されています。

※最低限必要となるLinuxコマンドの理解とVimの操作は理解されている前提で話が進んでいます。
コマンド実行後の結果も内容をコンパクトにする目的で基本的に省略しています。

準備

基本的にはWindowsもMacもやり方は変わりませんただWindowsの場合はBIOSで Intel(R) VirtualizationTechnology を有効化させないといけません。(AMDも似たようなのがあります)
この辺は各端末によってBIOSの立ち上げ方も異なるためご自身のPCのBIOSの立ち上げ方の確認と上記設定の変更方法を調べて変更してください(Hyper-Vは使わないなら有効化する必要はないです)

それと下記ページから必要となるツールをダウンロードしてください。
(インストールの際の設定はすべてデフォルトのままで問題ありません。)

Vagrant
https://www.vagrantup.com/downloads.html
vagrant.png

VirtualBox
https://www.virtualbox.org/wiki/Downloads
ダウンロード.jpg

基本的に最新版で問題ないはずですが※2019/12/22追記、VirtualBoxのバージョンがvagrantより新しいとvagrant upでこけますのでちょっと古いほうがおすすめ
2019年9月23日時点での筆者のバージョンを記載します。※バージョンが違うのは入れた時期が違うからで意図はないです。
Windows
・Vagrant 2.2.5
・Oracle VM VirtualBox 6.0.10
Mac
・Vagrant 2.2.5
・Oracle VM VirtualBox 6.0.8

※Windowsの方のみ見てください
Windowsの場合bashが使えるといろいろ便利なのと割と使いやすいのででGit for Windowsがおすすめです。
https://gitforwindows.org/
git.png

インストールが終わるとGit Bashが使えるはずですので以降これを使ってコマンドを打っていきます。

仮想環境の立ち上げ準備

任意のディレクトリに仮想環境を立ち上げるためのフォルダを作成します。
今回はデスクトップのstudyフォルダを作ってvagrant initで準備開始です。

hirok@DESKTOP-RR8UU2O MINGW64 ~/Desktop/study
$ vagrant init bento/centos-7.5

簡単な解説

Vagrantとはあくまで仮想環境を作るためのツールになります。実際に仮想環境を作るのはVirtualBoxです。
vagrant init以降に作られるvagrantfileに必要な情報を記載してvagrant upするとその設定を読み込んで立ち上げてくれます。
あとbentoというvagrantのOSイメージを使うことで細かい設定もこみこみ入っています。
ということでvagrantfileを編集します。
※一部この後行う共有フォルダ設定のための部分も同時に編集しています。必要ない方はその行は編集しないでください。

hirok@DESKTOP-D8ED7Q7 MINGW64 ~/Desktop/study
$ vi Vagrantfile
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.
  config.vm.box = "bento/centos-7.5"

  # 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: 80, host: 8080

  # 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.
  # ↓プライベートIPの設定
  config.vm.network "private_network", ip: "192.168.33.10"

  # 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 ".", "/vagrant", type: "virtualbox"

  # Provider-specific configuration so you can fine-tune various
  # backing providers for Vagrant. These expose provider-specific options.
  # Example for VirtualBox:
  # ↓VMに配分するメモリなどの設定
  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

メモリ量は必要に応じて増やしたり減らしたりできますしCPUのコア数も指定できます。
メモリに余裕がある方はもっと増やしても大丈夫です。

仮想環境の立ち上げ

下記コマンドで仮想環境を立ち上げます。

hirok@DESKTOP-D8ED7Q7 MINGW64 ~/Desktop/study
$ vagrant up

よく使うvagrantコマンド

ちゃんと立ち上がっているか確認することもあるのでその際は下記のコマンドで確認できます。

$ vagrant status
Current machine states:

default                   running (virtualbox)

The VM is running. To stop this VM, you can run `vagrant halt` to
shut it down forcefully, or you can run `vagrant suspend` to simply
suspend the virtual machine. In either case, to restart it again,
simply run `vagrant up`.

文字通りrunningになっていれば起動中になっています。
停止したい場合は下記コマンドでシャットダウンできます。

$ vagrant halt

開発環境にログイン

hirok@DESKTOP-D8ED7Q7 MINGW64 ~/Desktop/study
$ vagrant ssh
[vagrant@localhost ~]$

↑のようになっていればログインできています。

開発環境の整備

自分も詳しくはないのですがトラブルのもとになりやすいそうなのでselinuxを事前に無効化します

[vagrant@localhost ~]$ sudo sed -i "s/\(^SELINUX=\).*/\disabled/" /etc/selinux/config

設定反映のためにいったんログアウト

[vagrant@localhost ~]$ exit

仮想環境を立ち上げ直し

hirok@DESKTOP-D8ED7Q7 MINGW64 ~/Desktop/study
$ vagrant reload

共有フォルダ設定

※共有フォルダ設定した人は見てください
現状どことどこがつながっているかというと
ローカル上→vagrantfileが置いてあるディレクトリ(手順通り作っていれば/Desktop/study)
仮想環境上→/vagrant

が共有フォルダとしてつながるように設定されています。(任意のフォルダに決定することも可能かと思いますが/vagrantがデフォルトの共有フォルダディレクトリになっているのでので上記設定がおすすめかと思います。)

デフォルトでログインした際に入るディレクトリは/home/vagrant
いちいち設定したディレクトリに移動するのが面倒なので下記のエイリアスを設定しておくと楽です

hirok@DESKTOP-D8ED7Q7 MINGW64 ~/Desktop/study
$ vi ~/.bash_profile

下記コマンドを.bash_profileの一番下に追記してください。

~/.bash_profile
alias vssh='vagrant ssh -c "cd /vagrant; bash"';

ほんとに共有できているか見てみましょう
まずローカル上

hirok@DESKTOP-D8ED7Q7 MINGW64 ~/Desktop/study
$ ls
Vagrantfile

↑のような感じかと思います。
仮想環境へログイン

hirok@DESKTOP-D8ED7Q7 MINGW64 ~/Desktop/study
$ vssh
[vagrant@localhost vagrant]$ ls
Vagrantfile
[vagrant@localhost vagrant]$

適当にディレクトリを作ります

[vagrant@localhost vagrant]$ mkdir test
[vagrant@localhost vagrant]$ ls
test  Vagrantfile
[vagrant@localhost vagrant]$

現在仮想環境上でtestディレクトリを作成しています。
ログアウトしてローカル上を確認します。

[vagrant@localhost vagrant]$ exit
exit
Connection to 127.0.0.1 closed.

hirok@DESKTOP-D8ED7Q7 MINGW64 ~/Desktop/study
$ ls
test/  Vagrantfile

hirok@DESKTOP-D8ED7Q7 MINGW64 ~/Desktop/study
$

仮想環境上で作ったディレクトリがローカル上のsutdyディレクトリに作られています。
当たり前ですがああすればこうなる

hirok@DESKTOP-D8ED7Q7 MINGW64 ~/Desktop/study
$ ls
test/  Vagrantfile

hirok@DESKTOP-D8ED7Q7 MINGW64 ~/Desktop/study
$ cd test/

hirok@DESKTOP-D8ED7Q7 MINGW64 ~/Desktop/study/test
$ touch test

hirok@DESKTOP-D8ED7Q7 MINGW64 ~/Desktop/study/test
$ ls
test

hirok@DESKTOP-D8ED7Q7 MINGW64 ~/Desktop/study/test
$ vssh
[vagrant@localhost vagrant]$ cd test/
[vagrant@localhost test]$ ls
test
[vagrant@localhost test]$ pwd
/vagrant/test
[vagrant@localhost test]$

こうしておくと何が良いかというとローカル環境のエディタを使って仮想環境上のファイルをいじれるのでVScodeとかAtomとか使って開発できます。

いったんここまでで仮想環境の基本的な整備は済みました。
次回はRails×MySQLの環境を整備します。

その2へ続く・・

3
3
0

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