1

I am writing a bash script to install a number of packages. Do I need to call apt-get -y update each time before I call apt-get install <package_name>? Or is it enough to call apt-get -y update once at the beginning of the script?

If it needs to be called multiple times, could you explain why?

James Newton
  • 1,063

1 Answers1

4

You need to call apt-get -y update if:

  1. It has not been called for a long time (e.g. in the last 24h)
  2. If the list of repositories has changed since the last update

For the first point: obviously in a simple case it would be sufficient to call update just once at the beginning. In a more complicated script it may be useful to call an internal update procedure before each call to apt-get -y install and inside of this internal procedure you would automatically detect if the last update was run a long time ago and needs to be executed. See How to know last time `apt-get update` was executed?

For the second point: If you have a more complicated scenario and your script may possibly add repositories between the calls to install the packages, you may want to check the last modification dates of

  • /etc/apt/sources.list
  • all files in /etc/apt/sources.list.d/

in order to determine if new repositories were added after the last call of apt-get update, and if it needs to be called again in that case.

Zanna
  • 70,465
  • Some install processes require apt-get update to be called in order to terminate properly. For example, the last three steps for installing Phusion Passenger are: sudo sh -c 'echo deb https://oss-binaries.phusionpassenger.com/apt/passenger xenial main > /etc/apt/sources.list.d/passenger.list' sudo apt-get update and finally sudo apt-get install -y nginx-extras passenger. Without apt-get update, the package is not correctly installed. – James Newton Nov 24 '17 at 16:40