Solution for Slow Ubuntu in VirtualBox 21

Posted by JD 09/14/2012 at 17:00

Last night at an installFest, I helped someone with a Core i7, 6GB of RAM and 300GB free install Ubuntu 12.04 with Unity into a virtual machine. After the install, it was painfully slow. That is an understatement. Every character that I typed didn’t get displayed until about 30 seconds later. To the other person, it seemed that Ubuntu had locked up. He wanted to delete the Ubuntu install and leave. Clearly, something was broken. This was with 12.04.1 32-bit desktop inside the latest available VirtualBox on MS-Windows7 x64..

If I hadn’t seen this myself, I wouldn’t believe it either. Complete instructions follow to speed up VirtualBox for Ubuntu 12.04.1. It should work for prior versions and other Linux-based VMs too.

11/2013 Update

More and more people are running non-LTS Ubuntu systems. This is sad, since 95% of the Ubuntu people have no business using an OS with 6-9 months of support, but whatever. The tips below will prevent Ubuntu Unity 3D from running. That is by design. Inside a VM, 3D graphics is a complete waste. I understand that the desktop recognizes this and switches from using the GPU to doing all those calculations with the CPU if it must. That is also not good, IMHO. Canonical clearly has a different idea about virtualization than people like us.

Now there are methods to use Unity inside a VM. I’ve seen a solution over at AskUbuntu.com that appears to have worked for many people. It addresses the graphics side only, not the diskIO, network IO and other VM tuning shown below.

I have used the setting below on an Ubuntu 13.10 VM under KVM. The settings still work great. The general ideas below apply to all VM solutions, not just VirtualBox.

This will probably be my last update related to VirtualBox. I have switched 100% to KVM + libvirt + virt-manager + nxclient for my desktop VM needs. Just leave off nxclient for servers.

More VBox Details Required Now

It seems there are more instructions required now to make Ubuntu work under VirtualBox. The good news is that we figured them out in about 20 minutes last night. However, a new VirtualBox was just released and is supposed to have expert level interface changes. I haven’t seen it, so these items apply only to the 4.1.xx releases.

VBox Defaults Appear to Have Changed

It appears that many of the formerly reasonable defaults in VirtualBox have been changed. I’ll try to spell out the settings that are most critical again, in the order of the different vbox tabs. Not all of these settings are mandatory, but we didn’t spend too much time trying find the exact minimums.

Critical New VM Wizard Decisions

When it comes to the VirtualBox settings, only 2 of the virtual settings are critical during the new VM wizard. All the others can be easily modified after the install and Linux doesn’t care too much.

  1. When you create the disk storage for the VM, always preallocate the entire amount. DO NOT USE dynamic allocation on spinning HDDs. Be certain that you have defragmented the partition before as well. If you are using an SSD to hold the VM, then you can use sparse allocations. SSD performance is so high that there is not any downside to letting allocations grow dynamically.
  2. Be certain to have VBox emulate an Intel PRO/1000 network card. This is in the Network Advanced Settings and not usually displayed by default. The actual card in the physical machine does not matter. Wifi or wired does not matter.
  3. If you are running a recent Linux, use virtio drivers for both the storage controller and network card. This is more efficient than SATA or the Intel PRO/1000 network cards.
  4. Select the ICH-based chipsets where available. Newer and more standard is better than other options.

Detailed VM Settings

