LoginSignup
1
2

More than 3 years have passed since last update.

Vagrantfileを作る

Last updated at Posted at 2020-02-03

Vagrantfile を作ってみよう

Vagrant と VirtualBox を使うときに作るのが Vagrantfile
これがあると簡単にVM環境を生成/破棄したり出来る

  • 作り方
    • Vagrantfileをしまっておくフォルダを作る
      • フォルダに1つだけ必要
      • 何度も使われる
      • 書き換えたら更新できる(当たり前だけど)
  1. フォルダを作る
    • mkdir する
    • ここでは仮に mkdir myapp にしておく
  2. フォルダに移動
    • vagrant init bento/ubuntu-18.04 を実行する
      • boxの内容は他のでもいいけど、ココで解説するのは bento/ubuntu-18.04 だけだよ
      • Vagrantfile が myapp フォルダの中に出来る
    • VM環境のディスク容量を指定したい場合
      • vagrant plugin install vagrant-disksize を実行する
      • 参考サイトは、ココ
    • クリップボードとかを双方向で使いたい場合
      • vagrant plugin install vagrant-vbguest を実行する
      • 参考サイトは、ココ
  3. テキストエディタで開いて Vagrantfile の中身を確認する

中身には色々書いてます。
VM環境で使うメモリのサイズとか、IPとか、GUIを使うかどうかとか。

Vagrantfileの詳細は公式で確認してね

  • Box は bento/ubuntu-18.04 を使う
  • 他は以下みたいな形で書けば、とりあえずは良いかもしれない
Vagrantfile
#Vagrantfile
Vagrant.configure("2") do |config|
  config.vm.box = "bento/ubuntu-18.04"
  config.vm.box_version = "201912.14.0"
  #config.disksize.size = '40GB' # 指定する場合はpluginをインストールしてね
  config.vm.box_check_update = false
  config.vm.network "private_network", ip: "192.168.123.222"
  config.vm.provider :virtualbox do |vb|
    vb.memory = 4096
    vb.cpus = 2
    vb.gui = true
    vb.customize [
      "modifyvm", :id,
      "--vram", "256",
      "--clipboard-mode", "bidirectional", # VirtualBox6.0だと"--clipboard"なので注意。idは同じ
      "--draganddrop", "bidirectional",
      "--accelerate3d", "off",
      "--hwvirtex", "on",
      "--nestedpaging", "on",
      "--largepages", "on",
      "--ioapic", "on",
      "--pae", "on",
      "--paravirtprovider", "kvm",
    ]
end

  config.vm.provision :shell, :inline => <<-EOS
    wget -q https://www.ubuntulinux.jp/ubuntu-ja-archive-keyring.gpg -O- | sudo apt-key add -
    wget -q https://www.ubuntulinux.jp/ubuntu-jp-ppa-keyring.gpg -O- | sudo apt-key add -
    sudo wget https://www.ubuntulinux.jp/sources.list.d/bionic.list -O /etc/apt/sources.list.d/ubuntu-ja.list
    sudo apt-get update
    sudo apt-get upgrade -y
    sudo apt-get full-upgrade -y
    sudo apt-get autoremove -y
    sudo apt-get autoclean
    sudo apt-get clean
    sudo apt-get install -y ubuntu-desktop ubuntu-defaults-ja aptitude curl dnsutils lsof zip unzip lsb-release tree man
    sudo timedatectl set-timezone Asia/Tokyo
    sudo update-alternatives --set editor $(update-alternatives --list editor | grep 'vim.basic')
    #sudo apt-get install build-essential #gccを使うなら有効にしてね
    EOS
end

参考

1
2
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
1
2