
{"id":7047,"date":"2022-05-17T16:32:37","date_gmt":"2022-05-17T16:32:37","guid":{"rendered":"https:\/\/rax-stg-wp-aws.com\/blogs\/basics-of-hashicorp-vagrant\/"},"modified":"2026-11-03T19:27:41","modified_gmt":"2026-11-03T19:27:41","slug":"basics-of-hashicorp-vagrant","status":"publish","type":"blogs","link":"https:\/\/www.rackspace.com\/de\/blogs\/basics-of-hashicorp-vagrant\/","title":{"rendered":"Basics of HashiCorp Vagrant"},"content":{"rendered":"<p>As per the Vagrant official <a href=\"https:\/\/www.vagrantup.com\/intro\">website<\/a>, \u201cVagrant is a tool for building and managing virtual machine environments in a single workflow. With an easy-to-use workflow and focus on automation, Vagrant lowers development environment setup time, increases production parity, and makes the works on my machine excuse a relic of the past.\u201d<\/p>\n<p><strong>Introduction<\/strong><\/p>\n<p>In this blog, you will see how easily and quickly you can spin up multiple virtual machines. This allows configuring the environment with desired tools without affecting the host machine. Vagrant supports multiple virtualization providers such as <strong>VirtualBox<\/strong>, <strong>VMware Fusion<\/strong> and <strong>Hyper-V.<\/strong><\/p>\n<p><strong>Prerequisites:<\/strong><\/p>\n<ol>\n<li>Install the latest version of <a href=\"https:\/\/www.vagrantup.com\/docs\/installation\">Vagrant<\/a><\/li>\n<li>Install any virtualization provider tool \u2013 <a href=\"https:\/\/www.virtualbox.org\">VirtualBox<\/a>, <a href=\"https:\/\/customerconnect.vmware.com\/downloads\/get-download?downloadGroup=FUS-PUBTP-2021H1\">VMware Fusion<\/a>&nbsp; <a href=\"https:\/\/docs.microsoft.com\/en-us\/virtualization\/hyper-v-on-windows\/quick-start\/enable-hyper-v\">Hyper-V<\/a><\/li>\n<\/ol>\n<p><a href=\"https:\/\/learn.hashicorp.com\/tutorials\/vagrant\/getting-started-index?in=vagrant\/getting-started\">Reference Link<\/a><\/p>\n<p>In my example, I am using a VirtualBox tool.<\/p>\n<p><strong>Initialize your vagrant project<\/strong><br \/>\nCreate a directory and change it into that directory. This directory will act as the root directory for your project. Many of the vagrant configurations will be relative to this directory.<\/p>\n<p>\n{{&lt; highlight bash &gt;}}<br \/>\nmkdir vagrant_dir\/<br \/>\ncd vagrant_dir\/<br \/>\n{{&lt; \/highlight &gt;}}<\/p>\n<p><strong>Create your first virtual machine<\/strong><\/p>\n<p>The following command will create a *Vagrantfile* and download Ubuntu 18.04 LTS 64-bit base image on your host. For this version of Ubuntu image, you need to run it once.<\/p>\n<p>&#8220;<em>vagrant init generic\/centos7&#8243;<\/em><\/p>\n<p>Check the file named <strong>Vagrantfile&nbsp;<\/strong>created in the current directory<\/p>\n<p>&#8220;ls -al&#8221;<\/p>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2026\/blog_article_images\/26214-body-Picture1.png\" alt=\"vagrant 1\" \/><\/p>\n<p>The Vagrantfile consists of default configurations required to launch a virtual machine. In this file, you can define the type of machine and resources you want to run and can pass scripts to install tools and OS configurations before the virtual machine is ready to use.<\/p>\n<p>Run the following command to start the virtual machine<\/p>\n<p><em>&#8220;vagrant up&#8221;<\/em><\/p>\n<p>\nWith just two commands, you got your virtual machines started, and ready for use.<\/p>\n<p>Run the following command to check the status of your virtual machine:<\/p>\n<p><em>&#8220;vagrant status&#8221;<\/em><\/p>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2026\/blog_article_images\/26214-body-Picture2.png\" alt=\"Vagrant 2\" \/><\/p>\n<p><strong>Connecting to a new virtual machine<\/strong><\/p>\n<p>To login\/ssh into your virtual machine, run:<\/p>\n<p><em>&#8220;vagrant ssh&#8221;<\/em><\/p>\n<p><strong>Terminate SSH session<\/strong><\/p>\n<p>To terminate the SSH session press *COMMAND+D* in mac (in other OS *CTRL+D*) or type `logout` or `exit`<\/p>\n<p><strong>Suspend the virtual machine<\/strong><\/p>\n<p><em>&#8220;vagrant suspend&#8221;<\/em><\/p>\n<p><em>&nbsp;<\/em><strong>Forcefully shut down the virtual machine<\/strong><\/p>\n<p><em>&#8220;vagrant halt&#8221;<\/em><\/p>\n<p><strong>Destroy your virtual machine<\/strong><\/p>\n<p>To destroy your virtual machine run the following command. This will terminate your virtual machine. To run your virtual machine again just run vagrant up.<\/p>\n<p>&#8220;<em>vagrant destroy<\/em>&#8220;<\/p>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2026\/blog_article_images\/26214-body-Picture3_1.png\" alt=\"Vagrant 3\" \/><\/p>\n<p><strong>Vagrant Box<\/strong><\/p>\n<p>\nTo start a virtual machine, you need one base image. In vagrant, these base images are called **boxes**. You need to download a desired box only once, then Vagrant uses this box to quickly clone a virtual machine. Any changes to this box will apply only to the copy of the box and the original box is left unchanged.<br \/>\nTo quickly add a box without creating a Vagrantfile, you can run the following command:<\/p>\n<p>&#8220;vagrant box add generic\/centos7&#8221;<\/p>\n<p>To get a catalog of boxes go to <a href=\"https:\/\/app.vagrantup.com\/boxes\/search\">HashiCorp\u2019s Vagrant cloud<\/a>&nbsp;You can also host your custom box here.<\/p>\n<p>If you want to manually create a script file and add a box in it, write the following code in a file named <strong>Vagrantfile<\/strong>.<\/p>\n<p>&#8220;vim Vagrantfile<br \/>\nVagrant.configure(&#8220;2&#8221;) do |config|<br \/>\n&nbsp; config.vm.box = &#8220;generic\/centos7&#8221;<br \/>\n<strong>&nbsp;To add a specific version<\/strong><br \/>\n&nbsp; config.vm.box_version = &#8220;3.6.12&#8221;<br \/>\n&nbsp; Below line will sync your host directory<br \/>\n&nbsp; to a VM directory.<br \/>\n&nbsp; <strong>First argument is a host path<\/strong><br \/>\n&nbsp; <strong>Second argument is a VM path<\/strong><br \/>\n&nbsp; <em><strong>config.vm.synced_folder &#8220;.\/&#8221;, &#8220;\/vagrant_data&#8221;<br \/>\nend&#8221;<\/strong><\/em><\/p>\n<p><strong>Start the virtual machine<\/strong><\/p>\n<p><em>&#8220;vagrant up<br \/>\nvagrant status<br \/>\nvagrant ssh&#8221;<\/em><\/p>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2026\/blog_article_images\/26214-body-Picture4_2.png\" alt=\"Vagrant 4\" \/><\/p>\n<p>&nbsp;<strong>Synchronize host and guest files<\/strong><\/p>\n<p>There are times when you want to copy some files to and from your host to the virtual machine. It can be some scripts or your entire project directory. This functionality is quite handy when you want to edit your program files in your host machine on your favourite IDE and run them inside your virtual machine.<br \/>\nBy default, vagrant syncs the Vagrantfile with the virtual machine at *path \/vagrant*. This path is different from the one you will be after SSH into the virtual machine *(\/home\/vagrant).*<br \/>\nTo test this feature, create a file\/folder inside the virtual machine at location \/vagrant and check the same in your host machine.<\/p>\n<p><em>&#8220;ls -l \/vagrant_data\/<br \/>\ntouch \/vagrant_data\/myfile<br \/>\nexit<br \/>\nls&#8221;<\/em><\/p>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2026\/blog_article_images\/26214-body-Picture5_1.png\" alt=\"Vagrant 5 \" \/><\/p>\n<p>If syncing is not working, then follow <em>the troubleshooting tip #1<\/em>&nbsp;at the end of the blog.&nbsp;<\/p>\n<p><strong>Provision a custom virtual machine<\/strong><\/p>\n<p>With the following Vagrantfile, you need to pre-install apache web server and configure it before you login in to the VM. This is useful when you want to automatically install desired tools every time you start your virtual machine.<\/p>\n<p><strong>Configure Vagrant File<\/strong><\/p>\n<p><em>&#8220;vim Vagrantfile<br \/>\nVagrant.configure(&#8220;2&#8221;) do |config|<br \/>\n&nbsp; config.vm.box = &#8220;hashicorp\/bionic64&#8221;<br \/>\n&nbsp; config.vm.provision &#8220;shell&#8221;, path: &#8220;scripts\/bootstrap.sh&#8221;<br \/>\n&nbsp; config.vm.synced_folder &#8220;.\/&#8221;, &#8220;\/vagrant&#8221;<\/em><\/p>\n<p><em>end<\/em><\/p>\n<p><strong>Create a directory in your host machine and add an index file<\/strong><\/p>\n<p>&#8220;<em>mkdir html\/<br \/>\nvim html\/index.html<br \/>\n&lt;html&gt;<br \/>\n&nbsp; &lt;body&gt;<br \/>\n&nbsp; &nbsp; &lt;h3&gt;Welcome to vagrant tutorial !!!&lt;\/h3&gt;<br \/>\n&nbsp;&lt;\/body&gt;<br \/>\n&lt;\/html&gt;<\/em>&#8220;<\/p>\n<p><strong>Create a script to configure and start the apache web server<\/strong><\/p>\n<p><em>&#8220;mkdir scripts\/<br \/>\nvim scripts\/bootstrap.sh<\/em><\/p>\n<p><em>#!\/usr\/bin\/env bash<br \/>\napt-get update<br \/>\napt-get install -y apache2 w3m<br \/>\nif ! [ -L \/var\/www ]; then<br \/>\n&nbsp; rm -rf \/var\/www<br \/>\n&nbsp; ln -fs \/vagrant \/var\/www<br \/>\nfi&#8221;<\/em><\/p>\n<p>If you are provisioning this virtual machine for the first time, then run<\/p>\n<p><em>&#8220;vagrant up&#8221;<\/em><\/p>\n<p>Else if you have already run the above command, and your virtual machines are in a running state, then instead of destroying and recreating virtual machines you can just reload the changes with<\/p>\n<p>&#8220;vagrant reload &#8212;provision&#8221;<\/p>\n<p>\nIn case you get the following error then follow <em>troubleshooting tip #1<\/em><\/p>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2026\/blog_article_images\/26214-body-Picture6_0.png\" alt=\"Vagrant 6\" \/><\/p>\n<p>You need to run the following commands for accessing the apache webserver&nbsp;:<\/p>\n<p><em>&#8220;vagrant ssh<br \/>\ncurl http:\/\/localhost:80<br \/>\nw3m http:\/\/localhost:80&#8243;<\/em><\/p>\n<p><strong>Port forwarding configuration<\/strong><\/p>\n<p>Vagrant port forwarding allows you to access a port on the virtual machine by linking it to a port on the host machine. Now let\u2019s see how it works. You will access a port on the host machine, and all the network traffic is internally routed to the configured port on a virtual machine.<\/p>\n<p>Modified Vagrantfile with port forwarded looks like the following (highlighted port forwarded code)<\/p>\n<p><em>&#8220;exit<br \/>\nvim Vagrantfile<br \/>\nVagrant.configure(&#8220;2&#8221;) do |config|<br \/>\n&nbsp; config.vm.box = &#8220;hashicorp\/bionic64&#8221;<br \/>\n&nbsp; config.vm.provision &#8220;shell&#8221;, path: &#8220;scripts\/bootstrap.sh&#8221;<br \/>\n&nbsp; config.vm.synced_folder &#8220;.\/&#8221;, &#8220;\/vagrant&#8221;<br \/>\n&nbsp; config.vm.network &#8220;forwarded_port&#8221;, guest: 80, host: 1234<br \/>\nend&#8221;<\/em><\/p>\n<p>Reload your virtual machines to let your changes take effect&nbsp;<\/p>\n<p>Now try to access your webserver from a browser in your host machine<\/p>\n<p>[http:\/\/localhost:1234](http:\/\/localhost:1234)<\/p>\n<p><strong>List Box Files<\/strong><\/p>\n<p><em>&#8220;vagrant box list&#8221;<\/em><\/p>\n<p><strong>To remove a particular box file<\/strong><\/p>\n<p><em>&#8220;vagrant box remove hashicorp\/bionic64<br \/>\nvagrant box remove generic\/centos7&#8243;<\/em><\/p>\n<p><strong>Troubleshooting Tips:<\/strong><\/p>\n<ol>\n<li>If syncing between host and guest is not working, then install vagrant-vbguest plugin.<\/li>\n<\/ol>\n<p><em>Note: This is not a vagrant official plugin<\/em><\/p>\n<p><em>&#8220;vagrant plugin install vagrant-vbguest<br \/>\nvagrant reload &#8211;provision&#8221;<\/em><\/p>\n<p><strong>Conclusion<\/strong><br \/>\nBy using HashiCorp\u2019s vagrant tool, you can easily create and manage virtual machines and automate the process of configuring virtual machines. For more functionalities of Vagrant visit the official website of HashiCorp\u2019s Vagrant <a href=\"https:\/\/www.vagrantup.com\/docs\">here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Basics of HashiCorp Vagrant<\/p>\n","protected":false},"author":2,"featured_media":7049,"menu_order":0,"comment_status":"open","ping_status":"open","template":"","format":"standard","meta":{"content-type":"","nid":"26214","content_type":"blog","created_by":"10130","authored_on":"2023-11-05T17:32:37+00:00","field_article_industries":"","tag_category":"Technical","field_blog_author_text":"by Bachu Paul, DevOps Engineer, Rackspace Technology","seo_description":"Easily set up multiple virtual machines with Vagrant. Configure your desired environment swiftly, without impacting your host machine. Read blog.","field_featured_image":"","insights_image":"\/wp-content\/uploads\/2026\/blog_technical_images\/26214-insights_image-devops-technical-blog-1-insights.jpg","field_partners":"","rxt_platforms":"","product":"VMware Server Virtualization","publisheddate":"2022-05-17T16:32:37","rxt_solutions":"Application Management & Operations","opengraph_tag":"","x_tag":"","teaser_flag":"Technical Blog","seo_titile":"HashiCorp Vagrant Development | Rackspace Technology","field_topics":"","field_slug":"\/basics-of-hashicorp-vagrant","footnotes":""},"categories":[178],"tags":[204],"class_list":["post-7047","blogs","type-blogs","status-publish","format-standard","has-post-thumbnail","hentry","category-blog","tag-technical"],"_links":{"self":[{"href":"https:\/\/www.rackspace.com\/de\/wp-json\/wp\/v2\/blogs\/7047","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rackspace.com\/de\/wp-json\/wp\/v2\/blogs"}],"about":[{"href":"https:\/\/www.rackspace.com\/de\/wp-json\/wp\/v2\/types\/blogs"}],"author":[{"embeddable":true,"href":"https:\/\/www.rackspace.com\/de\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rackspace.com\/de\/wp-json\/wp\/v2\/comments?post=7047"}],"version-history":[{"count":2,"href":"https:\/\/www.rackspace.com\/de\/wp-json\/wp\/v2\/blogs\/7047\/revisions"}],"predecessor-version":[{"id":8794,"href":"https:\/\/www.rackspace.com\/de\/wp-json\/wp\/v2\/blogs\/7047\/revisions\/8794"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.rackspace.com\/de\/wp-json\/wp\/v2\/media\/7049"}],"wp:attachment":[{"href":"https:\/\/www.rackspace.com\/de\/wp-json\/wp\/v2\/media?parent=7047"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rackspace.com\/de\/wp-json\/wp\/v2\/categories?post=7047"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rackspace.com\/de\/wp-json\/wp\/v2\/tags?post=7047"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}