1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

MacOSのセットアップを自動化しよう!

1
Last updated at Posted at 2026-07-13

はじめに

新しいMacのセットアップは骨の折れる作業です。アプリのダウンロード、システム設定の変更、ターミナル環境の構築などを行っていると、あっという間に1日が終わってしまいます。

私はこれまで時間をかけて、一連のシェルスクリプトを使用した自動のセットアップ手順を練り上げてきました。これらのスクリプトを順番に実行するだけで、まっさらな macOS を、完全にカスタマイズされた自分専用の開発マシンへと短時間で変身させることができます。
本記事では、macOS をゼロからセットアップするための設計図をステップバイステップで解説します。

この記事で紹介しているスクリプトはすべて、私の GitHub リポジトリで公開しています:


フェーズ1:最初の手動ステップ

スクリプトを実行する前に、箱から出してすぐにいくつかの手動ステップを行う必要があります。

  1. iCloud へのサインイン: 基本的なデータと設定を同期します。
  2. ブラウザとクラウドストレージのインストール: 私は通常、すぐに(Safari 経由で)Google ChromeGoogle Drive をインストールします。これでアカウント認証の準備をします。(注:完全に iCloud Drive, Safari に依存する方がシステムとの統合性やシンプルさの観点から良いかもしれませんが、これは個人の好みです)。
  3. メールのセットアップ: Gmailや普段使っているメールプロバイダにログインします。

これらの基本作業が終わったら、ターミナルを開いて、面倒な作業をスクリプトに任せましょう。


フェーズ2:自動化スクリプトの実行

依存関係が正しい順番でインストールされるように、セットアッププロセスを6つの番号付きスクリプトに分割しています。

(注:この記事に沿って進める場合、dotfilesの場所など、スクリプト内のファイルパスをご自身のディレクトリ構造に合わせて変更する必要があります)。

ステップ1:Xcodeコマンドラインツールのインストール

開発ツールをインストールする前に、macOS には Xcode コマンドラインツールが必要です。これにより、git が使用できるようになります。

# 1_xcode.sh
# install to use git
xcode-select --install

ポップアップが表示されます。「インストール」をクリックし、完了するのを待ってからステップ2に進んでください。

ステップ2:Homebrewのインストール

Homebrew は macOS に欠かせないパッケージマネージャーです。これを使えば、CLI ツールや GUI アプリケーションをターミナルから直接インストールできます。

#!/bin/bash
# 2_homebrew.sh

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

exec ${SHELL} -l # Reload the shell

ステップ3:アプリとパッケージのインストール

Homebrew の準備ができたら、すべてのアプリケーションを一括でインストールします。私のスクリプトは以下の3つのカテゴリを処理します:

  1. CLI ツール(brew install): bat(進化したcat)、fzf(あいまい検索)、neovimnodeuv(Python ツールチェーン)、そしてシンタックスハイライトや自動補完のための各種 Zsh プラグインなどのモダンなツール。個々のツールの詳細については、こちらの記事 を参照してください。
  2. GUI アプリ(brew install --cask): ブラウザ、IDE、ユーティリティツール。Visual Studio CodeRectangle(ウィンドウ管理)、Shottr(スクリーンショット)などなど。
  3. Mac App Storeアプリ(mas): mas CLI を使用すると、App Store のアプリをアプリ ID 経由でインストールできます。たとえば、mas install 302584613 で Kindle がインストールされます。
  4. おまけ: このスクリプトは GitHub CLI(gh)の認証も処理し、リポジトリをターミナルから直接削除するショートカットなどのカスタムエイリアスも設定します。

Homebrew による CLI ツールと GUI アプリのインストール:

#!/bin/bash
# 3_brew_install.sh

set -e
set -u


