DEV Community

Cover image for How To Auto Update and Upgrade Homebrew
dev_neil_a
dev_neil_a

Posted on

How To Auto Update and Upgrade Homebrew

Introduction

Hi there. In this article, I'm going to cover how you can automate the updating of Homebrew and the upgrading of packages that were installed using brew.

The process is relatively simple to do, but there is a caveat with the upgrading of casks (macOS apps), which I'll cover as well.

YouTube Video

If you would prefer to see this article in video form, there is a YouTube video available below:

Warning

Before I get started, I just wanted to add a bit of a warning about doing this. As this will upgrade packages and casks to newer versions, it could cause some compatibility issues, depending upon what it is.

For example, Python may get upgraded and break compatibility with some libraries that you use that haven't been updated to work with the newer version.

If you are ok with this, then you are ok to continue but I just wanted to make you aware of that before proceeding any further.

Now, With that said, let's begin.

Installing autoupdate

First, open your terminal of choice. Any will work just fine.

Then, run brew update to update homebrew. This needs to be done before we go any further so that the latest version of Homebrew is installed and the package database is up-to-date.

With that done, the next step is to install an extension to homebrew called autoupdate. This is done by using the tap command in Homebrew. This allows you to add additional repositories to Homebrew. For this, we need the autoupdate extension. To install that, run:

brew tap domt4/autoupdate
Enter fullscreen mode Exit fullscreen mode

01

I'm recommending using the version from the domt4 repo as the one that is in the official homebrew repo has been taken out and moved back to the domt4 repo so it could be out of date.

Configuring autoupdate

Now that autoupdate is installed, let's check the status of it by running:

brew autoupdate status
Enter fullscreen mode Exit fullscreen mode

02

As you can see, it is not setup or running. To start it, run:

brew autoupdate start
Enter fullscreen mode Exit fullscreen mode

03

Once it has started, it will check for updates to homebrew every 24 hours from when it was started, or when the system booted up. It runs as a launchd service in the background and you’ll get a notification appear to let you know it is now running in the background.

04

Now, what it won't do by default, is upgrade any packages or casks that are installed. It will only do the equivalent of running brew update.

To get it to do an upgrade as well as update, some additional settings need to be set to the brew autoupdate start command and an additional package needs to be installed.

Before doing this, stop the autoupdate service by running:

brew autoupdate delete
Enter fullscreen mode Exit fullscreen mode

05

You can also use brew autoupdate stop but I prefer to delete the config, just to be sure.

Next, install the package called pinentry-mac by running:

brew install pinentry-mac
Enter fullscreen mode Exit fullscreen mode

06

This is required to handle any casks that require admin permissions to install an app into the Applications folder. If this wasn't installed, when brew does an upgrade, any casks that require admin rights will be ignored and not updated. With it installed, when a cask requires admin rights, a normal macOS prompt will appear asking you for a username and password to use that has admin rights to the Applications folder on your Mac.

This was the caveat I was alluding to earlier.

With that installed, let's reenable autoupdate with the additional settings by running:

brew autoupdate start, followed by 86400 --ac-only --cleanup --immediate --sudo --upgrade
Enter fullscreen mode Exit fullscreen mode

The order the settings are in does not matter, except for the time, which I believe must go first.

To go over what these settings or switches do:

  • 86400 sets autoupdate to run every 24 hours from when the Mac was last powered on or when the command was started. You can change this to a longer or shorter interval if you prefer. The time is in seconds.
  • --ac-only will only allow autoupdate to perform an update and upgrade when your Mac is plugged into mains power. If it's on battery or UPS battery power, it won't run.
  • --Cleanup is the same as running brew cleanup, which cleans up Homebrew's cache.
  • --Immediate will run autoupdate immediately and when the system starts up from being powered on or rebooted.
  • --Sudo will allow autoupdate to install casks that require admin rights to the Applications folder. This requires the pinentry-mac package that was previously installed to work.
  • --Upgrade will perform the equivalent of brew upgrade.

You don't have to use all of them if you don't want to. For example immediate and ac-only are not required but prefer to have them on.

07

Once the command is run, the notification will appear again and another one will also be shown to indicate that you may get alerts. You can do with that as you want.

08

And that's it for setting it up.

Now, when an update that runs in the background is completed, you will get a notification indicating that an update was run and some of the output from the logs.

09

Logs

Speaking of logs. If you want to know what was installed when autoupdate ran, you can examine the logs by running:

brew autoupdate logs
Enter fullscreen mode Exit fullscreen mode

10

For reference, the log files are stored in ~/Library/Logs/com.github.domt4.homebrew-autoupdate.

Changing Autoupdate Settings

If you need to change the settings for autoupdate, you will need to run brew autoupdate delete, which will delete the schedule, the settings that were previously set and the log files.

11

You can then rerun brew autoupdate start with the settings you want.

Uninstalling Autoupdate

The last thing I want to cover is removing autoupdate. To do this, make sure it isn't running and delete the running configuration by running:

brew autoupdate delete
Enter fullscreen mode Exit fullscreen mode

12

After that, run:

brew uninstall pinentry-mac
Enter fullscreen mode Exit fullscreen mode

13

This is optional in case you need it for something else.

Finally, to uninstall autoupdate, run:

brew untap domt4/autoupdate
Enter fullscreen mode Exit fullscreen mode

14

Optionally, you can run brew cleanup to clear out any cached Homebrew data.

If I try running brew autoupdate start again, I get the error indicating it's not a valid command.

15

Conclusion

And that is it for this article.

Thank you for reading and have a nice day!

Top comments (0)