Vagrant Share is a new feature of Vagrant 1.5+ that lets you share your local development environment to the web in literally seconds. You can even set it up to let others SSH into your virtual machine. I not only find this incredibly useful, but it's quite cool as well.
What is Vagrant Cloud?
I finally updated my Vagrant to 1.5+ (1.6.3 to be exact). I never liked to upgrade because I was afraid of things breaking or plugins not being compatible. With Vagrant 1.5+ though, Vagrant promised more stability and a ton of cool new features. It also comes with the new and awesome service Vagrant Cloud. Vagrant Cloud makes collaboration with Vagrant easy for teams small or large. Some things Vagrant Cloud offers are:
- Box Hosting (You may have to host the box yourself)
- Discover Public and Private Boxes
- Vagrant Share
- Vagrant Share Static Names
- Vagrant Share Custom Domain
So, say I need a virtual environment for Node.js, PHP, or Ruby, I can login to Vagrant Cloud, search some shared boxes, and then find one to help me get started with my development environment. Some of these boxes are full-featured and require almost no overhead configuration. This collaboration from community boxes is really cool for small teams who don't have the luxury of full-time Server Admin and Operations people.
Vagrant Share is the other huge part of Vagrant Cloud. Vagrant Share lets you easily share your Vagrant Virtual Machine to the web and make it accessible to anyone. So, say you quickly need to share with a client or team member your progress - rather than setting up a full-blown staging environment, you can just run
vagrant share to get a temporary URL on vagrantcloud.com.
This is neat, but Vagrant actually takes this a a step further. With Vagrant Share you can even share SSH access to your VM with anyone from anywhere in the world! I've heard of some people setting this up while on the same network, but with this, as long as the user is connected to the Vagrant Cloud, it doesn't matter which network they are on. This is super useful for debugging and collaborating. The rest of this tutorial will walk you though how to set this up.
The first thing you need to do is make sure you have the latest Vagrant version installed. I completely uninstalled Vagrant and reinstalled from scratch (including Virtual Box) since it's quick, easy, and clean. Here's the official instructions on uninstalling Vagrant. To uninstall Vagrant on Mac, just run these commands from the terminal:
sudo rm -rf /Applications/Vagrant sudo rm /usr/bin/vagrant sudo rm -rf ~/.vagrant.d
vagrant plugin install vagrant-berkshelf --plugin-version '>= 2.0.1' vagrant plugin install vagrant-hostmanager vagrant plugin install vagrant-omnibus
Create a Vagrant Cloud Account
The next thing you'll need to do is create a Vagrant Cloud account if you already don't have one. Vagrant Cloud is completely free for basic users (which provides plenty of support). If you need some of the more advanced options, they have some very cheap and reasonable membership prices. Everything we'll be doing is under the free tier, however.
Authenticate Your Vagrant Cloud Account with Vagrant
Now that you have successfully signed up for a Vagrant Cloud account, you'll need to authenticate Vagrant Cloud with your install of Vagrant. From the command line, run the following code :
It should prompt you with steps that would look something like this:
That's it! You're now authenticated. For whatever reason, if you ever need to logout, you can just run:
vagrant login --logout
Create an Environment with Vagrant
Now that we have our Vagrant Cloud setup, we'll create a virtual environment so we can share it. We're going to be using the exact same LAMP Stack from the last Vagrant tutorial to make things easy. Make sure you have Berkshelf installed if you didn't follow the last tutorial.
git clone email@example.com:MiniCodeMonkey/Vagrant-LAMP-Stack.git vagrant-share-demo cd vagrant-share-demo vagrant up
Vagrantfileand name the project something else besides "projectname".
After that runs, you can now navigate to http://projectname.local to see your Vagrant LAMP stack is working:
Run the Share Command
Now that everything is setup, all you need to do is run the
vagrant share command:
It will look something like this:
That's all there is to it! Now you can visit the outputted URL and see your VM from any network anywhere in the world. As you start developing your application, it's important that you make sure things like URLs are relative and not absolute.
Since this works by creating a local tunnel, you can make changes and see them reflected immediately. For example, change some text and visit the URL to see it updated instantly. This is really useful for when you need:
- Help from a colleague
- Show or demo for a client
- Debug using a third-party program (e.g.: BrowserStack)
The connection will automatically close after about one hour. To close the connection manually, just enter:
vagrant share --https 443to get an SSL encrypted URL.
HTTP sharing like the example above is great for many reasons, but you can even share SSH access to your VM through Vagrant Cloud. To do this, make sure you terminate the previous share, then enter this command:
vagrant share --ssh
This time when the command runs, you'll be prompted to create a password. After you create your password, Vagrant will actually print the code that you can share to grant SSH access to your VM to someone else. It will look something like this:
vagrant connect --ssh terrible-bear-6896
Now, if someone is authenticated with the Vagrant Cloud and runs that command, they'll get full SSH access to your VM. They'll also have to enter the password you created. This can be really useful for debugging and quickly helping someone fix a bug or issue. Collaborating locally just got a whole lot easier. You can terminate the share the same way you did with HTTP Sharing.
That's all there is to using Vagrant Share. Some people mimic this or get pretty close to it with local tunnel tools or pseudo DNS tools like:
Vagrant Share makes this process easier, seamless, and ultra-fast. It also has the added benefit of allowing people SSH access to a VM from anywhere in the world.
I really enjoy using Vagrant, and it has made my development much more efficient. I imagine as Vagrant Cloud grows, they'll start managing deployment for users, too. So from Vagrant Cloud you can Share temporary links, one-click provision to Digital Ocean or Amazon, and more. I may be wrong, but I really can see them growing Vagrant Cloud to manage servers similar to Larvel's Forge tool does.
Like this article? Follow @whatnicktweets on Twitter