# CLI
brew install bat
brew install cmatrix
brew install coreutils
brew install duf
brew install duti
brew install fd
brew install fzf
brew install gcc
brew install libomp
brew install gh
brew install git-delta
brew install git-filter-repo
brew install ghostscript
brew install mas
brew install mole
brew install neovim
brew install node
brew install pnpm
brew install rename
brew install scc
brew install sqlite
brew install tree
brew install uv
brew install yt-dlp
brew install zsh-autosuggestions
brew install zsh-completions
brew install zsh-git-prompt
brew install zsh-syntax-highlighting

# C++
#sudo ln -s /opt/homebrew/bin/gcc-14 /usr/local/bin/gcc
#sudo ln -s /opt/homebrew/bin/g++-14 /usr/local/bin/g++

# GUI
brew install --cask nikitabobko/tap/aerospace
brew install --cask anki
brew install --cask appcleaner
brew install --cask betterdisplay
brew install --cask coconutbattery
brew install --cask coteditor
brew install --cask clipy
brew install --cask cryptomator
brew install --cask drawio
brew install --cask espanso
brew install --cask handy
brew install --cask iina
brew install --cask jupyter-notebook-viewer
brew install --cask google-chrome
brew install --cask google-drive
brew install --cask keyboardcleantool
brew install --cask keycastr
brew install --cask mathpix-snipping-tool
brew install --cask MonitorControl
brew install --cask qlmarkdown
brew install --cask rectangle
brew install --cask shottr
brew install --cask stats
brew install --cask stay
brew install --cask syntax-highlight
brew install --cask visual-studio-code
brew install --cask timac/vpnstatus/vpnstatus
brew install --cask xbar
brew install --cask zoom
brew install --cask zotero

App Store からの mas CLI を使ったインストール:

# LINE
mas install 539883307
# hand-mirror
mas install 1502839586
# kindle
mas install 302584613

GitHub CLI のエイリアスの登録:

----------------------------------------------------
gh alias set repo-delete 'api -X DELETE "repos/$1"'
gh auth refresh -h github.com -s delete_repo

## check alias
# gh alias list
## usage (WARNING: no confirmation!)
# gh repo-delete user/myrepo

# Authenticate Git
gh auth login -s delete_repo
----------------------------------------------------

シェルの再読み込み:

exec ${SHELL} -l

もし、Homebrew によるセットアップをさらに自動化したいのであれば、Brewfile への移行を検討してください。Brewfile は、数十行の brew install コマンドをシェルスクリプトにハードコードする代わりに、以下のコマンドで簡単に現在の環境設定をエクスポート(バックアップ)できます:

brew bundle dump

そして、新しい Mac をセットアップする際には、以下のコマンドでその Brewfile を読み込んで一括でインストールできます:

brew bundle --file=Brewfile

ステップ4:Dotfilesのシンボリックリンク作成

設定ファイル(.gitconfig.zshenv など)の管理は、一元化されたGitリポジトリに保存し、ホームディレクトリにシンボリックリンクを張るようにすると非常に簡単になります。

このスクリプトは dotfiles リポジトリ内をループ処理し、home/ ディレクトリ内のすべてに対してシンボリックリンク(ln -sf)を作成します。また、.zshenv ファイルは別の場所に保存しているため、個別に処理しています。

#!/bin/bash
# 4_lns.sh

set -e
set -u


# dotfiles
cd ~/Desktop/github/dotfiles/home/
for i in .[a-z]*
do
    ln -sf ~/Desktop/github/dotfiles/home/"$i" ~/"$i"
done

# .zshenv
ln -sf ~/Desktop/github/dotfiles/zsh/.zshenv ~/.zshenv

ステップ5:macOSのシステム環境設定

macOS では、数多くのシステム設定を defaults コマンドから変更できます。システム設定の各項目を手作業で変更する代わりに、以下のシェルスクリプトを実行することで、複数の設定をまとめて適用できます。

#!/bin/bash
# 5_mac.sh

set -e
set -u


# 参考資料:
# - https://github.com/rootbeersoup/dotfiles
# - https://github.com/skwp/dotfiles
# - https://github.com/sobolevn/dotfiles
# - https://github.com/webpro/dotfiles
# - https://apple.stackexchange.com/questions/14001/how-to-turn-off-all-animations-on-os-x
# - https://macos-defaults.com/


