This article covers a version of Ubuntu that is no longer supported. If you are currently operate a server running Ubuntu 12.04, we highly recommend upgrading or migrating to a supported version of Ubuntu:
Reason: Ubuntu 12.04 reached end of life (EOL) on April 28, 2017 and no longer receives security patches or updates. This guide is no longer maintained.
See Instead:
This guide might still be useful as a reference, but may not work on other Ubuntu releases. If available, we strongly recommend using a guide written for the version of Ubuntu you are using. You can use the search functionality at the top of the page to find a more recent version.
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.
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/
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.
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.
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.
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.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
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!
how can we clone a repo using packer?
There are missing commas in the example template provided.
See: https://www.digitalocean.com/community/questions/packer-io-wont-work-with-do
I need to reboot the droplet with live website, just so I could have a zip utility…? Is there some more elegant solution?
Hey Guys,
love your Blog! Wouldn’t it be easier to
source ~/.bashrc
instead of a reboot? Or is there an other reason to reboot the instance?Greetings, Thomas Supertramp