LoginSignup
3
6

More than 5 years have passed since last update.

Vagrant + VirtualBox + Ubuntuの環境をセットアップしてみる

Last updated at Posted at 2018-10-10

目的

  • 下記の動機から、Windowsマシン上にUbuntu環境を構築してみる。
    • Ubuntu上で動作するGUIアプリ開発環境を構築したい

環境

  • OS : Windows 10 Home
  • Vagrant 2.1.5
  • VirtualBox 5.2.18
  • Ubuntu 16.04 LTS

方法

  • VMを立てる
    • WSL(Windows Subsystem for Linux)を使う方法もあるが、GUI環境を構築するのに手間がかかりそうなのでVMを立てることにした(実際の手順は簡単なのかもしれない)
  • Vagrant使ってみる
    • VagrantfileにVMの設定を記述できて、VMのインストールも簡単そうだったから
  • VMのホスティングにはVirtualBox使う
    • Vagrantで使うデフォルトがVirtualBoxだったから

構築方法

  • 参考にしたサイトはこちら
  • Boxファイルは最新のものを取得してきたかったので、VagrantCloudを検索、Ubuntu 16.04 LTSは、「Ubuntu/xenial64」というパスになっていることがわかる
  • 手順0:BIOSの設定でVMの機能を有効化する
  • 手順1:VirtualBoxとVagrantをインストールしたら、workフォルダ(c:\vagrant\ubuntu16など)を作成して、カレントをworkに移す
  • 手順2:Ubuntu環境を初期化する
$vagrant init ubuntu/xenial64
  • 手順3: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 = "ubuntu/xenial64" # 追加

  # 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.
  # 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" # 作成するアプリの都合上、NAPTにしたくなかったので、ブリッジ接続にした

  # 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 "../data", "/vagrant_data"

  # 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 # GUI環境がほしい
     # Customize the amount of memory on the VM:
     vb.memory = "2048"
     vb.customize [
       "modifyvm", :id,
       "--vram", "128", # 値は適当
       "--clipboard", "bidirectional",
       "--draganddrop", "bidirectional",
       "--cpus", "2",
       "--ioapic", "on" # これは不要かも
     ]
  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
  • 手順4:vmを起動して、デスクトップ環境をインストールする(参考サイトを参照)
  • 手順5:画面解像度を1024x768固定から、ホスト環境に合わせられるようにする
    • ここはかなりはまって、Vagrantfileで何とかできるように下記のように試したが全てNG
    • こちらの記事を参考にvbguest pluginをインストールしてvmを再起動したら、解像度がホスト環境に合うようになった
     #vb.customize "post-boot", ["controlvm", :id, "setvideomodehint", "1920", "1080", "32"]
     #vb.customize ["setextradata", "global", "GUI/MaxGuestResolution", "any"]
     #vb.customize ["setextradata", :id, "CustomVideoMode1", "1920x1080x32"]
  • 手順6:vm内でubuntuユーザーのパスワードを設定する
    • vagrantユーザーとubuntu(Ubuntu?)ユーザーがデフォルトで入っているが、vagrantユーザーの権限はstandard、ubuntuユーザーはadminだがパスワードは?という状態、CLIでsudoできるので大抵は問題ないが、GUIで設定変更とかやろうとすると、ubuntuユーザーのパスワードを聞かれて進まない、検索してもVagrantfileに記述されているという記事は出てくるが、何も記述されていない状態
    • ここを参考に、そもそもパスワードが設定されていない状態ということで、下記を実行、できた!でもVagrantfileに記述したほうがスマート $sudo passwd ubuntu
3
6
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
6