echo 'Macを設定しています。しばらくお待ちください。'
osascript -e 'tell application "System Preferences" to quit'


## 一般設定

# 起動音を消音する
sudo nvram SystemAudioVolume=" "
sudo nvram StartupMute=%01

# ダウンロードしたアプリケーションを開く際の隔離警告を無効にする
defaults write com.apple.LaunchServices LSQuarantine -bool false

# 通知センターを無効にする
launchctl unload -w /System/Library/LaunchAgents/com.apple.notificationcenterui.plist


## キーボード

# Caps Lockが有効になるまでの遅延をなくす
hidutil property --set '{"CapsLockDelayOverride":0}'

# キーリピート開始までの待ち時間とキーリピート速度を設定する
defaults write -g InitialKeyRepeat -int 15
defaults write -g KeyRepeat -int 2

# 英文入力時の自動大文字化を無効にする
defaults write -g NSAutomaticCapitalizationEnabled -bool false

# スペースキーを2回押したときのピリオド自動入力を無効にする
defaults write -g NSAutomaticPeriodSubstitutionEnabled -bool false

# スマートダッシュとスマート引用符への自動変換を無効にする
defaults write -g NSAutomaticDashSubstitutionEnabled -bool false
defaults write -g NSAutomaticQuoteSubstitutionEnabled -bool false

# ダブルクォートとシングルクォートに直線型の引用符を使用する
defaults write -g KB_DoubleQuoteOption -string '"abc"'
defaults write -g KB_SingleQuoteOption -string "'abc'"

# 日本語入力で数字を半角文字として入力する
# true: 全角数字、false: 半角数字
defaults write com.apple.inputmethod.Kotoeri JIMPrefFullWidthNumeralCharactersKey -bool false

# 日本語入力時のShiftキーの動作を設定する
# 0: カタカナ、1: ローマ字
defaults write com.apple.inputmethod.Kotoeri JIMPrefShiftKeyActionKey -int 0


## トラックパッド

# トラックパッドのポインタ移動速度を設定する
defaults write -g com.apple.trackpad.scaling -float 3

# タップによるクリックを有効にする
defaults write com.apple.AppleMultitouchTrackpad Clicking -bool true

# 1段階目と2段階目の強めのクリックに必要な押し込みの強さを設定する
# 0: 弱い、1: 中程度、2: 強い
defaults write com.apple.AppleMultitouchTrackpad FirstClickThreshold -int 0
defaults write com.apple.AppleMultitouchTrackpad SecondClickThreshold -int 0


## メニューバー

# メニューバーのステータスアイコン間の余白を狭くする
defaults -currentHost write -g NSStatusItemSpacing -int 6


## Dock

# Dockからすべてのアプリケーションアイコンを削除する
defaults delete com.apple.dock persistent-apps

# Dock右側からすべてのフォルダ、ファイル、ショートカットを削除する
defaults delete com.apple.dock persistent-others

# 起動中のアプリケーションアイコンの下に表示されるインジケータを非表示にする
defaults write com.apple.dock show-process-indicators -bool false

# 最近使用したアプリケーションをDockに表示しない
defaults write com.apple.dock show-recents -bool false

# 自動的に隠したDockが表示されるまでの遅延をなくす
defaults write com.apple.dock autohide-delay -float 0

# ウインドウを最小化するときにスケールエフェクトを使用する
defaults write com.apple.dock mineffect -string scale

# 最小化したウインドウをアプリケーションアイコンに格納する
defaults write com.apple.dock minimize-to-application -bool true

# Dockのアプリケーションアイコンのサイズを設定する
defaults write com.apple.dock tilesize -int 43

# 指定したアプリケーションをDockに追加する
apps=(
	"/System/Applications/Utilities/Terminal"
	"/Applications/CotEditor"
	"/Applications/Visual Studio Code"
	"/Applications/Google Chrome"
	"/Applications/Zotero"
	"/System/Applications/Mail"
)