Now we have a new VM, but it hasn’t been booted. Work our way though each of the vertical and horizontal tabs in the VM settings to set and verify correct settings for performance.

  • General
    • Basic
      • Unique name
      • OS – Linux
      • Match the correct flavor – Ubuntu
    • Advanced
      • Snapshot folder – default
      • Clipboard – bidirectional
      • Removable Media – whatever you prefer
      • Mini ToolBar – whatever you prefer
    • Description – whatever you prefer
  • System
    • Motherboard
      • Base memory – 768 to 3.5 GB depending on your desires. Always leave at least 1GB for the hostOS
      • Chipset – ICH9 this is important
      • Extended Features
        • Check – Enable IO APIC (absolutely critical)
        • Uncheck – Enable EFI this is important
        • Check – HW Clock in UTC
        • Check – Enable absolute Pointing device
    • Processor
      • 1 CPU; never allocate more CPU than you need. 2 vCPUs would be a max for a desktop VM and only if you have 3 or more physical Cores.
      • Execution Cap: 100%
      • Check – Enable PAE/NX
    • Acceleration
      • Check – Enable VT-x/AMD-v (this must be supported by the CPU, MB and enabled in the BIOS)
      • Check – Enable nested paging
  • Display
    • Video
      • 128MB video RAM – this is for a desktop. Servers get 9MB
      • 1 monitor
      • Extended Features – disable both 3D and 2D acceleration. Experiment later, if you like.
    • Remote Display – whatever you prefer
  • Storage
    • IDE Controller – whatever.
    • SATA Controller – AHCI If the controller chipset is available, select the latest ICH model this is importantSATA is much, much, much faster than IDE. If your VM supports Virtio, that will be even more efficient AND faster than SATA. Recent Linuxen support virtio.
  • Audio – whatever
  • Network
    • Adapter 1
      • Bridged – I’ve used NAT, but sometimes it just doesn’t work. Start bridged and experiment later with NAT.
      • Name – this is where you select which physical adapter the VM will use. If you have wired and wifi, you’ll want to toggle between them here.
      • Advanced
        • Adapter Type – Intel PRO/1000 MT this is important If your VM supports Virtio, that will be even more efficient AND faster than SATA. Recent Linuxen support virtio.
        • Promisc Mode – deny
        • MAC Address – default is ok.
        • Cable Connected – checked
    • Adapter 2, 3, 4 … unused unless you are expert.
  • Serial – I’ve never used these at all, ever.
  • USB – Not covered here. Until the Guest Additions are installed, doesn’t matter anyway.
  • Shared Folders – I use this all the time, but not when making a fresh VM.

Ok, there you have it. The VBox settings for the best performance. Hopefully, the VirtualBox team hasn’t changed too many names for these settings in the newer versions.

No Unity

Connect the Ubuntu ISO to the CDROM in Storage and start the install. Since we disabled the GPU advanced 2D and 3D acceleration, Unity should not install. This is good. Unity 2D might, which is fine. It doesn’t seem to cause as many issues.

Guest Addition Dependencies

After the OS install, a few things are needed to make VBox nicer. Mainly the Guest Additions.

$ sudo apt-get install build-essential dkms

One of three kernel header packages is required. Run
$ uname -r
and based on the returned string, install one of these:
{}-generic-pae = apt-get install linux-headers-generic-pae
{}-generic = apt-get install linux-headers-generic
{}-server = apt-get install linux-headers-server
Important: Reinstalling Guest Additions must be performed manually if DKMS doesn’t handle it automatically after a new kernel is installed.

Do not use kernel-version specific versions. You want one of the listed choices above so your system will automatically manage updates with every new kernel.

Mount and Install Guest Additions

In the “machine menu”, Select the Devices menu, then Install Guest Additions. For Linux, this just makes the device available. In Ubuntu, the device should be auto-mounted under /media/. Open in File Manager dialog will probably be displayed. It doesn’t matter to me if you use this or not.
  1. Open a terminal
    $ cd /media/VB*
    $ sudo sh ./VBoxLinuxAdditions.run
If there are any errors in this last step, look at the log file and resolve them. Reboot the VM to have the guest additions start working. The main thing these addons provide is nicer mouse integration, better video performance and resizing of the VM window. It really is worth the hassle for any GUI VM. For servers, I don’t usually bother as the mouse and video don’t matter at all.

Now your VM should work with about 95% of native performance. I wouldn’t bother using VirtualBox with any CPU less than a Core 2 Duo or any amount of RAM less than 2GB. It just isn’t worth it.

