38

i'm currently using the Homebrew package manager and my question is: is possibile to write a bash's script in order to execute brew update and eventually brew upgrade whenever opening a shell for the first time? I'm using iTerm at the moment.

0

6 Answers 6

35

This was easily done with scripts and now the built-in autoupdate mechanism handles this elegantly. See Tate’s excellent answer for details


If the automatic update and upgrade aren’t working, here is another way for older versions.

For efficiency (and cool factor), I would use a tool like Lingon to launch this script periodically using launchctl/launchd instead of each time you start a shell. On my MacBook, it takes 3 seconds to update the second time (no work done, DNS cache set, etc...) and it take 10 second to run the first time (no work done) or 15+ seconds if a package needs to be downloaded or compiled.

Perhaps once a day or once an hour - running in the background would be sufficient given those times to execute?

You could make a simple script /usr/local/bin/brewup that calls brew in turn and logs the results to the system log. If you don’t like chasing logs in the centralized log store, “teeing” them to a text file works well, too.

#!/bin/bash

brew=/usr/local/bin/brew
logger=/usr/bin/logger

$brew update 2>&1  | $logger -t brewup.update
$brew upgrade 2>&1 | $logger -t brewup.upgrade
$brew cleanup 2>&1 | $logger -t brewup.cleanup

I just call the brewup when I'm about to go make tea or when I get started and let it run in the background.

brewup &
9
  • 2
    +1 I agree that it's better to use tome sort of launchclt instead of running at every start of Terminal. Additonally it's Terminal independent (for iTerm users). Commented Sep 19, 2015 at 18:48
  • 1
    This is brilliant, thank you! :) How about crontab instead of Lingon?
    – g_rmz
    Commented Sep 19, 2015 at 18:53
  • 3
    @g_rmz There's nothing wrong with crontab. . If it already works for you or you want to learn it, run with it. The benefits of launchd over cron is that it's more resilient, more power friendly, and handles sleep / missed intervals more naturally. It’s also more complicated, and fewer people have used it for decades.
    – bmike
    Commented Sep 19, 2015 at 18:59
  • 1
    Hey @dahved. See the man page for logger for the dry version of the t flag. I use it so I can find these messages in the sea of logs with an easy grep or log command and predicate/search term.
    – bmike
    Commented Mar 31, 2018 at 13:39
  • 2
    So you brewup when ever you're about to brew up? @bmike
    – yosefrow
    Commented Jun 25, 2019 at 6:57
47
+100

Homebrew has an autoupdate subcommand. So you can run:

brew autoupdate start

To automatically run brew update every 24 hours. The docs linked above outline a number of configuration options. Because you'd like to also run brew upgrade, you can run:

brew autoupdate start --upgrade

Under the hood brew autoupdate is using launchd.

05/2024 Update: As of March 2024, autoupdate is no longer an official homebrew tap, this means you'll need to run brew tap DomT4/homebrew-autoupdate for this functionality.

6
  • 2
    This is the way, I’m adding this info as a link to your post at the very top of my old answer. +1
    – bmike
    Commented Jul 7, 2021 at 17:39
  • Are you referring to this (which is an autoupdate command using launchd): github.com/Homebrew/homebrew-autoupdate? If so, maybe clarify that it's not the built in autoupdate? Since the developer of that subcommand claims that: "This command mostly exists to ensure Homebrew is updated regardless of whether you invoke brew or not, which is the primary difference from the autoupdate mechanism built into brew, the latter requiring a user to explicitly run any of brew install, brew tap or brew upgrade." Commented Jan 19, 2023 at 16:23
  • The autoupdate link in my answer points to what I'm referring to. autoupdate is listed under the Official External Commands. Commented Jan 19, 2023 at 18:05
  • 1
    I get a command not found with brew autoupdate, I'm running 4.2.19
    – Tim
    Commented Apr 23 at 21:17
  • I suggest clarity in your answer that it is a third party tool, not a built-in command. One has to install it first using brew tap homebrew/autoupdate (Or is it domt4/?). The documentation is in the tool's own GitHub repo. Also, Homebrew has removed this tool from the list of "Official External Commands" (which actually means they are third-party tools).
    – ADTC
    Commented May 30 at 6:37
18

There is a tool called homebrew-autoupdate which will do this for you. It can automatically run brew update in the background every 24 hours (configurable) to ensure that you always have fresh homebrew data when you go to install/upgrade packages.

To install it run brew tap domt4/autoupdate and brew autoupdate --start 43200 to configure it to autoupdate every 12 hours (43200 seconds).

EDIT: This is now integrated into Homebrew, see https://github.com/Homebrew/homebrew-autoupdate.

1
  • 2
    FYI this functionality has since been integrated into Homebrew, see Tate's answer below. Commented Jan 9, 2022 at 9:01
4

You might want to ask yourself: "Do I really want to auto-update applications, which the applications that I build myself depend upon?"

Brew team turned down this feature request to facilitate early updates.

My interpretation of their reasoning: "The risk of malicious code slipping through the opensource auditing process weighs more than the incidental need for a speedy update."

If this is your line of reasoning, you like to be in control of software updates, and you care a lot about homebrew packages being up-to-date, you are probably better off adding a line to the bottom of .bashrc (or the rc file of your favorite shell) asking:

brew outdated

This will confront you, in every new terminal, with the outdated packages. When your annoyance reaches the limit (you feel ready to update), you'll be happy to do the necessary.

2

I prefer to update Homebrew on startup. I have a script Update Homebrew.sh in ~/Library/Scripts:

#!/usr/local/bin/bash
    
brew update
brew upgrade
brew cleanup -s

Don't forget to make the script executable using chmod u+x.

The script is launched on startup using launchd. For that, I have Update Homebrew.plist in ~/Library/LaunchAgents:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
    <dict>
        <key>Label</key>
        <string>Update Homebrew</string>
        <key>ProgramArguments</key>
        <array>
            <string>/Users/[home]/Library/Scripts/Update Homebrew.sh</string>
        </array>
        <key>RunAtLoad</key>
        <true/>
    </dict>
</plist>

Where [home] is the name of your home folder. (You can't use the tilde in this file.)

1

You can use cron to simplify this.

Find your brew installation location with which brew. Mine was in the default location:

/usr/local/bin/brew

Using crontab -e add this line to the editor (update with your brew location):

0 9 * * * /usr/local/bin/brew update && /usr/local/bin/brew upgrade && /usr/local/bin/brew cleanup

The above will update brew, upgrade formulae and casks, and then clean up - every morning at 9am.

1
  • This works as long as none of the casks requires sudo.
    – nohillside
    Commented Mar 6, 2021 at 11:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .