Skip to content

Instantly share code, notes, and snippets.

@ChristopherA
Last active September 29, 2023 02:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChristopherA/f73473389a92736c746ceb690cdf41cf to your computer and use it in GitHub Desktop.
Save ChristopherA/f73473389a92736c746ceb690cdf41cf to your computer and use it in GitHub Desktop.
Erase & Install for macOS (also works with Catalina 10.15, Monterey 11, Big Sur 12, Ventura 13, Sonoma 14)
#!/bin/bash
## Erase & Install for macOS (works with Catalina 10.15, Monterey 11,
## Big Sur 12, Ventura 13, Sonoma 14)
#
# Bash script for erasing the current boot partition and starting a fresh
# install of the current macOS.
################
# WARNING! #
################
# This is a lethal script and should obviously only be used by power users and
# developers, since it wipes your boot volume!
## Copyright
#
# Unless otherwise noted the contents of this file are Copyright :copyright:2020 by
# Christopher Allen, and are shared under [spdx:MIT License](https://spdx.org/licenses/MIT.html)
# open-source license.
## Support My Open Source & Digital Civil Rights Advocacy Efforts
#
# If you like these tools, my writing, my advocacy, my point-of-view, I invite you to sponsor me.
#
# It's a way to plug into an advocacy network that's not focused on the "big guys". I work to
# represent smaller developers in a vendor-neutral, platform-neutral way, helping us all to
# work together.
#
# You can become a monthly patron on my [GitHub Sponsor Page](https://github.com/sponsors/ChristopherA)
# for as little as $5 a month; and your contributions will be multipled, as GitHub is matching
# the first $5,000!
#
# But please don’t think of this as a transaction. It’s an opportunity to advance the open web,
# digital civil liberties, and human rights together. You get to plug into my various projects,
# and hopefully will find a way to actively contribute to the digital commons yourself. Let’s collaborate!
#
# -- Christopher Allen <ChristopherA@LifeWithAlacrity.com\>
# Github: [@ChristopherA](https://github.com/ChristopherA)
# Twitter: [@ChristopherA](https://twitter.com/ChristopherA)
# SCRIPT DETAILS:
# This script will download the latest macOS installer, install it in a hidden
# partition, and then erase the boot partition (not just the boot volume
# but any APFS volumes sharing the same partition) and install a fresh copy
# macOS.
#
# Note that this technique does NOT erase other partitions on the same drive
# In my case, I have a separate partition named "/Volumes/Persistent/" that
# contains my scripts for restoring my settings and installing my apps and
# development environment.
#
# It can also be useful if you have a large internal hard drive to have a
# separate "/Volumes/Time Machine/" partition for quick restoration. What I
# do is exclude from Time Machine backup "/Applications/", my "~/Dropbox" and
# other cloud sync'ed folders, "/Volumes/Persistent/", and my development
# tools and projects, as they are seperately backed up and archived, which
# makes the data saved on the "/Volumes/Time Machine/" much smaller.
# My 4TB MacBook Pro has 1TB for boot, 1.5 TB for Time Machine, and 1.5 TB
# for my "/Volumes/Persistent/" which also has my dropbox and cloud data.
# Inspiration for this code from:
# https://grahamrpugh.com/2018/03/26/reinstall-macos-from-system-volume.html
# & https://grahamrpugh.com/2020/06/09/startosinstall-undocumented-options.html
# & https://www.jamf.com/blog/reinstall-a-clean-macos-with-one-button/
if [ ! -f "/Applications/Install macOS Ventura.app/Contents/Resources/startosinstall" ]; then
read -p "You don't currently have a macOS installer. Download it? [y/n]:" -n 1 -r
echo # (optional) move to a new line
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
exit 1
fi
printf "Downloading latest macOS installer…\n"
# --fetch-full-installer only works on Catalina 10.14+
sudo /usr/sbin/softwareupdate \
--fetch-full-installer \
--full-installer-version 13.0
# if you want a specific version
else
printf "Installer Ready!\n"
fi
read -p "Proceed with Erase Install? This will delete everything in the boot partition. Are you sure? [Y/n]:" -n 1 -r
echo # (optional) move to a new line
if [[ ! $REPLY =~ ^[Y]$ ]]
then
exit 1
fi
# /Applications/Install\ macOS\ Ventura.app/Contents/Resources/startosinstall --usage
sudo /Applications/Install\ macOS\ Ventura.app/Contents/Resources/startosinstall \
--eraseinstall --preservecontainer --agreetolicense --nointeraction \
--passprompt --forcequitapps --newvolumename 'Macintosh HD'
# # or you can choose to not erase and just upgrade to latest Big Sur.
# /Applications/Install\ macOS\ Ventura.app/Contents/Resources/startosinstall --agreetolicense --forcequitapps
@ChristopherA
Copy link
Author

This may require addition of --passprompt or --stdpassin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment