How to Install and Use Vagrant with VirtualBox 7 on Ubuntu 24.04

When it comes to VirtualBox, it is generally installed on Windows, where virtual machines are typically managed using a graphical interface. However, today we will be installing Vagrant and VirtualBox in a Linux CUI environment. After installation, virtual machines can be easily launched on Windows, but on Linux, you will need to manage them using command-line operations.

In this article, I will explain how to install Vagrant and VirtualBox 7 on Ubuntu 24.04, as well as reference older articles related to this topic.

While it is possible to control virtual machines using commands with VirtualBox alone, the commands can be complex and confusing. Vagrant is a convenient tool that simplifies these difficult commands, so we will install it alongside VirtualBox.

https://www.vagrantup.com/

Let’s start by installing Vagrant. The installation instructions are provided on the official page linked above. The following steps are for installing Vagrant on Ubuntu 22.04.

curl -fsSL https://apt.releases.hashicorp.com/gpg | apt-key add -
apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
apt-get update && apt-get install vagrant

As I updated the article, the installation commands were changed, so please use the commands below. The detailed records of the installation process are provided at the end of the article.

wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update && sudo apt install vagrant

Screenshots before the website update

Screenshots after the website redesign

Next, we will install VirtualBox.

https://www.virtualbox.org/

Find and click on the link for Linux distributions. Right-click on the link for the OS you are installing to obtain the URL. Since then, VirtualBox has been updated to version 7, compatible with Ubuntu 24.04, so we will be installing this version. Naturally, we will be using the installation method suitable for Ubuntu 24.04.

Screenshots from the past

Latest screenshots

If you want to install Vagrant and VirtualBox 7 on Ubuntu 24.04, you can skip this part of the article.

Enter the commands in the black screen after SSH connection. The following section covers installing VirtualBox 6 on Ubuntu 22.04, but I’ve kept it here as a historical reference since it might still be useful. Reviewing error messages later can also be a valuable learning experience. The latest instructions are provided further down.

wget [Paste the URL you copied here]
dpkg -i [Downloaded file name]

An error occurred during the package installation.

root@ubuntu2204:/tmp# dpkg -i virtualbox-6.1_6.1.34-150636.1Ubuntujammy_amd64.deb
Selecting previously unselected package virtualbox-6.1.
(Reading database … Currently, 138,722 files and directories are installed.)
Preparing to unpack virtualbox-6.1_6.1.34-150636.1Ubuntujammy_amd64.deb …
Unpacking virtualbox-6.1 (6.1.34-150636.1Ubuntujammy) …
dpkg: dependency problems prevent configuration of virtualbox-6.1:
virtualbox-6.1 depends on libqt5core5a (>= 5.15.1); however:
Package libqt5core5a is not installed.
virtualbox-6.1 depends on libqt5gui5 (>= 5.14.1) | libqt5gui5-gles (>= 5.14.1); however:
Package libqt5gui5 is not installed.
Package libqt5gui5-gles is not installed.
virtualbox-6.1 depends on libqt5opengl5 (>= 5.0.2); however:
Package libqt5opengl5 is not installed.
virtualbox-6.1 depends on libqt5printsupport5 (>= 5.0.2); however:
Package libqt5printsupport5 is not installed.
virtualbox-6.1 depends on libqt5widgets5 (>= 5.15.1); however:
Package libqt5widgets5 is not installed.
virtualbox-6.1 depends on libqt5x11extras5 (>= 5.6.0); however:
Package libqt5x11extras5 is not installed.
virtualbox-6.1 depends on libsdl1.2debian (>= 1.2.11); however:
Package libsdl1.2debian is not installed.
virtualbox-6.1 depends on libxt6; however:
Package libxt6 is not installed.

ddpkg: error processing package virtualbox-6.1 (–install):
dependency problems – leaving unconfigured
Processing triggers for hicolor-icon-theme (0.17-2) …
Processing triggers for shared-mime-info (2.1-2) …
Errors were encountered while processing:
virtualbox-6.1

I will now install the tools that were reported as not being installed.

apt install libqt5core5a libqt5gui5-gles libqt5opengl5 libqt5printsupport5 libqt5widgets5 libqt5x11extras5 libsdl1.2debian libxt6

An error occurred, and the installation could not proceed. I will follow the instructions and enter the suggested command.

E: Unmet dependencies. Try ‘apt –fix-broken install’ (or specify a solution).

apt --fix-broken install

I encountered the following issues, but I will continue with the VirtualBox installation.

There were problems setting up VirtualBox. To re-start the set-up process, run
/sbin/vboxconfig
as root. If your system is using EFI Secure Boot you may need to sign the
kernel modules (vboxdrv, vboxnetflt, vboxnetadp, vboxpci) before you can load
them. Please see your Linux system’s documentation for more information.

dpkg -i virtualbox-6.1_6.1.34-150636.1~Ubuntu~jammy_amd64.deb

The same issue occurred, but I will proceed to check the version.

