231

When I try to

apt-get install -y tzdata

the command line option for picking timezone shows up. I am trying to use this in a script to do some setup, how can I make the apt-get run without user input?

I know to reconfigure the tzdata I can do

echo "America/New_York" > /etc/timezone
dpkg-reconfigure -f noninteractive tzdata

But when installing I need it to run fully even if it doesn't set the right timezone, I can always reconfigure it.

I tried

echo 5 | apt-get install -y tzdata

but it is not working as expected.

1
  • stackoverflow.com/a/20693661/1174169, while marked as "off-topic" also answers this question, by creating a "pre-seed" text file with debconf settings, prior to running apt-get install. With this technique I was able to install tzdata non-interactively without any symlink workarounds.
    – cod3monk3y
    Commented Jan 26, 2022 at 23:53

8 Answers 8

319

This is the script I used

(Updated Version with input from @elquimista from the comments)

#!/bin/bash

ln -fs /usr/share/zoneinfo/America/New_York /etc/localtime
DEBIAN_FRONTEND=noninteractive apt-get install -y tzdata
dpkg-reconfigure --frontend noninteractive tzdata

Seems to work fine.

As one liner:

DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends tzdata
3
  • 76
    If you are fine with UTC, here's a one-liner: DEBIAN_FRONTEND=noninteractive apt-get install -y tzdata
    – elquimista
    Commented Sep 19, 2018 at 11:12
  • 18
    This is such a pain. I can't install tzdata without it prompting me to set my default timezone (configuration prompt during package installation? whose idea was this?), and I can't set the default timezone without first installing tzdata. It already defaults to UTC if you pass in EOF. Why not just do that and let user update later if they want. Or let me set TZ environment variable and have tzdata look for that. Commented Apr 16, 2019 at 23:16
  • 4
    Doing ln -fs /usr/share/zoneinfo/America/New_York /etc/localtime step before DEBIAN_FRONTEND=noninteractive apt-get install -y tzdata was the only way I could get this to install w/o a prompt.
    – fncomp
    Commented Jun 14, 2019 at 21:18
163

If someone wants to achieve it in Dockerfile, use as below.

RUN DEBIAN_FRONTEND=noninteractive apt-get -y install tzdata
7
  • 30
    This can be done as a single line: RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends ... The DEBIAN_FRONTEND variable must be set for the install part. It does not work if set before the && for the update part. Commented Aug 24, 2020 at 4:01
  • 1
    Issuing ARG DEBIAN_FRONTEND=noninteractive on a separate line worked for me sometimes, while on a single line as recommended by @DanielStevens worked for me always. Commented Feb 19, 2021 at 2:32
  • 1
    ARG is useful for when you have command-line arguments. You want ENV to set it unconditionally.
    – tripleee
    Commented Oct 28, 2021 at 18:49
  • 1
    Setting DEBIAN_FRONTEND=noninteractive with ENV should be actively discouraged. Use ARG instead. Commented Aug 12, 2022 at 20:01
  • @StephanSamuel Glad I'm not the only one. What's the reason behind ARG DEBIAN_FRONTEND=noninteractive only working sometimes? Commented Aug 12, 2022 at 20:06
24

To avoid playing directly with symlinks and to run configuration only once, I suggest to use debconf-set-selections command:

echo 'tzdata tzdata/Areas select Europe' | debconf-set-selections
echo 'tzdata tzdata/Zones/Europe select Paris' | debconf-set-selections
DEBIAN_FRONTEND="noninteractive" apt install -y tzdata
1
  • For aesthetic reasons, maybe use something like printf '%s\n' 'tzdata tzdata/Areas select Europe' 'tzdata tzdata/Zones/Europe select Paris' | debconf-set-selections in a single RUN command.
    – tripleee
    Commented May 21 at 9:29
13

I have recently found the following solution in a Dockerfile building the Cingulata FHE library:

ln -snf /usr/share/zoneinfo/$(curl https://ipapi.co/timezone) /etc/localtime

It basically uses the API provided by ipapi.co to retrieve the timezone information. This automatically configures the timezone properly instead of skipping the dialog and using the default (UTC).

1
  • 2
    Smart idea to use ipapi.co/timezone, provided that the timezone to set is same as where the machine is located. This may not work for e.g. with user in Asia, but server in US.
    – Henry Luo
    Commented May 19, 2021 at 6:30
10

All credit for this should go to @PYA but the right order should be:

ln -fs /usr/share/zoneinfo/America/New_York /etc/localtime
export DEBIAN_FRONTEND=noninteractive
apt-get install -y tzdata
dpkg-reconfigure --frontend noninteractive tzdata
0
3

Here is how I did it:

echo 1 > input.txt
echo 1 >> input.txt
apt-get install -y tzdata < input.txt
ln -fs /usr/share/zoneinfo/America/Los_Angeles /etc/localtime
echo America/Los_Angeles > /etc/timezone

The first two echo statements create a text file that contains the selection numbers for the geographic area menu and the city/region menu. This file is then used to provide input to the apt-get install command. The tzdata package will be installed without asking for any user input. The timezone will be set to Africa/Abidjan as if you entered 1 and 1 in response to the prompts you would normally get. Then I change the timezone to what I want with the last two commands.

Instead of 1 and 1, you could use the actual numbers for the geographic area and city/region that you want, but it seems to me that those numbers could change.

3

here is what worked for me:

from ubuntu:bionic
ARG DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get install -y tzdata

RUN unlink /etc/localtime
RUN ln -s /usr/share/zoneinfo/America/New_York /etc/localtime
1

After reading the comments, I did two steps below to use the TZ environment variable:

  • Added the following to the Dockerfile
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends tzdata && apt-get clean
  • Added the following to the docker CMD script:
    if [ ! -z "${TZ}" ]; then
        echo "${TZ}" > /etc/timezone
        dpkg-reconfigure -f noninteractive tzdata
    fi

This worked for me and allowed me to set the time zone when starting the container.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.