Key Takeaways
To create a virtual machine running the Raspberry Pi OS, you'll need to:
Download the Raspberry Pi OS image.
Launch VirtualBox and create a new virtual machine.
Boot the virtual machine using the Raspberry Pi OS image.
Install the Raspberry Pi OS, then perform any final tweaks, such as setting the screen resolution.
Running a Raspberry Pi in a virtual machine is a useful hack for developers, hobbyists, and anyone who wants to try before they buy. Here’s how to do that in VirtualBox.
Why You Might Need a Virtual Raspberry Pi
The Raspberry Pi is a credit card-sized single board computer that offers a fully-functioning PC experience at an affordable price and impressive specifications. The product line includes various models, starting from the minimalist Pi Pico priced under $10, up to the flagship Raspberry Pi 5, which was announced in September 2023. The Raspberry Pi 5 is available in a 4GB model for $60 and an 8GB model for $80.
Due to their compact size, affordability, and compatibility with Linux distributions, Raspberry Pis have been enthusiastically adopted by hobbyists, makers, and tinkerers. These versatile devices have been used in a wide range of fascinating and innovative projects, surpassing their initial educational market. Often utilized as headless systems, Raspberry Pis harness their computational power for tasks that don't require a display, keyboard, or mouse. Acting as the project's embedded brain, the Raspberry Pi operates autonomously without human interaction. Many individuals have integrated the Raspberry Pi into their own networked appliances and Internet-of-Things devices, utilizing it as the central computing element.
You have the flexibility to use more than headless devices with Raspberry Pi. Most Raspberry Pi models can support high-resolution monitors. In fact, Raspberry Pi models 4 and 5 have two micro-HDMI outputs, allowing them to drive two monitors simultaneously. Even the earlier models 1, 2, and 3 can drive a single monitor. Load them with a Linux distribution designed for ARM hardware, connect a keyboard, mouse, and monitor, and you can utilize it as a desktop PC.
It is true that the earlier models had limited performance, but the newer models are significantly more powerful. Using Raspberry Pi in this way simplifies the development process for the Pi. If you develop on a different platform, such as a traditional desktop PC, you may encounter unexpected issues when transferring your code to the Raspberry Pi for testing.
Software developers require significant computing power, which is limited on the Raspberry Pi. To ensure efficient progress in development, it is essential to reduce compilation times. Additionally, the Raspberry Pi's memory capacity may not be sufficient for your tool chain, and disk-intensive tasks may be hindered due to its reliance on an SD card for storage.
A potential solution is to utilize other hardware to run the Raspberry Pi operating system in a virtual machine. By doing so, you can harness the necessary power to drive it at an optimal speed and customize the amount of RAM and storage space according to your requirements.
Use it to assist in your development efforts and then test your app on another virtual machine that is configured to replicate the exact specifications of the Raspberry device you plan to use for the final software. By creating a snapshot of the virtual machine, you can easily revert back to a clean state between tests.
If you're not a developer, but you're interested in the Raspberry Pi and want to explore its capabilities before making a purchase, running the Raspberry OS in a virtual machine will provide you with a good understanding of its functionalities and suitability for your needs.
Of course, there will be some variations. The hardware of the actual Raspberry Pi will not be tested, therefore, the network throughput and video quality will not accurately reflect that of the real device. Now, let's proceed with the steps to configure a Raspberry Pi virtual machine in VirtualBox.
Downloading the Raspberry Pi Desktop OS
There are several operating systems available for the Raspberry Pi, however, it is important to note that the Raspberry Pi hardware is based on ARM architecture. Unfortunately, VirtualBox does not support emulation of ARM hardware. Therefore, in order to create a virtual machine, it is necessary to select a Raspberry Pi operating system that offers a version compatible with x86 hardware.
The Raspberry Pi OS is the official operating system for Raspberry Pi. It is an adaptation of Debian Linux and is available for download on both PC and Mac platforms. For the purpose of this instruction, we will be using the version compatible with Raspberry Pi Desktop. Ensure to download the operating system and make a note of the location of the ISO image on your computer, most likely in the "Downloads" folder.
Creating The Virtual Machine
The first step is to create the virtual computer that we’re going to install the operating system into.
Start by launching VirtualBox. Then, navigate to the "Machine" tab and choose "New" to create a new virtual machine. For this particular machine, we will need to assign a name to it. Remember, this name is simply a way for VirtualBox to recognize the machine, so feel free to choose any name you prefer. In this case, we will use the name "RaspberryPi."
Please enter the desired directory in the "Folder" field to specify where VirtualBox should store your new virtual machine. In the "ISO Image" field, select the downloaded file from the dropdown menu to navigate to it. Lastly, check the "Skip Unattended Installation" box and proceed by clicking "Next".
The "Hardware" page allows you to allocate the desired amount of RAM and number of CPU cores for this virtual machine.
I selected 8GB of RAM and left the CPU cores at one. Click the “Next” button when you’re ready to proceed.
The “Virtual Hard Disk” page lets you select the size of the hard drive in our VirtualMachine. This will replicate the SD card that the Raspberry Pi uses for storage.
I allocated a 16GB hard drive and disregarded the remaining choices. Allocating 16GB to your hard drive doesn't immediately consume the space, as VirtualBox enables the virtual machine's virtual drive to expand as needed, with a maximum limit of 16GB. To proceed, click the "Next" button.
The “Summary” page shows you a recap of your choices.
Click the “Finish” button to create your virtual machine.
You’ll see a new entry in the VirtualBox application, with the name you’ve just chosen. In our case, it was “Raspberry Pi.”
We’re going to fine-tune some settings in our machine. Select your new virtual machine and click the “Settings” icon.
Select “Display” in the sidebar, and drag the “Video Memory” slider all the way to the right.
This gives the virtual machine more RAM to use for its virtual graphics card.
Select “Network” in the sidebar and choose “Bridged Adapter” in the “Attached to” drop down.
By assigning our virtual machine a network connection within the identical IP range as your real PC, it becomes simpler to SSH onto your virtual machine, if necessary. Furthermore, this facilitates the virtual machine's access to physical devices like printers. Proceed to click the "OK" button located at the bottom of the screen to close the settings dialog.
Now we can launch our virtual machine by clicking the green arrow icon.
Our virtual machine is launched and the ISO image is booted.
Select “Install” from the menu and hit “Enter.” Select your keyboard from the list, and hit “Enter.”
On the “Partitioning Disks” screen select “Guided - use Entire Disk” and hit “Enter.”
We’ve only got one hard drive in this virtual machine. We’re warned it will be wiped of all data. That’s fine, there’s nothing on this brand-new virtual drive.
Hit “Enter” to move on. You’re asked for a partitioning scheme. Make sure “All files in one partition (recommended for new users)” is selected and hit “Enter.”
You’ll see a summary of the partitioning choices you’ve made.
Hit “Enter” to proceed. The screen changes to allow you to opt out. “No” is selected by default.
Select "Yes" and press the "Enter" key. A notification will appear on the screen indicating that the installer is assuming your preference to install the GRUB boot loader on the primary drive of the virtual machine.
It’s the only drive we’ve got, so you can hit “Enter” to accept that location.
On the next screen select “/dev/sda (ata-VBOX_HARDRISK….)” and hit “Enter.”
That tells the installer to use the first partition on the hard drive as the location for the boot loader.
A progress bar will slowly creep along as the installation takes place.
Eventually you’ll see the “Finish the Installation” page.
Make sure “Continue” is highlighted and hit “Enter.” Your virtual machine will reboot.
Booting into Raspberry Pi for the First Time
As your virtual machine is booting you’ll see the usual boot screens just as if you were booting a physical Raspberry Pi. The first screen you’ll see is the GRUB menu.
To complete the configuration of your virtual Pi, you have two options: press "Enter" or simply wait for the menu to automatically disappear. Typically, this process takes approximately 10 seconds.
Upon the initial boot of your virtual machine, you will be prompted to input necessary details.
Press the "Next" button once the initial setup screen appears. On the "Set Country" screen, utilize the provided drop-down menus to designate your preferred country, language, and timezone configurations.
Click the “Next” button to proceed.
On the “Create User” screen, enter the name of your user and create a password for them.
Click on the "Next" button to proceed with the creation of your user account.
The "Update Software" screen verifies the current installation against the software repositories to ensure it is up to date. This process may take some time, but I suggest patiently waiting and enduring it.
Clicking the “Next” button performs the update checks.
Click the “OK” button when the updates have completed.
Click the “Restart” button to apply the changes.
Setting the Screen Resolution
Upon the initialization of your virtual Raspberry Pi, it will automatically log you in and proceed to boot directly into the LXDE desktop. However, you may notice that the desktop appears as a small rectangle occupying the center of your screen, with a resolution set at 800x600.
Setting a more realistic resolution is easy. Click the Raspberry icon in the top left corner.
From the menus, select “Preferences” > “Screen Configuration.”
The “Screen Layout Editor” application will open on your desktop. Right-click on the gray square labeled “virtual1.”
From the menus select “Resolution” and then the resolution you’d like to use. You’ll need to reboot for the changes to take effect.
You're All Set
That's it. You're ready to experiment and explore in your virtual Raspberry Pi.