There were problems setting up VirtualBox. To re-start the set-up process, run
/sbin/vboxconfig
as root. If your system is using EFI Secure Boot you may need to sign the
kernel modules (vboxdrv, vboxnetflt, vboxnetadp, vboxpci) before you can load
them. Please see your Linux system’s documentation for more information.

VBoxManage -v

WARNING: The vboxdrv kernel module is not loaded. Either there is no module
available for the current kernel (5.15.0-33-generic) or it failed to
load. Please recompile the kernel module and install it by

sudo /sbin/vboxconfig

You will not be able to start VMs until this problem is fixed.
6.1.34r150636

A warning appeared, so I followed the instructions and entered the suggested command.

/sbin/vboxconfig

root@ubuntu2204:/tmp# /sbin/vboxconfig
vboxdrv.sh: Stopping VirtualBox services.
vboxdrv.sh: Starting VirtualBox services.
vboxdrv.sh: Building VirtualBox kernel modules.
This system is currently not set up to build kernel modules.
Please install the gcc make perl packages from your distribution.
This system is currently not set up to build kernel modules.
Please install the gcc make perl packages from your distribution.

There were problems setting up VirtualBox. To re-start the set-up process, run
/sbin/vboxconfig
as root. If your system is using EFI Secure Boot you may need to sign the
kernel modules (vboxdrv, vboxnetflt, vboxnetadp, vboxpci) before you can load
them. Please see your Linux system’s documentation for more information.

An error occurred. Following the displayed instructions, I will install the missing modules.

apt install gcc make perl

After completing the installation, I re-entered the command that previously caused an error. This time, it worked successfully.

/sbin/vboxconfig
VBoxManage -v

We will also install the VirtualBox Extension Pack. Similarly, obtain the link to the extension pack and download the file. Use the VirtualBox-specific command to install the extension pack.

wget [Paste the URL you copied here]
VBoxManage extpack install [Downloaded file name]

Now that everything is set up, let’s try something.

https://app.vagrantup.com/boxes/search

Building everything from scratch can be tedious, so you can search for and use pre-configured environments. You can find various options in Vagrant Boxes. Here, we will set up a Laravel environment in VirtualBox. Since the Laravel environment is already pre-configured, this makes the process easier.

To make it easier to understand, let’s confirm the process using Vagrant and VirtualBox installed on Windows. First, create a suitable folder. For simplicity, I created a directory called “test” directly under the C drive. Then, launch the command prompt and navigate to the folder you just created.

cd\
cd test

While in this folder, enter the command found in Vagrant Boxes.

vagrant init laravel/homestead

This should create a “Vagrantfile” in the folder where you executed the command. We will make a small edit to this file, changing the memory size to 4GB, and then save it. While there are many other settings you can adjust, we will only change the memory size for now. Let’s go ahead and start the virtual machine.

vagrant up

By checking both VirtualBox and the command prompt simultaneously, you can confirm that the commands you entered are reflected in VirtualBox. While you can use other software to establish an SSH connection, you can also enter the virtual machine using Vagrant commands.

vagrant ssh

Vagrant provides many other useful commands, and with the configuration file in place, creating a virtual machine is relatively easy.

Installing Vagrant and VirtualBox 7 on Ubuntu 24.04

How to Install Vagrant on Ubuntu 24.04

  1. What is Vagrant?

First, let’s briefly explain what Vagrant is. Vagrant is a tool designed to simplify the setup and management of virtual machines. You don’t need to remember complex commands, as Vagrant allows you to start virtual machines using simple commands.

  1. Prerequisites

To install Vagrant, you need to have Ubuntu 24.04 installed. Additionally, you must be connected to the internet.

  1. Installation Steps

First, let’s prepare for the installation of Vagrant. Please follow the steps below in order.

3.1 Installing the GPG Key

The GPG key is used to verify the source of the software and ensure it hasn’t been tampered with. Run the following command to install the GPG key for Vagrant.

wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg

This command downloads the official GPG key from HashiCorp (the company that develops Vagrant) and saves it in the /usr/share/keyrings folder.

3.2 Adding the Repository

Next, add the repository where Vagrant’s software packages are stored.

echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list

This command adds the Vagrant repository to your system and saves it as a list in the /etc/apt/sources.list.d folder.

3.3 Updating the Package List

Next, update the package list. This step is necessary to retrieve the latest information from the newly added repository.

sudo apt update

Running this command will allow the system to retrieve the latest package list from the repository.

3.4 Installing Vagrant

Finally, install Vagrant.

sudo apt install vagrant

This command will install Vagrant on your system. Once the installation is complete, you will be able to manage virtual machines using Vagrant.

3.5 Checking the Vagrant Version

To ensure that the installation was successful, run the following command to check the version of Vagrant.

vagrant --version

Running this command will display the installed version of Vagrant. For example, if you see a version number like Vagrant 2.x.x, it confirms that the installation was successful.

