How to install Packer on Ubuntu 20.04/18.04/16.04?. Packer is an open source tool for creating identical machine images for multiple platforms from a single source configuration. It is a cross-platform command-line and lightweight application capable of generating and highly machine images for multiple platforms in parallel.

Check Packer use cases if you’re new to it and trying to figure out how you benefit from this tool.

What is Packer Machine Image?

A machine image is a single static unit that contains a pre-configured operating system and installed software which is used to quickly create new running machines. Machine image formats change for each platform. Some examples include AMIs for EC2, VMDK/VMX files for VMware, OVF exports for VirtualBox, etc.

How To Install Packer on Ubuntu 20.04/18.04/16.04?

Packer may be installed from a precompiled binary or from source. The easy and recommended method for all users is binary installation method. This is the one used in this article.

First, check the latest release of Packer on the Downloads page. Then download the recent version for your platform. But for us we’ll perform installation from APT repository:

Install basic dependencies:

sudo apt update
sudo apt -y install apt-transport-https ca-certificates curl software-properties-common

Add Hashicorp APT repository to your Ubuntu system:

curl -fsSL | sudo apt-key add -
sudo apt-add-repository "deb [arch=amd64] $(lsb_release -cs) main"

Finally update package index and install packer:

sudo apt update
sudo apt install packer

After installing Packer, verify the installation is working by checking that the packer is available:

$ packer
Usage: packer [--version] [--help]
Available commands are:
build build image(s) from template
fix fixes templates from old versions of packer
inspect see components of a template
validate check that a template is valid
version Prints the Packer version

To check software version run:

$ packer --version

If you placed the packer bi in a different directory, the PATH variable should contain this directory.

How To Use Packer

Builders are used to generate images and create machines for various platforms from templates. You can see a full list of suppported builders. A template is a configuration file used to define what image is built and its format is JSON.

In our example below, we will use VirtualBox Builder to create an Ubuntu Virtual Machine and export it in the OVA or OVF format.

Let’s create our working directory.

mkdir projects/packer/
cd projects/packer/

Create a file called build.json with below contents

    "variables": {
        "hostname": "ubuntu",
        "domain": "local",
        "ssh_user": "packer",
        "ssh_pass": "[email protected]"
    "builders": [{
        "type": "virtualbox-iso",
        "guest_os_type": "Ubuntu_64",
        "vm_name": "ubuntu-18.04-vbox-template",
        "iso_url": "",
        "iso_target_path": "/home/jmutai/iso/ubuntu-18.04.2-server-amd64.iso",
        "iso_checksum": "34416ff83179728d54583bf3f18d42d2",
        "iso_checksum_type": "md5",
        "memory": "1024",
        "cpus": "1",
        "disk_size": "20000",
        "format": "ova",
        "guest_additions_mode": "upload",
        "headless": "false",
        "http_directory": "http",
        "ssh_username": "{{user `ssh_user`}}",
        "ssh_password": "{{user `ssh_pass`}}",
        "ssh_wait_timeout": "20m",
        "shutdown_command": "sudo /sbin/halt -p",
        "boot_command": [
            " auto<wait>",
            " console-setup/ask_detect=false<wait>",
            " console-setup/layoutcode=us<wait>",
            " console-setup/modelcode=pc105<wait>",
            " debconf/frontend=noninteractive<wait>",
            " debian-installer=en_US<wait>",
            " fb=false<wait>",
            " initrd=/install/initrd.gz<wait>",
            " kbd-chooser/method=us<wait>",
            " keyboard-configuration/layout=USA<wait>",
            " keyboard-configuration/variant=USA<wait>",
            " locale=en_US<wait>",
            " netcfg/get_domain={{user `domain`}}<wait>",
            " netcfg/get_hostname={{user `hostname`}}<wait>",
            " grub-installer/bootdev=/dev/sda<wait>",
            " noapic<wait>",
            " preseed/url=http://{{ .HTTPIP }}:{{ .HTTPPort }}/preseed.cfg",
            " -- <wait>",

    "provisioners": [{
        "type": "shell",
        "inline": [
            "sleep 30",
            "sudo apt update",
            "sudo apt -y install bash-completion wget vim php php-fpm php-mysql"

Change the settings to fit ypur deployment design. We also need a preseed file to automate installation of Ubuntu 18.04 on VirtualBox with Packer.

Create a http directory as specified in JSON build file.

mkdir http

Then paste below to http/preseed.cfg file.

# Language and Locale
d-i debian-installer/language string en
d-i debian-installer/locale string en_US.UTF-8
d-i localechooser/preferred-locale string en_US.UTF-8
d-i localechooser/supported-locales en_US.UTF-8

# Hostname / domain
d-i netcfg/get_hostname string ubuntu
d-i netcfg/get_domain string local

# Keyboard
d-i console-setup/ask_detect boolean false
d-i keyboard-configuration/layout select USA
d-i keyboard-configuration/variant select USA
d-i keyboard-configuration/modelcode string pc105

# Timezone / Time
d-i time/zone string UTC
d-i clock-setup/utc-auto boolean true
d-i clock-setup/utc boolean true

# Server
tasksel tasksel/first multiselect standard, ubuntu-server

# No proxy
d-i mirror/http/proxy string

# Packages Policy
d-i pkgsel/install-language-support boolean false
d-i pkgsel/update-policy select none
d-i pkgsel/upgrade select full-upgrade
d-i pkgsel/include string openssh-server cryptsetup build-essential libssl-dev libreadline-dev zlib1g-dev

# Partitioning
d-i partman-auto/method string lvm
d-i partman-auto-lvm/guided_size string max
d-i partman-auto/choose_recipe select atomic
d-i partman-auto/disk string /dev/sda
d-i partman-lvm/confirm boolean true
d-i partman-lvm/confirm_nooverwrite boolean true
d-i partman-lvm/device_remove_lvm boolean true
d-i partman/confirm_nooverwrite boolean true
d-i partman/confirm boolean true
d-i partman/confirm_write_new_label boolean true
d-i partman/choose_partition select finish

# Create packer user account.
d-i passwd/user-fullname string packer
d-i passwd/username string packer
d-i passwd/user-password password [email protected]
d-i passwd/user-password-again password [email protected]
d-i user-setup/allow-password-weak boolean true
d-i user-setup/encrypt-home boolean false
d-i passwd/user-default-groups packer sudo

If using this example, you’ll need to change username and password for user to be created in the preseed file.

To build your image by running the following command.

$ packer build build.json

Since we had turned off installation in headless mode, you should be able to watch the installation on VirtualBox UI.

install packer ubuntu 18.04 01 1
install packer ubuntu 18.04 02 1
install packer ubuntu 18.04 03 1
install packer ubuntu 18.04 04 1

I’ll cover the usage of other providers such as Docker, AWS, CloudStack, QEMU e.t.c in the following guides. Subscribe to our newsletter to get instant updates.

Also check:

How to Create CentOS / Fedora / RHEL VM Templates on KVM

Provisioning of VMs on KVM with Terraform

Your support is our everlasting motivation,
that cup of coffee is what keeps us going!

As we continue to grow, we would wish to reach and impact more people who visit and take advantage of the guides we have on our blog. This is a big task for us and we are so far extremely grateful for the kind people who have shown amazing support for our work over the time we have been online.

Thank You for your support as we work to give you the best of guides and articles. Click below to buy us a coffee.


Please enter your comment!
Please enter your name here