Install and use Packer on Ubuntu 22.04/20.04/18.04/16.04

Posted on 293 views

How to install Packer on Ubuntu 22.04/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.

Install Packer on Ubuntu 22.04/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

Import GPG key used in package signing:

curl -fsSL | sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/hashicorp.gpg

Add Hashicorp APT repository to your Ubuntu system:

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
    console         creates a console for testing variable interpolation
    fix             fixes templates from old versions of packer
    fmt             Rewrites HCL2 config files to canonical format
    hcl2_upgrade    transform a JSON template into an HCL2 configuration
    init            Install missing plugins or upgrade plugins
    inspect         see components of a template
    plugins         Interact with Packer plugins and catalog
    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.

Using Packer on Ubuntu 22.04/20.04/18.04/16.04

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

        "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": "/tmp/ubuntu-18.04.6-server-amd64.iso",
        "iso_checksum": "f5cbb8104348f0097a8e513b10173a07dbc6684595e331cb06f93f385d0aecf6",
        "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",
            " console-setup/ask_detect=false",
            " console-setup/layoutcode=us",
            " console-setup/modelcode=pc105",
            " debconf/frontend=noninteractive",
            " debian-installer=en_US",
            " fb=false",
            " initrd=/install/initrd.gz",
            " kbd-chooser/method=us",
            " keyboard-configuration/layout=USA",
            " keyboard-configuration/variant=USA",
            " locale=en_US",
            " netcfg/get_domain=user `domain`",
            " netcfg/get_hostname=user `hostname`",
            " grub-installer/bootdev=/dev/sda",
            " noapic",
            " preseed/url=http:// .HTTPIP : .HTTPPort /preseed.cfg",
            " -- ",

    "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.





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.

Gravatar Image
A systems engineer with excellent skills in systems administration, cloud computing, systems deployment, virtualization, containers, and a certified ethical hacker.