Here is a PDF of the VBox settings which might make it easier for some.

  1. Bob 09/16/2012 at 15:41

    Thanks for your post! It seems to have helped for my Xubuntu install.

  2. JD 09/22/2012 at 23:44

    My original VirtualBox Performance Settings post. Doing what that post says, my WinXP VM boot times dropped almost 50% – primarily by forcing WinXP from IDE to SATA virtual drivers. For networking, using the Intel PRO/1000 virtual NIC makes a huge difference too – 650Mbps easy.

    The above article has the best settings that I know for performance today, but the other article has a better explanation for why you want certain settings.

    If you are using KVM or Xen hypervisors, the ideas are still the same, just the specifics have changed. Under KVM, use the virtio disk and network drivers. For Linux guest OSes, I think this is the default. For Windows guest OSes, I had to work a little harder to get virtio drivers inside the Windows VM and I’m not certain it performed any better than the SATA and Intel PRO/1000 drivers. I do have a Windows7 Media Center recording TV that runs inside a KVM VM. It works fairly well. Disk and network performance were an issue originally, but I made some tweaks as outlined on this site, and I think both those are providing near-native results now.

    And there is something that I should have explained better in this article. The APIC settings need to be enabled in the BIOS before you do this in a VM. If you don’t, I hope VBox won’t let you set them incorrectly.

    I’m curious, has anyone gotten SPICE working under KVM? If so, do you know of a good guide to accomplish that?

  3. Roberto 10/29/2012 at 14:56

    Awesome!

    I didn’t have to reinstall the whole system again. I cloned my current machine (just in case) and set the configuration as the blog said and it does work. I don’t think the result is perfect but at least tolerable and now I can finish what I was doing.

    The only thing I couldn’t set is turn off the dynamic storage for a static one. Any ideas?

  4. JD 10/30/2012 at 19:21

    @Roberto:

    Having static storage allocation is probably the single most important setup for performance. The way that I’d do it, if I’d done what you did, is …

    • Make a backup of the entire VM. Use whatever backup tool you like, but partimage is probably the best answer. You’ll want to boot from a different OS, but attach the VM image file/storage to it.
    • Add another storage file to the VM – be certain to pre-allocate it 100%.
    • Use dd to mirror the old, sparse storage into the new fully allocated storage. Shutdown the VM.
    • Remove the old storage from the VM, leaving the fully allocated storage for boot.
      That should be it. Good luck.
  5. Bruno Rafael Oliveira 12/04/2012 at 18:17

    On the Guest Addition Dependencies, simply run:

    $ sudo apt-get install build-essential linux-headers-$(uname -r)

    I’m saying this because I’m a newbie and was running:

    sudo apt-get install linux-headers-generic

    (I was neglecting part of the “uname -r” output) and it should be:

    sudo apt-get install linux-headers-3.5.0.17-generic

    Greets,
    B.R.O.

  6. JD 12/04/2012 at 18:27

    @Bruno – actually, you do not want to install the specific headers.
    You want the ones that map to whatever the current kernel is. If you use the `uname -r`, you will never get the updated headers automatically when a new kernel is installed. That will be bad.

    At least that is what I recall today.

  7. harish 12/26/2012 at 04:54

    Thank you so much for the instructions. I spent 5 hours trying different things coming to believe VB won’t work on newly released Win 8. My laptop I bought yesterday (i5, 6 G RAM, 640 G HD) came with Win8 and no other option. Just installed Ubuntu 10.04 and took 2 hours with default settings in VB 4.2.6 r 82870.

    I tried Ubuntu 12.10 with same extreme slow (un-usable) performance. Now after making change from dynamic disk allocation made magic. Also enable EFI did not work for me. It came with menu options as soon as booting started which is beyond my understanding as a desktop system user. So with that now all things are working with much better speed. Thank you so much for sharing your solution.

  8. JD 12/26/2012 at 12:31

    I should point out that if your VM storage is located on SSDs, then you do not need to make fully pre-allocated storage. The SSDs are soooooo fast it just doesn’t matter. I’ve learned about a company that has virtualized all their servers to SSDs into RAID1 sets and use sparse VM storage.

    I believe that EFI should be avoided unless absolutely required. Isn’t that a Mac thing?

  9. Jason 12/30/2012 at 06:09

    Thanks for the post! I’m installing now and hoping the changes will make a difference (I’m sure they will).

    Quick question — When creating the new VM and allocating disk storage, seems that the default is 8 gig. However, I’m then taken to a screen to allocate up to 2TB, which my physical drive doesn’t have.

    Would leaving the default at ~8 gig (fixed) be alright? Or should I have moved the slider up to 2TB?

    (running 4.2.6 r82870)

    Thanks,
    Jason

  10. JD 12/30/2012 at 12:32

    @Jason: When it comes to creating storage for Linux machines OR VMs, there are many, many, many considerations. Most of the time, for virtual machines, I’m creating a single purpose installation, so I allocate the amount of storage that I will need “local” to the VM. For an Ubuntu Desktop, I use 10-15G. No more. Large data is never stored inside the VM – it is stored on the network somewhere.

    Most VMs here get 4G of storage – again, they are single purpose. A few, get more like the email or document management servers – by there nature, more storage is required.

    When it comes to storage I have learned 1 main rule. If you have it, you need to have a place to back it up. If you cannot backup the primary storage, then do not use it. That leads to this corollary – keep storage allocations as small as possible.

    For a desktop with a normal number of apps, 10G should be fine when you start out.

    Here’s the allocation for my daily use desktop (which runs inside a KVM VM):

    $ df .
    Filesystem 1K-blocks Used Available Use% Mounted on
    /dev/vda1 13912144 10090996 3114436 77% /

    Only about 14G is allocated. I’ve been using and migrating data forward on this VM since 2008. On the network, it has access to over 2TB of storage, but that is all on a different machine. The network is the computer.

    For some virtual machines, it is smart to create 2 virtual HDDs.

    • OS+Apps on /
    • Personal settings and $HOME on /home
      This means that swapping out a new OS becomes very easy and makes losing personal data during the update almost impossible.

    Anyway – just some thoughts. I hope it makes sense.

  11. Alan 01/09/2013 at 17:19

    Just wanted to say thanks – these steps worked perfectly for me. My VM had been running great on a Windows host, but when I moved over to a mac host, it ran very sluggishly. This helped quite a bit!

  12. Anon Y Mouse 01/26/2013 at 09:14

    Just wanted to say that this solved incredibly slow video on 12.10 Lubuntu (Ubuntu) 64-bit on Win7 host. I already had guest extensions enabled and I just updated the settings on existing VM and instant success. Thank you

  13. vik 01/28/2013 at 02:55

    thanks a lot

  14. Dale 02/01/2013 at 19:59

    Hip Hop Hooray! Ubuntu finally runs in my VirtualBox. I just allocated the nondynamic disk, enable IO APIC and increased my video memory. What a difference. Thanks for the good work!

  15. JD 02/01/2013 at 20:34

    I’m just happy all the work put into the details of the article are helping people.
    You are all very welcome.

    On Feb 1, I added some additional information to the main article about using virtio and SSDs. Also clarified the method for installing the dependencies to make new kernels automatically update all the headers needed for Guest Additions to work.

  16. Bill 02/14/2013 at 12:53

    Thanks so much for the very detailed instructions and explanation. This is a very excellent resource.

  17. Sigurdur Karl 02/25/2013 at 11:51

    Thanks my Xubuntu is running super fast now! :)

  18. Piotr 03/12/2013 at 06:47

    @John great tips. I had all 4 cores (8 processors) devoted to VM guest Ubuntu on Win7 host, I thought guest-host will share them gracefully. However up to yesterday UI responsivenes was bad. Now I have reduced number of processors to 5 and all performance problems went away. Thanks!

  19. JD 03/12/2013 at 13:47

    @Piotr: 5 cores seems like a HUGE amount for any Linux desktop. I consider 2 cores overkill. Most Linux desktops perform great with 1 current-gen core. My daily-driver is a Linux desktop running inside a VM. It has 1.5G of RAM and 1 core allocated to it. More isn’t always better.

    For CPU, RAM, and disk – only allocate what you need. No more.

    Over allocating CPUs to a client is a common mistake. Never allocate more that is necessary. Start with 1 core, and only if there is a slowdown, add 1 more. Regardless, if you have 5+ cores, always, always, always leave 1 for the hostOS.

    The same applies to RAM. Always, always, always leave the recommended amount of RAM uncommitted for the HostOS. For Windows-Hosts, that is usually 1GB. A little less if Linux, but don’t forget about the virtual-video-RAM used by each VM. For example, if you have 8G of RAM in the system, you should not allocate 7×1G VMs and think that you have left 1G for the hostOS. If you follow the guidelines above, each of those 7 VMs could be allocated 128MB of video-memory each – you’ve just over committed the system RAM and performance will suffer. Even servers would have 9MB of virtual video-RAM allocated, so 7 server (non-GUI) VMs would over commit for a Windows hostOS. These are rules of thumb, so you might find where it works fine for some configurations, but not others. Play in the lab and at home, not on production systems.

    Even for fairly busy servers like Zimbra (F/LOSS MS-Exchange replacement), I only allocate 2 cores. I consider any more to be a waste of resources that other VMs or the host would use better.

    Next month, I’m scheduled to provide a presentation on VirtualBox at the metro area PC-UG, so I’ll be updating all my knowledge about the VirtualBox current settings soon.

  20. Glenn 05/30/2013 at 08:35

    I’ve been doing web dev every day inside an Ubuntu guest, on both a Windows 7 host and more recently an Ubuntu host.

    Most of the time everything would be fine but seemingly randomly everything would just slow to a crawl and the host Ubuntu machine’s CPU interrupt count would go through the roof. The host machine would be running fine but the guest VM would be unusably slow.

    The setup here seems to have fixed my problems…changing to only 1 guest CPU, using virtio drivers and maxing out the video RAM to 128MB are the changes I made.

    Thanks so much!

  21. JD 06/01/2013 at 13:34

    @Glenn – the most important change is really to use a fully pre-allocated virtual HDD. If your Ubuntu guest is not a server, then added video RAM can help, but if it is a server (i.e. no GUI), then that is just wasting RAM.