Today we’re going to learn how to set up a Vagrant Ubuntu base box. This is perfect for those of you who want to avoid the hassle of setting up your own virtual machines from scratch and instead just use someone else’s pre-made one. It’s like buying a car that already has all the features you need, rather than building it yourself out of scrap metal in your garage.
To kick things off: make sure you have Vagrant installed on your machine. If not, go ahead and download it from their website (https://www.vagrantup.com/downloads) and follow the instructions to install it. It’s pretty straightforward, but if you get stuck, just Google “how to install Vagrant” and you should be able to find plenty of helpful tutorials out there.
Once that’s done, let’s create a new directory for our project:
# Create a new directory called "my-project"
mkdir my-project
# Move into the newly created directory
cd my-project
# The "$_" variable holds the last argument from the previous command, in this case, "my-project"
# This allows us to use the directory name without having to type it out again
# However, it is not necessary in this case as we are already in the "my-project" directory
# Therefore, we can simply use "cd my-project" instead of "cd $_"
# Also, it is good practice to use quotes around the directory name in case it contains spaces or special characters
cd "my-project"
This will create a new folder called “my-project” and change into it automatically. Now we can initialize Vagrant in this directory by running the following command:
# This script initializes Vagrant in a new folder called "my-project" and changes into it automatically.
# Create a new folder called "my-project"
mkdir my-project
# Change into the "my-project" folder
cd my-project
# Initialize Vagrant in the current directory with the specified box
vagrant init ubuntu/trusty64
# Explanation:
# The "mkdir" command creates a new folder called "my-project".
# The "cd" command changes into the "my-project" folder.
# The "vagrant init" command initializes Vagrant in the current directory with the specified box, in this case "ubuntu/trusty64".
This tells Vagrant to use an Ubuntu base box with version 14.04 (Trusty Tahr) as its starting point. If you want a different version, just replace “ubuntu/trusty64” with the appropriate URL for your chosen base box.
Now that we’ve initialized Vagrant, let’s add some basic configuration to our new project:
# This script is used to configure Vagrant to use an Ubuntu base box with version 14.04 (Trusty Tahr) as its starting point.
# If a different version is desired, the URL for the appropriate base box can be replaced.
# The first line uses the 'echo' command to add a line of code to the Vagrantfile, specifying the base box to be used.
echo 'config.vm.box = "ubuntu/trusty64"' >> Vagrantfile
# The second line uses the 'echo' command to add another line of code to the Vagrantfile, specifying a private network with a specific IP address.
echo 'config.vm.network :private_network, ip: "192.168.33.10"' >> Vagrantfile
# The '>>' symbol is used to append the output of the 'echo' command to the Vagrantfile, rather than overwriting it.
# The purpose of these lines is to add basic configuration to the new project.
The first line sets the base box to use for our project (the same one we initialized with). The second line adds a private network IP address of 192.168.33.10, which will be assigned to our virtual machine when it’s created. You can change this IP address if you want, but make sure it doesn’t conflict with any other networks on your local machine.
Finally, let’s start the VM and watch as it magically appears before our very eyes:
# This script starts the virtual machine using Vagrant.
# It assumes that the IP address 192.168.33.10 will be assigned to the VM.
# You can change this IP address if needed, but make sure it doesn't conflict with any other networks on your local machine.
# Start the virtual machine using Vagrant.
vagrant up
# This command will create and provision the VM according to the Vagrantfile in the current directory.
# If the VM is already created, it will simply start it.
# If the VM is not created, it will first create it and then start it.
# This command may take some time to complete, depending on the configuration of the VM.
# Once the VM is started, you can access it using SSH or other methods.
# You can also monitor the progress of the VM creation and provisioning by using the Vagrant logs.
# Once the process is complete, the VM will be ready for use.
# Congratulations, you now have a virtual machine up and running!
This will download the base box (if necessary) and create a new virtual machine based on its configuration. Once it’s done, you can SSH into your new Ubuntu VM by running:
# This line uses the "vagrant" command to SSH into the virtual machine.
vagrant ssh
And that’s it! You now have a fully functional Vagrant Ubuntu base box up and running on your local machine. From here, you can install any additional packages or software you need using the standard apt-get commands (or whatever package manager your chosen version of Ubuntu uses).
Hope this helps! Let me know if you have any questions or run into any issues.