Tutorial

How To Use DigitalOcean as Your Provider in Vagrant on an Ubuntu 12.10 VPS

Published on August 1, 2013
How To Use DigitalOcean as Your Provider in Vagrant on an Ubuntu 12.10 VPS

This tutorial is out of date and no longer maintained.

About Vagrant

Vagrant is a powerful open source software for configuring and deploying multiple development environments. It is designed to work on Linux, Mac OS X, or Windows and although it comes with VirtualBox for the virtualization needs, it can be used also with other providers such as VMware or AWS.

In this tutorial, we will set up Vagrant to use DigitalOcean as a provider. This means that you will be able to use DO droplets (VPS) as development machines to deploy from Vagrant. The tutorial assumes you have already installed Vagrant on your environment and it is recommended you consult two previous articles about Vagrant:

  1. How to Install Vagrant on a VPS Running Ubuntu 12.04
  2. How to Use Vagrant on Your Own VPS Running Ubuntu

Installing the DigitalOcean: Vagrant Integration Plugin

To install the plugin, you have to follow two quick steps. First, run the command of installing the plugin:

vagrant plugin install vagrant-digitalocean

Followed by the command to add the new box to Vagrant (remember what boxes are from the previous articles?):

vagrant box add digital_ocean https://github.com/smdahlen/vagrant-digitalocean/raw/master/box/digital_ocean.box

Configuring a Project to Run with this Provider

In order to use this plugin, you’ll need some form of letting Vagrant communicate with DigitalOcean. This is why the API is there, and why you now have to log into your DigitalOcean account and generate a new API key for authentication. Navigate to https://www.digitalocean.com/api_access and click Create to generate your new API key. You'll use this in the configuration together with your client ID seen above it.

Now that you have installed the plugin, added the DO specific box, and you have your API key ready. Let’s create a project that will use them. In the previous articles we’ve been working with a project to illustrate how Vagrant works with its default provider: VirtualBox. Let’s now create another project for DigitalOcean cloud server. Create a root directory for it (outside of the previous project folder) and navigate in it:

mkdir test_project2
cd test_project2

Next, run the initialization command:

vagrant init

This will create the required Vagrantfile.

Before we edit this file, let’s create the SSH keys needed for authentication with DigitalOcean. Run the following command to generate your SSH key pair:

ssh-keygen -t rsa

You can accept the defaults by pressing enter. This will place the SSH private and public keys to the path we will specify below in the Vagrantfile configuration. For more information about generating SSH key, check out this tutorial. Now let’s edit the Vagrantfile and configure it:

nano Vagrantfile

Find the following line:

config.vm.box = "base"

And replace it with:

config.vm.box = "digital_ocean"

Below it, add the following lines:

config.ssh.private_key_path = "~/.ssh/id_rsa"
config.vm.provider :digital_ocean do |provider|
    provider.client_id = "YOUR CLIENT ID"
    provider.api_key = "YOUR API KEY"
    provider.image = "Ubuntu 12.10 x64"
    provider.region = "New York 2"
  end

With the first line, you are specifying the path to the SSH private key file. This means that the provider will create a new DigitalOcean SSH key using your public key which is at that path with a .pub extension (the one you created earlier). The next two lines are for the DO API, so make sure you replace where needed with your client ID and the API key. With the image, you specify what type of cloud server you want deployed whereas with the region you choose the DigitalOcean data center you’d like the droplet to be created in. To see the available options, check the DO droplet creation page.

You can add some additional settings in the vm.provider block:

  • provider.size: A string representing the size to use when creating a new VPS (e.g. 1GB). Currently, it defaults to 512MB.
  • provider.ssh_key_name: A string representing the name to use when creating a DigitalOcean SSH key for VPS authentication. It defaults to Vagrant.

Save the file and exit. Now you can run the vagrant up command specifying the DigitalOcean provider to deploy your new VPS:

vagrant up --provider=digital_ocean

If you get the following error:

The secure connection to the DigitalOcean API has failed. Please
ensure that your local certificates directory is defined in the
provider config.

    config.vm.provider :digital_ocean do |vm|
      vm.ca_path = "/path/to/ssl/ca/cert.crt"
    end

