Set up a virtualised Raspbian environment on your favourite computer.
This guide provides everything you need to set up and use a virtual Raspberry Pi operation system (that is, Debian with Raspberry Pi Desktop, from which Raspbian is based).
WHY VIRTUALISE A PI?
As much as we like a physical Pi to work from, there are instances where you might want to use Debian on your laptop, desktop or whatever computer you prefer.
Particularly for portability, virtualising can be the best to mobilise your environment for development while commuting, or need concurrent access to software which cannot run on Raspbian. This is particularly true if developing graphics to use in your software.
If you’re on the same network as your Raspberry Pi (or have set it up for global access), you can fairly easily screen share or SSH into your Pi. That’s great, but isn’t always suitable either.
There are instances where virtualisation provides functionality you won’t get from a Raspberry Pi, or may lack functionality which you would normally find on your Pi.
This might include particular display / hardware which your host machine has (as your desktop/laptop most likely has a better graphics processor than your Raspberry Pi). Sadly however, this also goes for the GPIO.
However, Raspbian itself provides a powerful Python development platform. If your code is ultimately going to be deployed on a Raspberry Pi, virtualising the operating system and environment is a smart move.
WHY NOT INSTALL LOCALLY?
We can hear you asking; “why not install Python locally”? It’s true that for much Python development this might be your best option.
Python is, after all, available cross platform. Whether you fall into the macOS, Windows, or Linux camps, there are Python releases available for you.
While this means your Python itself will run a similar way (even identical depending on what OS and version of Python you use), you might need to lean on hardware, software packages that are part of Debian, and more. So virtualising a Raspberry Pi will get you a closer environment to what you’ll experience on a Raspberry Pi board.
The added benefit of virtualisation is being able to replicate hardware limitations. You can easily set processor and memory limitations that will more closely emulate the Raspberry Pi you’re going to deploy your code on. After all, if you develop code which won’t run due to memory issues, or runs very slowly due to a slower CPU, it’s not going to do you much good when you deploy it.
CREATE A VIRTUAL PI
The steps to do this are fairly straightforward, and you shouldn’t have too much trouble, regardless of your skill level.
Head over to VirtualBox and download and install the client for your OS. There are ready to roll versions for Windows, Mac, and Linux.
The installation is super fast, but follow any prompts you may get. You should then be presented with the VirtualBox dashboard, ready to create our virtual machine.
The VirtualBox dashboard
Before we create our virtual machine, we need a copy of Debian. Go to https://www.raspberrypi.org/downloads/raspberry-pi-desktop/
and download the image for Debian Buster with Raspberry Pi Desktop. This is an official release, so it’s fully supported and maintained.
You can download the Torrent if you’re familiar with that process, but you probably just want the ISO. This is just like having a CD (remember those?)!
Now we have everything we need to create the virtual Raspberry Pi. Go back to VirtualBox (or open it up if you closed it) and select New.
Give your virtual machine a name (Virtual Pi or whatever you like).
For the “Machine Folder”, this is where your virtual machine will live. This can be on your local hard disk, a USB drive with adequate capacity, or a network volume (depending on its type).
Then simply select Linux under Type and Debian (64-bit) Version. You should end up with something looking similar to this:
Next, select the memory size. We suggest matching the Hardware for your Raspberry Pi to create a better testing environment. We selected 1024MB to match our Raspberry Pi 3 B. Naturally, if your intention is to deploy on a larger Raspberry Pi 4, you can adjust this value to match your intended hardware.
One benefit of virtualisation is that you don’t need a dedicated hard disk. However, the operating system needs to think it has one. To do this, select “Create a virtual hard disk now” and click Create.
Choose VDI for file type. This keeps your virtual machine and its contents nicely self-contained and can be moved around later too, making it extra convenient to use.
Next, choose the disk size you’d like to use. We selected a Fixed size with the default 8GB here, which is plenty for our testing purposes. Ideally, this should also match what you intend to deploy in the end, so if you’re going to use 8GB SD cards, this is a logical choice. Naturally, change it to suit your application and plan.
We now have a virtual machine but it doesn’t have an operating system. We can now install the Debian image, just like you would on a computer.
Click Start to turn on the virtual machine. Then you should be presented with a dialog to insert a virtual disk file. Click the browse folder icon and navigate to the Debian image you downloaded earlier then click Start.
Now there are two options here; you can run directly from the ISO file or you can install it properly onto the virtual hard disk, just like you would on a real Pi.
Note: you might get prompted the first time you click into your virtual machine. It explains how to get in and out of the virtual machine with your mouse. Different operating systems do handle this differently, so you can usually select “Do not show this message again” and be on your way. However, have a read first so you don’t get stuck inside your machine (sometimes a problem when running fullscreen).
SINGLE USE INSTALLATION
If you only need to test something in a fresh RPi environment for some reason, then you may not need to create an actual installation. If this is the case, then you can effectively boot from the ISO file into memory each time. This means that no changes are saved, and it’ll be like a brand new installation each time.
However, it does mean that you can’t even restart the virtual machine. If you run updates or apply changes, they’ll also be lost.
So this has limited use, but may be useful for you at some point. If you want to do that, you can simply select the “Run with persistence” on the boot screen.
The machine should now boot with Debian Desktop, just like your Raspberry Pi!
You’ll be prompted to set up the timezone and such, but remember these settings don’t apply with this boot style, so you’ll have to do it each time.
You might feel as though this is pointless! However, if you’re testing installation scripts or similar, it can be incredibly useful to execute code on a totally fresh installation, without having to go through installation steps each time.
If you want to be able to save files to your desktop, configuration changes to your filesystem and use it just like a regular Pi (which is fairly likely), you need to go through the installation steps. This is just like installing from a NOOBS SD card or similar. When presented with the boot screen, select “Install”. Naturally, you can “Install with speech synthesis” if you want, to add the extra packages, but we’re not covering that here.
The system will automatically load additional components required for installation, which handle hard disk partitioning. The partition manager can sometimes be a challenge, but only for complex configurations. Here we just want to use the entire disk.
It is also possible to use a variety of partitioning schemes, but they don’t provide a lot of use for us on an RPi, so just put all files into the one partition.
The system will ask you to confirm everything before the changes are actually applied (no changes to the filesystem are made until you confirm).
That is... a double confirm. After all, if you had contents on the drive you needed to keep, this double confirmation might save you! It may seem silly on a virtual machine where we just created the virtual disk, but this operating system is unaware it’s in a virtual environment, so the partition manager is the same as on any other computer.
The system will take a little while to format your disk, depending on the size of partition and disk speed.
The files will actually be extracted and copied to the virtual disk. This process generally takes a while, so go make a coffee or watch some cat videos while it happens. It goes slower when you watch it - proven science!
The final part of the installation is the boot loader installation. This is what allows the operating system to actually boot without anything else (otherwise you’ll get the dreaded black screen with a message asking you to insert a bootable disk). You can separate the boot loader, however, we want this virtual machine to boot normally like a Pi would in most instances. So we’ll install it onto the same disk.
When it prompts you to install the boot loader, confirm Yes (it’s generally pre-selected).
Select the partition with the Raspbian installation on it (generally /dev/sda) and hit enter.
The installation will continue and all going well, it’ll move on to finalising the installation process.
Once complete, you’ll be asked to restart, which will ensure it boots from the new installation.
It’ll run some cleanup on the filesystem before doing so, let it do what it needs to.
If you’ve ever installed an operating system from a computer before, you’ll recall that you usually need to eject the CD, or more commonly now, remove the USB media you started from. This ensures that the system uses the correct boot loader.
Occasionally, for some reason, the ISO file you used at the beginning will remain attached to your virtual machine.
This is effectively the same as leaving the installation CD in the drive. If you don’t remove it, the system will try and do this all over again.
It’s worth going into Machine -> Settings -> Storage, and ensure that under Controller: IDE, it shows as Empty. If it doesn’t manually remove the ISO disk image or you might not be able to boot your virtual machine from the installation.
If everything went as expected, you’ll see a regular Linux boot screen, as is often familiar on a Raspbian installation. Allow it to boot and you should end up with the familiar Raspberry Pi Desktop.
You may notice that the IP address shown on the startup screen isn’t from your normal address pool. This is because VirtualBox automatically configures a NAT (Network Address Translation) to create a new IP pool which routes through your host computer’s IP. You can change this to use an IP from the network if you prefer, which may be useful for certain circumstances. NAT will provide you with general internet access and only needs to be changed if you need more advanced network access.
Follow the prompts as you would with any Raspberry Pi installation, and you’re ready to go!
WHERE TO FROM HERE?
You can explore the functionality that exists in VirtualBox, which allows you to save snapshots and other various features, which are exceptionally handy when you’re modifying the system for some functionality. It allows you to snap restore back to a certain point, so you can boot it as though no changes were made. Especially useful if you made a critical error which caused the system to crash!