We hope you find this tutorial helpful. In addition to guides like this one, we provide simple cloud infrastructure for developers. Learn more →

How To Install and Get Started with Packer on an Ubuntu 12.04 VPS

PostedSeptember 6, 2013 37.4k views Configuration Management Ubuntu

About Packer

Packer is a nifty open source tool for creating machine images with pre-configured operating systems and installed software from a single source configuration. It runs on all the major operating systems and is capable of creating machine images on multiple platforms in parallel.

You can create images for the following platforms: Amazon EC2, DigitalOcean, OpenStack, VirtualBox and VMware. However, you can also extend Packer with plugins in order to support more platforms.

In this tutorial we will see how to install and get started with Packer on an Ubuntu 12.04 run VPS. In addition, it will illustrate how Packer works by creating a DigitalOcean Droplet image. So let’s get started. Therefore, I assume you already have sudo privileges to your own VPS through the terminal command line.

Installation

To install Packer, we need to get the right package for our system - Ubuntu (which is Linux based). Please note that Packer can also be installed on Mac OS X, Windows, FreeBSD and OpenBSD. So go to http://www.packer.io/downloads.html and copy the link to the 386 version. Then in your terminal, navigate to the root folder, create a new folder there called packer and navigate in it:

cd ~/
mkdir packer
cd packer

Then run the following command to download the package:

wget https://releases.hashicorp.com/packer/0.9.0/packer_0.9.0_linux_386.zip

Make sure you replace the URL with the one you found. This will download a .zip file that you will then have to unzip with the following command:

unzip packer_0.9.0_linux_386.zip

Again make sure you replace the name of the file with the one you downloaded. If you get an error and you don’t have unzip installed, run this command first to install it:

sudo apt-get install unzip

Then you can try unzipping the archive again and you should get a number of Packer related files. A next important step now is to make sure this new directory we created is on the Linux PATH (an environment variable). To do this, edit the .bashrc file found in the root directory:

nano ~/.bashrc

At the end of this file, paste the following line:

export PATH=$PATH:~/packer/

Then save the file, exit and reboot your VPS:

reboot

If you got disconnected, SSH back to your VPS and run the following command to make sure Packer is installed and all is in order:

packer

If you get an error "command not found", revisit the steps above and make sure you did everything properly. If you get some information about Packer available commands, you are good to go - Packer is now installed.

DigitalOcean Images

Now that we’ve installed Packer, let’s see how we can use use it to create DigitalOcean Droplet images (or snapshots if we use the DO terminology). To be able to do this, make sure you have a DigitalOcean account - as you’ll need the Client ID and an API key.

Once this is out of the way, we’ll create a configuration file that will define the image we want to build - called a template (a JSON file essentially). So go ahead and create a file called foo.json in the folder we installed Packer in (you can choose whatever file name you want though):

nano ~/packer/foo.json

Inside this file, paste the following:

{
  "builders": [{
    "type": "digitalocean",
    "api_token": "YOUR API TOKEN",
    "region": "nyc3",
    "size": "512mb",
    "image": "ubuntu-14-04-x64"
  }]
}

Make sure you add your API token where appropriate. What happens here? We’re basically defining a simple default builder of the DigitalOcean type (a 512 MB Droplet) in the NYC 3 region, running Ubuntu 14.04. You can consult this page for the rest of the DigitalOcean specific options you can include in this configuration file. But please note that since the default region Packer uses is no longer supported by DigitalOcean, it is mandatory to specify a region ID for deploying the VPS.

But before building it, let’s make sure this syntax is valid and we don’t have any errors. Save the file, switch into the packer folder, and run the following command:

cd packer
packer validate foo.json

If you don’t get an error, you are ready to create a DigitalOcean Droplet image with the following command:

packer build foo.json

After a little while (this shouldn’t be more than a 2-3 minutes), Packer will deploy a new Droplet based on the configuration file, will take a snapshot and then delete the Droplet. Now you are free to do what you want with your new image. Snapshots cost $0.05 per gigabyte per month, based on the amount of utilized space within the filesystem.

This hasn’t been such a big deal though. But Packer also does provisioning - which means you can have these images created with the software you want installed automatically. So let’s see how that works.

Provisioning

Packer allows you to install and configure software on the machine images. For this tutorial, we will have Apache installed automatically on the DO image created by Packer. So let’s see how that works.

Provisioners are configured in the same configuration file (template) we created earlier. So edit that file:

nano ~/packer/foo.json

And add another section to the JSON array in order to make your file look like this:

{
  "builders": [{
    "type": "digitalocean",
    "api_token": "YOUR API TOKEN",
    "region": "nyc3",
    "size": "512mb",
    "image": "ubuntu-14-04-x64"
  }],

  "provisioners": [{
    "type": "shell",
    "inline": [
      "sleep 30",
      "sudo apt-get update",
      "sudo apt-get install -y apache2"
    ]
  }]

}

Here we added a new shell provisioner (that comes by default with Packer and that will run scripts in the shell). When the new VPS is deployed, Packer will SSH into the new machine as soon as it’s available. The sleep 30 instruction allows the Ubuntu operating system to properly initialize before Packer connects through SSH to run the shell script, which will then update the repositories and install Apache.

Let’s validate the file again to make sure we made no syntax mistakes:

packer validate foo.json

Then we can go ahead with the build again:

packer build foo.json

Packer will now deploy the same DigitalOcean Droplet but will also install Apache on it. And then it will create an image that you’ll find in the Images section of your account and then destroy the Droplet. To make sure everything worked and Apache got installed, log in to your DigitalOcean account and deploy a new Droplet based on that image. Then point your browser to its IP and you should see Apache "It works!" message.

Conclusion

We’ve seen in this tutorial how to install and use Packer at its most basic level. You can read more about working with other platforms and how to deploy multiple machines on different platforms at the same time.

Article Submitted by: Danny

4 Comments

Creative Commons License