for app in "${apps[@]}"
do
	defaults write com.apple.dock persistent-apps -array-add \
		"<dict><key>tile-data</key><dict><key>file-data</key><dict><key>_CFURLString</key><string>$app.app</string><key>_CFURLStringType</key><integer>0</integer></dict></dict></dict>"
done


## Finder

# 名前順で並べ替えるときにフォルダを上部に表示する
defaults write com.apple.finder _FXSortFoldersFirst -bool true

# Finderウインドウの下部にパスバーを表示する
defaults write com.apple.finder ShowPathbar -bool true

# Finderウインドウの下部にあるステータスバーを非表示にする
defaults write com.apple.finder ShowStatusBar -bool false

# Finderで隠しファイルを常に表示する
# 表示を切り替えるキーボードショートカット: Command + Shift + .
defaults write com.apple.finder AppleShowAllFiles -bool true

# ファイル名の拡張子を常に表示する
defaults write NSGlobalDomain AppleShowAllExtensions -bool true

# ファイル名の拡張子を変更するときの警告を無効にする
defaults write com.apple.finder FXEnableExtensionChangeWarning -bool false

# Finderのデフォルト表示形式をカラム表示にする
# Flwv: Cover Flow表示
# Nlsv: リスト表示
# clmv: カラム表示
# icnv: アイコン表示
defaults write com.apple.finder FXPreferredViewStyle -string clmv

# Finderのカラム表示で列幅を自動調整する
defaults write com.apple.finder _FXEnableColumnAutoSizing -bool true

# ネットワークボリュームとUSBボリュームに.DS_Storeファイルを作成しない
defaults write com.apple.desktopservices DSDontWriteNetworkStores -bool true
defaults write com.apple.desktopservices DSDontWriteUSBStores -bool true

# 必要に応じて、プレーンテキストファイルのデフォルトアプリをCotEditorに設定する
# defaults write com.apple.LaunchServices LSHandlers -array-add \
#     '{LSHandlerContentType=public.plain-text;LSHandlerRoleAll=com.coteditor.CotEditor;}'

# ファイルをドラッグしたままフォルダに重ねたとき、フォルダを即座に開く
defaults write NSGlobalDomain com.apple.springing.delay -float 0

# ユーザーのLibraryフォルダを表示する
chflags nohidden ~/Library

# 必要に応じて、指定した標準フォルダを非表示にし、アクセスを制限する
# chflags hidden ~/{Documents,Movies,Music}
# chmod 000 ~/{Documents,Movies,Music}


## その他の設定

# Launchpadを表示・非表示にするときのアニメーションを無効にする
defaults write com.apple.dock springboard-show-duration -float 0
defaults write com.apple.dock springboard-hide-duration -float 0

# スクロールバーをクリックした位置へ直接移動する
# false: 次のページへ移動する
# true: クリックした位置へ移動する
defaults write -g AppleScrollerPagingBehavior -bool true


## スクリーンショット

# スクリーンショットのファイル名の接頭辞を設定する
defaults write com.apple.screencapture name screenshot

# スクリーンショット撮影後に表示されるフローティングサムネイルを非表示にする
defaults write com.apple.screencapture show-thumbnail -bool false

# スクリーンショットのファイル名に日時を含めない
defaults write com.apple.screencapture include-date -bool false

# ウインドウのスクリーンショットに影を含めない
defaults write com.apple.screencapture disable-shadow -bool true

# スクリーンショットにマウスポインタを含める
defaults write com.apple.screencapture showsCursor -bool true

# スクリーンショットをデスクトップに保存する
defaults write com.apple.screencapture location ~/Desktop/

# スクリーンショットをPNG形式で保存する
# macOSのバージョンによっては、GIF、JPEG、PDF、BMP、TIFF、
# PSD、JPEG 2000などの形式も使用できる
defaults write com.apple.screencapture type png

# 現在保存されているスクリーンショット関連の設定を表示する
# defaults read com.apple.screencapture