How to Install VirtualBox 7 on Ubuntu 24.04

  1. What is VirtualBox?
    VirtualBox is free software for creating and managing virtual machines. With VirtualBox, you can run different operating systems in a virtual environment, allowing you to conduct testing and development without affecting your actual machine.
  2. Prerequisites
    To install VirtualBox, you need to have Ubuntu 24.04 installed. Additionally, an internet connection is required.
  3. Installation Steps
    3.1 Installing the GPG Key
    First, install the GPG key needed to trust the VirtualBox repository. The GPG key verifies the source of the software and ensures that the packages have not been tampered with. Save the GPG key directly to /usr/share/keyrings and refer to it from there when adding the repository.

    wget -qO- https://www.virtualbox.org/download/oracle_vbox_2016.asc | sudo gpg --dearmor -o /usr/share/keyrings/oracle-virtualbox-archive-keyring.gpg

    wget -qO- https://www.virtualbox.org/download/oracle_vbox.asc | sudo gpg --dearmor -o /usr/share/keyrings/oracle-virtualbox-archive-keyring-2.gpg

    These commands add the official GPG key for VirtualBox to your system. They also convert the public key downloaded via wget into binary format using the gpg --dearmor command and save it in the /usr/share/keyrings/ folder.

    3.2 Adding the VirtualBox Repository
    Next, add the repository. To add the repository with the specified GPG key, use the following command.

    echo "deb [signed-by=/usr/share/keyrings/oracle-virtualbox-archive-keyring.gpg] https://download.virtualbox.org/virtualbox/debian $(lsb_release -cs) contrib" | sudo tee /etc/apt/sources.list.d/virtualbox.list

    This command adds the VirtualBox repository corresponding to your version of Ubuntu to your system.

    3.3 Updating the Package List
    After adding the repository, update the package list to retrieve the latest package information.

    sudo apt update

    This command will allow your system to retrieve the package list from the newly added repository.

    3.4 Installing VirtualBox 7
    Next, install VirtualBox 7.

    sudo apt install virtualbox-7.0

    This command will install VirtualBox 7 on your system.

    3.5 Checking the VirtualBox Version
    To ensure that the installation was successful, check the version of VirtualBox using the following command.

    vboxmanage --version

    Running this command will display the installed version of VirtualBox. If you see a version number like 7.0.x, it confirms that the installation was successful.

Reasons for Recommending Installation via a Repository

Installing from a repository is recommended for several reasons:

  • Automatic Updates: Using a repository allows your system to automatically retrieve the latest package information and automatically update installed software.
  • Dependency Management: Installing via a repository ensures that required dependency packages are automatically installed, reducing the need for manual installation work.
  • Security: Packages in the repository are signed with a GPG key, ensuring they have not been tampered with. This reduces security risks.

Handling Errors or Warnings Due to Repository Duplication

When adding a repository, if the same repository is configured multiple times, errors or warnings may occur. Here are some measures to avoid this:

  • Check Existing Repository Lists: Before adding a new repository, check to see if the same repository is already configured.
  • Delete Duplicate Lists: If duplicate repositories are configured, delete the old list to resolve the issue.

Steps to Handle Errors or Warnings Due to Repository Duplication

  1. Check Existing Repository ListsFirst, check which repositories are already configured. List the files in the /etc/apt/sources.list.d/ directory.

    ls /etc/apt/sources.list.d/

    This command will display the list of configured repositories.
  2. Identify Duplicate Repository Lists
    Check if the same repository is configured multiple times in the list. For example, if the VirtualBox repository is configured in multiple files, you will need to delete the duplicate.
  3. Delete Duplicate Repository Lists
    To delete a duplicate repository list, use the following command. For example, to delete a file named archive_uri-https_download_virtualbox_org_virtualbox_debian-noble.list, run the following command:

    sudo rm /etc/apt/sources.list.d/archive_uri-https_download_virtualbox_org_virtualbox_debian-noble.list

    After deletion, update the package list again:

    sudo apt update

    These steps will resolve repository duplication issues and prevent errors or warnings. Knowing this can make managing repositories easier.

How to Start and Connect to VirtualBox Using Vagrant

  1. Initialize Vagrant
    First, prepare a directory for creating the virtual machine, and initialize Vagrant within it. Use the following command to create a Vagrantfile:

    mkdir ~/vagrant_project
    cd ~/vagrant_project
    vagrant init ubuntu/bionic64

    This command will create a Vagrantfile that uses an Ubuntu 18.04 virtual machine named ubuntu/bionic64.
  2. Start the Virtual Machine
    Next, start the virtual machine.

    vagrant up

    This command will start the virtual machine based on the Vagrantfile, automatically downloading and configuring it in VirtualBox if necessary.
  3. Connect to the Virtual Machine
    Once the virtual machine is running, use the following command to connect to the virtual machine via SSH:

    vagrant ssh

    This command connects to the virtual machine using the SSH key that Vagrant automatically configured.
  4. Stop the Virtual Machine
    When you’re done working, you can stop the virtual machine.

    vagrant halt

    This command shuts down the virtual machine.
  5. Destroy the Virtual Machine
    If the virtual machine is no longer needed, you can delete it using the following command:

    vagrant destroy

    This command deletes all data associated with the virtual machine.
Please share if you like it!
TOC