Using vagrant to run virtual machines with desktop

2019-01-20 20:46发布

My company's development environment is based on virtual machines, running on VirtualBox. We would like to move one step further, and use the capabilities of Vagrant to have the description of the machine in a text file and then be able to "raise" that machine based on that text file. Combined to puppet, this would solve us the problem that everyone have different software versions installed in the VM.

However, Vagrant seems very focused to develop on the host, letting the machine in the background. We would need to have our development environment within the machine, so we would need a complete GUI, so when typing "vagrant up" a machine with a complete desktop environment (XFCE, KDE...) should appear.

So far, I've managed to create a "base" box from a Xubuntu distribution. But when I type "vagrant up", although the desktop appears, and I am able to login properly, Vagrant freezes at the message "Waiting for machine to boot. This may take a few minutes...". After a while Vagrant crashes due timeout. So shared folders are not created, nor the package provisioner -puppet- is executed.

How to create a virtual machine with a complete GUI using vagrant?

9条回答
趁早两清
2楼-- · 2019-01-20 21:18

Here is a slightly adapted Vagrantfile for Ubuntu 18.04 LTS / bionic - thanks to Air's and Nik's answers, and this post explaining how to increase the disk size when using VirtualBox (default = 10 GB).

The VM includes a LightDM login screen.

Update: I've created a GitHub repo from this example, and added many software packages for frontend + backend development.

# Optional - enlarge disk:
#vagrant plugin install vagrant-disksize
vagrant up
vagrant reload
# After reboot, the VM screen should show the LightDM login screen.
# Log in as user "vagrant", password "vagrant".
Vagrant.configure(2) do |config|
  config.vm.box = "ubuntu/bionic64"
  # Optional - enlarge disk (will also convert the format from VMDK to VDI):
  #config.disksize.size = "50GB"

  config.vm.provider "virtualbox" do |vb|
    # Display the VirtualBox GUI when booting the machine
    vb.gui = true
  end

  # https://askubuntu.com/questions/1067929/on-18-04-package-virtualbox-guest-utils-does-not-exist
  config.vm.provision "shell", inline: "sudo apt-add-repository multiverse && sudo apt-get update"

  # Install xfce and virtualbox additions.
  # (Not sure if these packages could be helpful as well: virtualbox-guest-utils-hwe virtualbox-guest-x11-hwe)
  config.vm.provision "shell", inline: "sudo apt-get install -y xfce4 virtualbox-guest-dkms virtualbox-guest-utils virtualbox-guest-x11"
  # Permit anyone to start the GUI
  config.vm.provision "shell", inline: "sudo sed -i 's/allowed_users=.*$/allowed_users=anybody/' /etc/X11/Xwrapper.config"

  # Optional: Use LightDM login screen (-> not required to run "startx")
  config.vm.provision "shell", inline: "sudo apt-get install -y lightdm lightdm-gtk-greeter"
  # Optional: Install a more feature-rich applications menu
  config.vm.provision "shell", inline: "sudo apt-get install -y xfce4-whiskermenu-plugin"
end
查看更多
贪生不怕死
3楼-- · 2019-01-20 21:20

I'm using ubuntu desktop image, it works nicely with two monitors on windows with virtual box provider.

Vagrant.configure(2) do |config|
  config.vm.box = "box-cutter/ubuntu1404-desktop"

  config.ssh.forward_agent = true

  config.vm.network "forwarded_port", guest: 8080, host: 8080
  config.vm.network "forwarded_port", guest: 3000, host: 3000


  config.vm.synced_folder "../../git", "/home/vagrant/git"

  config.vm.provider "virtualbox" do |vb|
    vb.gui = true
    vb.customize ["modifyvm", :id, "--monitorcount", "2"]
    vb.memory = "2048"
  end
end
查看更多
Bombasti
4楼-- · 2019-01-20 21:21

Like the xfce4 solution by @Air. Once I had success, but today I failed with ubuntu16.04. I got this error:

xrdb can't open display 1

But luckily, I found this works:

startx
查看更多
霸刀☆藐视天下
5楼-- · 2019-01-20 21:23

Here's Air's excellent answer in the form of a Vagrantfile

Vagrant.configure(2) do |config|
  # Ubuntu 15.10
  config.vm.box = "ubuntu/wily64"

  config.vm.provider "virtualbox" do |vb|
    # Display the VirtualBox GUI when booting the machine
    vb.gui = true
  end

  # Install xfce and virtualbox additions
  config.vm.provision "shell", inline: "sudo apt-get update"
  config.vm.provision "shell", inline: "sudo apt-get install -y xfce4 virtualbox-guest-dkms virtualbox-guest-utils virtualbox-guest-x11"
  # Permit anyone to start the GUI
  config.vm.provision "shell", inline: "sudo sed -i 's/allowed_users=.*$/allowed_users=anybody/' /etc/X11/Xwrapper.config"
end

To start the vm

vagrant up

Login with username: vagrant, password: vagrant via the login prompt on the virtualbox GUI.

Start xfce

startx
查看更多
Viruses.
6楼-- · 2019-01-20 21:23

You might also consider using Packer to create VirtualBox images for developers to use.

Rather than sharing the Vagrantfile which developers each use to build and run their VM, you would have a packer template (json) which is used to create a VM image. Developers download or copy the image and run it locally, directly in VB, without having to build it themselves.

Many of the publicly shared Vagrant base boxes are created with Packer.

查看更多
Root(大扎)
7楼-- · 2019-01-20 21:27

Adding to billmalarky's comment above, on fedora 20 the following was necessary before starting xfce:

  • Install VirtualBox-guest.rpm (available from rpmfusion repos)
  • yum groups mark install 'graphical_environment'
  • yum groupinstall "Xfce"
  • yum install xorg-x11-drivers

Here is the code:

config.vm.provision "shell", inline: <<-SHELL        
    #Install Virtual Box guest additions from rpmfusion repos
    cd /vagrant
    yum install -y rpmfusion-free-release-20.noarch.rpm 
    yum install -y rpmfusion-nonfree-release-20.noarch.rpm
    yum update -y
    yum install -y VirtualBox-guest

    #Add XFCE desktop to fedora server
    yum groups mark install 'graphical_environment'
    yum groupinstall -y "Xfce"
    yum install -y xorg-x11-drivers   
SHELL
查看更多
登录 后发表回答