This is generally caused by the OpenSSL configuration associated
with the Ruby install being unaware of the system specific ca
certs.

It means you are missing the root certificates necessary to communicate with DigitalOcean. To fix it, open the .bashrc file:

nano ~/.bashrc

And add the following line at the bottom:

export SSL_CERT_FILE=/etc/ssl/certs/ca-certificates.crt

Save the file and exit. Open again the Vagrantfile and add the following line below the one in which you specified the region (the order is not so important but it has to be part of the vm.provider block):

provider.ca_path = "/etc/ssl/certs/ca-certificates.crt"

Save the file and exit. Now try again to deploy the VPS:

vagrant up --provider=digital_ocean

Now a droplet should be created in your account, in the specified region and using the image you wanted. You can check it out in your DO account. You can use that basically as you would have used a VirtualBox guest machine.

Supported Commands

The provider supports the following Vagrant sub-commands:

  • vagrant destroy: Destroys the droplet instance.
  • vagrant ssh: Logs into the droplet instance using the configured user account.
  • vagrant halt: Powers off the droplet instance.
  • vagrant provision: Runs the configured provisioners and rsyncs any specified config.vm.synced_folder.
  • vagrant reload: Reboots the droplet instance.
  • vagrant rebuild: Destroys the droplet instance and recreates it with the same IP address is was assigned to previously.
  • vagrant status: Outputs the status (active, off, not created) for the droplet instance.

Many thanks to Shawn Dahlen for creating this plugin.

Vagrant—Article #1

Vagrant—Article #2

Vagrant—Article #3

Article Submitted by: Danny

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
10 Comments


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

The link : Using Vagrant on Your Own VPS Running Ubuntu is broken

Etel Sverdlov
DigitalOcean Employee
DigitalOcean Employee badge
August 2, 2013

Thanks-- I’ll fix that now!

Or use puphpet :) Been using it lately to setup new dev servers at work and it’s very handy

If anyone is looking for a decent Vagrant image to get this up and running with, you can check out my Vagrant dev machine at https://github.com/bacongobbler/bacongobbler-vagrant-vm. You should just need to add the docs that are listed in this post, along with installing the plugins listed in the README. Let me know if you run into issues with this. :)

Note that if you installed Vagrant on Windows to c:\Program Files\Vagrant the space in the folder name will kill the vagrant-digitalocean plugin install. Error looks like:

$ vagrant plugin install vagrant-digitalocean Installing the ‘vagrant-digitalocean’ plugin. This can take a few minutes… C:/Program Files/Vagrant/embedded/lib/ruby/site_ruby/1.9.1/rubygems/installer.rb:556:in `rescue in block in build_extensions’: ERROR: Failed to build gem native extension. (Gem::Installer::ExtensionBuildError) “C:/Program Files/Vagrant/embedded/bin/ruby.exe” extconf.rb creating Makefile

make generating generator-i386-mingw32.def /bin/sh: C:/Program: No such file or directory make: *** [generator-i386-mingw32.def] Error 127 … (stack trace)

(I’m not sure whether to hate filenames with spaces or programs that can’t handle spaces in filenames…)

On Mac OS I can’t find the cert.crt location… does anyone know where is it?

/etc/ssl/certs/ca-certificates.crt looks like for linux only…

Thanks

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
September 12, 2013

@nik.mda: <pre>You can generate the file you need by opening Keychain Access (from /Applications/Utilities), going to the System Roots keychain, selecting everything and then choosing Export Items… from the File menu. Make sure the File Format is set to Privacy Enhanced Mail (.pem), then save it to your Desktop as Certificates. Next, in Terminal enter</pre> http://mercurial.selenic.com/wiki/CACertificates

Once you do that, the ca-certificates.crt mac equivalent would be stored as ~/Desktop/Certificates.pem

I haven’t tried it as I do not have access to a machine running OSX but let me know if that works :]

Those instructions seems a bit more clear: https://github.com/smdahlen/vagrant-digitalocean#install

will follow and will let you know. Thanks Kamal!

So you do this brew install curl-ca-bundle then you set the path in Vagrantfile and ~/.bashrc to /usr/local/opt/curl-ca-bundle/share/ca-bundle.crt and it worked.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.