Bash script for installing VirtualBox on Ubuntu 22.04 LTS Desktop

In this post, we will learn how to install VirtualBox on Ubuntu 22.04 LTS Desktop by using bash script. The reason for specifically writing VirtualBox on Ubuntu 22.04 LTS Desktop because we have tested this bash script in our personal DevOps laptop.

Being a DevOps Engineer, we should try to automate the things as much as possible. Either it is your personal laptop/PC or professional infrastructure setup, you must enjoy every chance of doing automation.
We created a simple bash script. If you are beginner in bash scripting, you will find some lines which will give some idea for example fetching Ubuntu Code Name in shell script.

VirtualBox is a virtualization software. We can create Virtual Machines by using VirtualBox. If you are a DevOps practitioner, we highly recommend to use the VirtualBox also. It has so well developed and mature. You can almost do so many practice/study on VirtualBox VM.
Anyway let’s see the bash/shell script for which you come for.

How to create bash/shell script for installing VirtualBox on Ubuntu 22.04 LTS Desktop

We expect you must have basic bash/shell knowledge to understand this script. The script is simple and self explanatory. If you have Linux Basic Command understanding also , this article is easy to follow.

Create The Bash Script File.

Open the terminal and use your file editor to create the bash script file. Here, we are using vim editor.

vim install_virtualbox_Ubuntu-22.04LTS.sh

Write The Bash Script For Installing VirtualBox.

#!/bin/bash
# Author: Sharad Chhetri
# Date: 26-Feb-2023
# Description: This script will help to install the VirtualBox on Ubuntu 22.04 LTS Desktop

_wget_package="wget"
_os_codename=$(lsb_release -c|awk '{print $2}')
_package_name="virtualbox-7.0"

# Check if wget is installed or not. A good example for learner - how to use the if condition. 
if [ $(dpkg-query -W -f='${Status}' $_wget_package 2>/dev/null | grep -c "ok installed") -eq 0 ];
then
  sudo apt-get update -y && sudo apt-get install $_wget_package;
fi

#Update Apt Repo List
echo "deb [arch=amd64 signed-by=/usr/share/keyrings/oracle-virtualbox-2016.gpg] https://download.virtualbox.org/virtualbox/debian $_os_codename contrib" | sudo tee /etc/apt/sources.list.d/virtualbox.list

#Oracle Public Key
wget -O- https://www.virtualbox.org/download/oracle_vbox_2016.asc | sudo gpg --dearmor --yes --output /usr/share/keyrings/oracle-virtualbox-2016.gpg

#Install VirtualBox
sudo apt update -y
sudo apt install -y $_package_name

Set Permission On Bash Script File

Use the given below command to set the permission on your bash script.

Here, Permission structure is like this.

Owner = Full Permission (7)

Group = Read And Execute(5)

Other = No Permission (0)

chmod 750 install_virtualbox_Ubuntu-22.04LTS.sh

Execute Bash Script

./install_virtualbox_Ubuntu-22.04LTS.sh

Once the script is successfully executed and completed, open the VirtualBox. If you are fairly new to Ubuntu then click to Activities>> Type VirtualBox in Search Box .Now click on VirtualBox icon to open.

FAQ

Why should I create bash script for installing VirtualBox

Using bash script for installing VirtualBox saves time because it is a single execution with no human intervention till installation get completed. Bash script learner should practice such small task in their routine to enhance their skills in bash scripting.

Conclusion

In this post we have learned how we can execute our VirtualBox installation work in less time and with less human intervention. The VirtualBox installation bash script is also a good example for bash script learners.

Read Some More Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.