173

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?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
PRiera
  • 1,731
  • 3
  • 11
  • 3
  • 1
    I've never had a problem using these [boxes](http://www.vagrantbox.es/). Maybe something went wrong when you packaged your base box. – bfitzpatrick Sep 18 '13 at 21:24
  • 1
    from the naive perspective, if you can develop on the host and not on the VM why are you bothering to install GUI capabalities on the Vagrant VM? why not develop on the host? – Alexander Mills Jan 02 '15 at 03:47
  • Developing on the VM can result in fewer issues when the VM/Vagrant specifications are maintained by the development team to be in sync with their application's target environment. You'll get fewer creeping dependencies and behaviors which are unique to your dev. environment while not being present (or not identical) in the target environment/installation. – Jim Dennis Jul 05 '16 at 09:07
  • Actually your conclusion is not completely right. Vagrant is not about having the dev environment outside of the box. Many people on linux develop in a terminal. And you can do that just fine with ssh-only vagrant boxes. – erikbstack Sep 29 '16 at 10:00

11 Answers11

173

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
Jason Gilmore
  • 3,698
  • 3
  • 23
  • 28
Air
  • 5,084
  • 5
  • 25
  • 19
  • 2
    Just wanted to drop in and say that after about 5 hours of trying to get xfce to work in a fedora 19 vagrant box, the solution was to install the virtualbox guest package (yum install VirtualBox-guest.x86_64). Without that package, xorg couldn't get access to my host machine monitor for some reason. So... Thanks man! – billmalarky Jun 14 '14 at 06:36
  • I discovered when trying this that the step 3 code needs to be contained within the `Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| ... done` method of the Vagrantfile. – neontapir Aug 05 '14 at 22:54
  • 1
    You can provision Xwindows etc. from Vagrant by including the following in your Vagrantfile: #Provision X Windows, VirtualBox Guest, curl and other apt packages config.vm.provision :shell, :inline => "sudo apt-get install -y curl xfce4 virtualbox-guest-dkms virtualbox-guest-utils virtualbox-guest-x11" config.vm.provision :shell, :inline => "sudo VBoxClient-all" – Farrukh Najmi Jun 08 '16 at 16:52
  • 2
    Just a heads up that Ubuntu 16.04 doesn't seem to allow starting XFCE as non-root in this way: https://bugs.launchpad.net/ubuntu/+source/xinit/+bug/1562219 – Air Oct 12 '16 at 19:04
  • 1
    Looks like `startxfce4` can be run as non-root on Ubuntu 16.04 (xenial) after installing `xserver-xorg-legacy` – Laurence Billingham Apr 12 '17 at 20:28
  • with bento/ubuntu-16.04 it gets stuck here - Package configuration┌──────────────────────────┤ Configuring grub-pc ├──────────────────────────┐│ A new version (/tmp/grub.Fol0DxWCxv) of configuration file││ /etc/default/grub is available, but the version installed currently has ││ been locally modified.││││ What do you want to do about modified configuration file grub?││││install the package maintainer's version││keep the local version currently installed ││show the differences between the versions││show a sid – openCivilisation Apr 15 '19 at 06:00
  • See also section **GUI vs. Headless** in [Vagrant documentation](https://www.vagrantup.com/docs/providers/virtualbox/configuration) – Guillaume Husta Sep 30 '22 at 08:04
80

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
Nik
  • 5,801
  • 2
  • 31
  • 23
  • This worked for me with two small adjustments. First I got a 404 when looking for the "ubuntu/vivid64" box, so I changed it to "larryli/vivid64" which I found on vagrantcloud. Second I had to add install the vagrant-proxyconf plugin to manage proxy settings. – Daniel Watrous Mar 04 '16 at 17:12
  • 2
    It screams -> startx is currently not installed?? – prayagupa Sep 15 '16 at 02:32
  • 1
    My bad, Internet was not configured in virtualbox to download the `xfce4`. It works now but is there a way I can skip the login and start the `xfce4` by default? – prayagupa Oct 13 '16 at 16:29
  • @prayagupd bit old, but can you elaborate on "Internet was not configured in virtualbox to download the `xfce4`"? I'm getting the same error when running startx after `vagrant ssh` – Joshua Zastrow May 02 '18 at 15:32
  • 1
    I'm having some trouble, where my virtual box says "startx: command not found". Could someone help me diagnose this? – OOProg Jan 22 '19 at 01:22
14

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
mh8020
  • 1,784
  • 1
  • 11
  • 14
  • On Ubuntu 16.04 I had to install xserver-xorg-legacy to get the XWrapper.config file to exist / be used. – MZB Feb 08 '19 at 16:26
  • Further testing suggests the sed line may not be needed as per the comment in the linked github repo. – MZB Feb 08 '19 at 17:06
  • @mh8020 Thanks for creating the github repo. Very clean and easy to follow, just what i needed. Just one issue: I cannot get sound on the guest Ubuntu system to work! Any idea what could enable this on your current setup? – timmwagener Feb 23 '20 at 19:10
  • Thanks, I couldn't get what the username and password were anywhere else – EHM Sep 08 '20 at 19:48
11

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.
Community
  • 1
  • 1
Terry Wang
  • 13,840
  • 3
  • 50
  • 43
7

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
Andrzej Rehmann
  • 12,360
  • 7
  • 39
  • 38
5

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.

Kief
  • 4,383
  • 4
  • 26
  • 21
2

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
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Anthony Roberts
  • 1,971
  • 1
  • 19
  • 34
2

I've patched Nik's answer a bit to avoid HTTP 404:

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

  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
Stepan Yakovenko
  • 8,670
  • 28
  • 113
  • 206
0

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
Izak Marais
  • 131
  • 4
0

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
Bucket
  • 7,415
  • 9
  • 35
  • 45
scil
  • 1
  • 1
0

I see a few people are having problems with "startx: command not found". I had this too and it was because I was trying login and startx before the first-time provisioning had completed. Be patient, go grab a coffee. Check the original console window to see what is happening especially when the provisioning has finished.