Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using vagrant to run virtual machines with desktop environment

People also ask

Does vagrant support GUI?

You'll have to do vagrant reload after enabling gui.

How does vagrant run virtual machines?

Vagrant pulls an image from the Vagrant Cloud. For example: ubuntu , centos , alpine , etc. Vagrant creates a Vagrantfile from an image and stores it in the local system. The Vagrantfile consists of the configuration of the VM, which is then used to boot up the VM.

Can vagrant work without VirtualBox?

Solutions that work with Vagrant include VirtualBox, VMware, Docker, Hyper-V, and custom solutions.

Is vagrant the same as VirtualBox?

VirtualBox is basically inception for your computer. You can use VirtualBox to run entire sandboxed operating systems within your own computer. Vagrant is software that is used to manage a development environment.


I just got this working with basically three steps. The advice from askubuntu.com didn't quite work for me, so try this simplified version:

  1. Get a basic Ubuntu image working. You should be able to boot it and vagrant ssh.
  2. Next, enable the VirtualBox display, which is off by default. Halt the VM and uncomment these lines in Vagrantfile:
    config.vm.provider :virtualbox do |vb|
      vb.gui = true
    end
  3. Boot the VM and observe the new display window. Now you just need to install and start xfce4. Use vagrant ssh and:
    sudo apt-get install xfce4
    sudo startxfce4&
    

If this is the first time you're running this Ubuntu environment, you'll need to run the following command before installing xfce4:

sudo apt-get update

That's it, you should be landed in a xfce4 session.

Update: For a better experience, I recommend these improvements:

  1. Don't start the GUI as root. You really want to stay the vagrant user. To do this you need to permit anyone to start the GUI: sudo vim /etc/X11/Xwrapper.config and edit it to allowed_users=anybody.
  2. Next, install the VirtualBox guest tools before starting the GUI. This will give you a healthy screen resolution, integrated mouse, etc.
    $ sudo apt-get install -y xfce4 virtualbox-guest-dkms virtualbox-guest-utils virtualbox-guest-x11
    $ sudo VBoxClient-all
  3. Only now should you start the GUI as the vagrant user, with $ startxfce4&.

Update 2: Tried this today and the VBoxClient-all script isn't always installed. If it's missing, you can replace with the equivalent:

sudo VBoxClient --clipboard
sudo VBoxClient --draganddrop
sudo VBoxClient --display
sudo VBoxClient --checkhostversion
sudo VBoxClient --seamless

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

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

My 2 cents

  • Make sure you are running latest vagrant (1.3.3 now) + VirtualBox (4.2.18) to avoid bugs.

  • You can use shell script or inline command to install a desktop environment or a light weight window manager

    For example install LXDE on top of Ubuntu 12.04 Precise base box from vagrantbox.es

Vagrant.configure("2") do |config|
  # ... other configuration

  config.vm.provision "shell" do |s|
    s.inline = "apt-get install lubuntu-desktop -y"
  end
end
  • If you build your own vagrant base boxes, make sure you follow the base box packaging instructions or consider tools like packer (or veewee) to automate the build.

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

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.


https://askubuntu.com/questions/300799/does-ubuntu-12-04-lts-32-bit-have-graphic-user-interface/300805#300805

After installing the desktop, you'll also want to install GDM which will let you boot directly into a graphical environment. You'll also want to configure it.

So maybe add this?

Vagrant::Config.run do |config|
    config.vm.provision :shell, :inline => "sudo apt-get install gdm"
    config.vm.provision :shell, :inline => "sudo dpkg-reconfigure gdm"
end