How to Install Vagrant on Ubuntu 18.04
26 June 2020 | Category : Ubuntu
What is Vagrant?
Vagrant is a tool which is developed by HashiCorp, this tool helped Linux System Administration, DevOps, and Web Developer to build a virtual environment of their projects in a single workflow.
At this time Vagrant is not only one tool to create a virtual environment, but Vagrant is a popular tool and one tool more its name Docker which has the same function as a Vagrant to create a virtual environment.
In this article, we will explain to you about How to Install Vagrant on Ubuntu 18.04 and how Getting Started to use Vagrant on your local environment.
Install Vagrant on Ubuntu 18.04
Before we start Install Vagrant on Ubuntu 18.04 you should complete these prerequisites
- Account user with sudo privileges
- Accessing Terminal Window or Console command line
To install Vagrant on your local environment, you can follow this step :
1. Install Virtualbox
Maybe in this first step, you have the question Why should install Virtualbox? for that question, we will tell you. Vagrant needs providers which can provision machines on top like Virtualbox, KVM, Hyper-V, and other application Virtualization, this is why vagrant still need Virtualbox as the provider. But it also doesn’t obstruct if Vagrant can provide machines with cloud providers like AWS, Digital Ocean, Vultr, and the others. So, to install Virtualbox you can follow this command :
sudo apt install -y virtualbox
This command will automatically download package virtualbox from the repository of Ubuntu 18.04
2. Install Vagrant
To Install Vagrant you have two ways (you can choose one of two ways) :
Option 1. Install from the command line with apt install, and
Option 2. Install from a binary file.
Option 1: Install from the command line
If you want install Vagrant from the command line you can follow this command :
sudo apt install -y vagrant
This command will automatically download package vagrant from the repository of Ubuntu 18.04
Option 2: Install from Binary file
If you choose to install from the binary file you must download a package of Vagrant first. You can visit https://www.vagrantup.com/downloads.html, in right there choose Debian to get the link download of package Vagrant or you can follow this command step to download :
sudo wget -O vagrant.deb https://releases.hashicorp.com/vagrant/2.2.9/vagrant_2.2.9_x86_64.deb
Then when you finish downloading the package you can start to install with this command :
sudo dpkg -i vagrant.deb
3. Verify Vagrant Installation
After finished install vagrant you can verify the installation it was successful or not, you can check it with this command :
The command output will look like this :
vagrant -v Vagrant 2.2.9
Now, Vagrant has been successfully installed in your local environment. To get started use Vagrant as a virtual environment in your local environment you can follow this step.
1. Create a directory project
First, to start creating a virtual environment you need to create a directory of your project. This directory will save the file of Vagrant, every you create a virtual environment with Vagrant it will generate a file with the name VagrantFile. VagrantFile uses Ruby as a default programming language and this file will describe the configuration to provision the virtual environment.
So, let’s create a directory and switch to a directory project with this command :
mkdir ~/vagrant-project && cd ~/vagrant-project
This command will create a directory vagrant-project and switch to the directory vagrant-project.
2. Initialize VagrantFile
Next, after the directory is created you need to initialize a new VagrantFile, to do that you can run this command :
vagrant init centos/8
This command will initialize new VagrantFile with vagrant boxes CentOS 8 as a virtual environment
The command output of vagrant init it will look like this :
vagrant init centos/8 A `Vagrantfile` has been placed in this directory. You are now ready to `vagrant up` your first virtual environment! Please read the comments in the Vagrantfile as well as documentation on `vagrantup.com` for more information on using Vagrant.
To see vagrant boxes you can check on this page https://app.vagrantup.com/boxes/search. Vagrant boxes are the package format of the Vagrant environment which inside that is the platform to build a virtual environment.
3. Start the virtual environment
After initializing VagrantFile with the vagrant boxes. Now, you can try to start up the Vagrant virtual environment with this command :
When vagrant up running for the first time it will be downloading of vagrant boxes into your local environment, so this is maybe will take a long time to wait until finish downloading the vagrant boxes.
The output of that command looks like this :
vagrant up Bringing machine 'default' up with 'virtualbox' provider... ==> default: Box 'centos/8' could not be found. Attempting to find and install... default: Box Provider: virtualbox default: Box Version: >= 0 ==> default: Loading metadata for box 'centos/8' default: URL: https://vagrantcloud.com/centos/8 ==> default: Adding box 'centos/8' (v1905.1) for provider: virtualbox default: Downloading: https://vagrantcloud.com/centos/boxes/8/versions/1905.1/providers/virtualbox.box ==> default: Box download is resuming from prior download progress default: Download redirected to host: cloud.centos.org default: Calculating and comparing box checksum... ==> default: Successfully added box 'centos/8' (v1905.1) for 'virtualbox'! ==> default: Importing base box 'centos/8'... ==> default: Matching MAC address for NAT networking... ==> default: Checking if box 'centos/8' version '1905.1' is up to date... ==> default: Setting the name of the VM: centos8_default_1593224971774_27471 ==> default: Pruning invalid NFS exports. Administrator privileges will be required... Password: ==> default: Clearing any previously set network interfaces... ==> default: Preparing network interfaces based on configuration... default: Adapter 1: nat ==> default: Forwarding ports... default: 22 (guest) => 2222 (host) (adapter 1) ==> default: Booting VM... ==> default: Waiting for machine to boot. This may take a few minutes... default: SSH address: 127.0.0.1:2222 default: SSH username: vagrant default: SSH auth method: private key default: default: Vagrant insecure key detected. Vagrant will automatically replace default: this with a newly generated keypair for better security. default: default: Inserting generated public key within guest... default: Removing insecure key from the guest if it's present... default: Key inserted! Disconnecting and reconnecting using new SSH key... ==> default: Machine booted and ready! ==> default: Checking for guest additions in VM... default: No guest additions were detected on the base box for this VM! Guest default: additions are required for forwarded ports, shared folders, host only default: networking, and more. If SSH fails on this machine, please install default: the guest additions and repackage the box to continue. default: default: This is not an error message; everything may continue to work properly, default: in which case you may ignore this message. ==> default: Rsyncing folder: /Users/geekid/Github/vagrant-rules/centos8/ => /vagrant
4. Remote the Virtual Environment
When the virtual environment is running up, and you want to connect/remote the virtual environment for manage, you can use this command :
This command will be accessing the host of the virtual environment with SSH. And now you can work on your virtual environment.
5. Shutdown the Virtual Environment
To shut down/power off the virtual environment, you can follow this command :
This command will shut down/power off the virtual environment.
6. Destroy the Virtual Environment
If you want to destroy the virtual environment or not used that again, you can run this command :
This command will destroy/deleted the current virtual environment
Vagrant can create a virtual environment to help Linux Administrator, DevOps, and Web Developer to build their virtual environment of projects. In this article, you have learned about How to Install Vagrant on Ubuntu 18.04 and Getting Started to use Vagrant on your local environment.