## 設定を反映するためのアプリケーション再起動

echo '設定の影響を受けるアプリケーションを再起動しています...'
killall Finder
killall Dock

echo '完了しました。'

ステップ6:デフォルトのファイル関連付けを設定する

デフォルトでは、Mac は .txt.md などのファイルをテキストエディット(TextEdit)で開きます。正直、このアプリはかなり使い勝手が悪いです。duti というCLIツールを使うことで、このスクリプトは一般的なテキストやプログラミング関連の拡張子を強制的に Visual Studio Code (com.microsoft.VSCode) に紐付けます。これで、いちいち :btn[右クリック -> このアプリケーションで開く] をする必要がなくなります!

#!/bin/bash
# 6_extension.sh

EDITOR_ID="com.microsoft.VSCode"

# Basic text types
duti -s $EDITOR_ID public.plain-text all
duti -s $EDITOR_ID public.text all
duti -s $EDITOR_ID public.data all
duti -s $EDITOR_ID public.source-code all

# Common file extensions
extensions=("txt" "md" "tex" "js" "jsx" "ts" "tsx" "py" "go" "rs" "c" "cpp" "h" "hpp" "css" "json" "yaml" "yml" "xml" "sh" "zsh" "bash")

for ext in "${extensions[@]}"; do
    duti -s $EDITOR_ID $ext all
done

echo "Default editor associations updated!"

VSCode 自体の環境設定は、Github と連携していれば同期の設定をオンにしておけば十分そうです。


フェーズ3:その他

コアシステムのインストールが完了したら、環境を完璧にするための2つの最後の調整を行います。

1. sudo で Touch ID を有効にする

sudo コマンドを実行するたびにパスワードを入力するのはすぐにうんざりしてきます。PAM(Pluggable Authentication Modules)の設定を変更することで、ターミナルで Mac の指紋認証リーダーを使用できるようになります。

必要な設定を追記する以下のスクリプトを実行します:

#!/bin/sh
# enable_TouchID_for_sudo.sh

cd /etc/pam.d

sudo tee -a sudo_local >/dev/null <<'EOF'
# sudo_local: local config file which survives system update and is included for sudo
# uncomment following line to enable Touch ID for sudo
auth       sufficient     pam_tid.so
EOF

2. 最後のシステム手動設定

システム設定で手動での介入が必要な項目がいくつか残っています:

  • キーボード設定で、日本語の入力ソースを設定します。
  • プログラミングに不可欠なバックスラッシュ(\)を入力できるように、¥(円)キーの割り当てなどを変更します。

トラブルシューティング : Zshのcompinitエラー

(Homebrew 経由でインストールした)Zshの補完機能を使用している場合、新しいターミナルウィンドウを開いたときに以下のエラーが表示されることがあります:

zsh compinit: insecure directories, run compaudit for list.
Ignore insecure directories and continue [y] or abort compinit [n]?

これは、Homebrew がフォルダの権限を変更することがあり、Zsh がそれらを「安全でない(insecure)」と判断するために起こります。

解決策:

  1. ターミナルで compaudit を実行して、問題のあるディレクトリ(通常は /opt/homebrew/share のようなパス)をリストアップします。
  2. chmod を使用して、それらのディレクトリの権限を修正します。例:
    chmod 755 /opt/homebrew/share
    

おわりに

マシンの設定をコードとして扱うことで、新しいデバイスをセットアップする際の時間と労力を大幅に削減できます。
もし今日私の Mac が壊れたとしても、自分のエイリアスやウィンドウマネージャーの設定、開発ツールが完全に揃った状態を30分で復元できます。

もしまだ Mac のセットアップを自動化していないのであれば、dotfiles 用の Git リポジトリを作成し、必須ツールやアプリのインストールを処理するスクリプトを書くことを強くお勧めします。
未来の自分がきっと感謝するはずです。(以前 MacbookPro が突然壊れて、新しく買い替えせざるを得なかった時の私のように!)


以下は、ブログページです。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?