terminal 104: installing tools on macOS with Homebrew terminal 104: ติดตั้งเครื่องมือบน macOS ด้วย Homebrew
install Homebrew, use `brew install` and `brew upgrade`, and understand why a newly installed command can still say `command not found` ติดตั้ง Homebrew, ใช้ `brew install` และ `brew upgrade`, แล้วเข้าใจว่าทำไมคำสั่งที่เพิ่งติดตั้งไปยังอาจขึ้นว่า `command not found` ได้
in terminal 103 you learned the workflow idea: search, pipe, redirect, keep only the useful part.
the next wall is simpler and more annoying:
“okay, but how do i install the command everyone keeps mentioning?”
on a Mac, that answer is usually Homebrew.
this lesson is Mac-only on purpose. the goal is not “learn every package manager.” the goal is to make one very common beginner moment feel predictable:
- you try
brew install ... - maybe
brewis missing - you install Homebrew
- the installer prints a shell-setup step
- you run that printed setup step instead of guessing
- the tool appears on disk
- and then
whichproves whether your shell can see it
once that sequence makes sense, “command not found” stops feeling random.
if you’re on Windows, don’t force this lesson to map 1:1 to your machine. the important mental model is still useful, but the install tool is different. you can skim this to understand the shell-setup/PATH problem, then continue with the series in terminal 105, where the PATH model gets explained directly.
what you’ll know by the end
- what Homebrew is and why Mac users reach for it
- how to install Homebrew with the official installer
- how to follow the installer’s post-install shell step instead of skipping it
- how to install a tool with
brew install - how to upgrade a tool with
brew upgrade - how to use
whichto check whether your shell can actually find a command - the footgun for this lesson: a tool can be installed on disk while your current shell still can’t see it
what this assumes
terminal 101, terminal 102, and terminal 103.
if pwd, ls, cat, pipes, and redirects feel okay now, you’re ready.
5 commands + 1 mental model
this lesson focuses on modern macOS with zsh.
the simulator uses the Apple Silicon Homebrew path, which is:
/opt/homebrew/binif you’re on an Intel Mac, you’ll usually see /usr/local/bin instead. same idea, different folder.
| what you want to do | command | what it means |
|---|---|---|
| install Homebrew | /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" | run the official installer |
| run the printed shell setup | echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zprofile then eval "$(/opt/homebrew/bin/brew shellenv)" | save Homebrew’s PATH setup and load it now |
check whether brew is visible | which brew | ask the shell where brew resolves |
| install a tool | brew install ripgrep | download and link the rg command |
check whether rg is visible | which rg | confirm the shell can find rg |
| upgrade a tool later | brew upgrade ripgrep | update that installed formula |
the demo uses ~/.zprofile because that is a common zsh login startup file on macOS. if your installer prints a different file, use the one it prints.
the mental model underneath those commands is the real lesson:
- install puts files somewhere on disk
- your shell only runs commands it can find through
PATH - the installer may put the right files on disk before your shell has loaded the PATH setup
Installed is not the same as visible
if PATH still feels fuzzy here, that’s fine — terminal 105 explains it directly.
try it yourself
the interactive demo below is a compressed version of the real beginner experience. click through this order:
brew install ripgrepfirst- watch it fail because
brewdoes not exist yet - run the Homebrew installer
- notice that the installer prints the shell-setup commands
- try
which brew - notice the teaching moment: Homebrew now exists on disk, but PATH is not set yet
- run the printed shell setup
- run
which brewagain - run
brew install ripgrep - run
which rg - run
rg deploy ~/projects/launch-plan - open a new shell and run
which brewone more time - run
brew upgrade ripgrep
the key moment is between steps 5 and 8:
- install success does not guarantee that the current shell sees the new command immediately
- on modern macOS, the installer prints the shell setup you need; follow those printed commands instead of guessing a startup file
- when a command still says “not found”, check whether it’s missing from disk or just missing from your current
PATH
ใน terminal 103 เราเริ่มใช้ terminal แบบเป็น workflow แล้วครับ ค้นหา ต่อคำสั่งกัน ส่ง output ไปต่อ และเก็บไว้เฉพาะส่วนที่ต้องใช้
กำแพงถัดมาจะง่ายกว่า แต่ชวนหงุดหงิดกว่า:
“โอเค แล้วจะติดตั้งคำสั่งที่คนอื่นชอบพูดถึงกันยังไง?”
บน Mac คำตอบนั้นมักจะเป็น Homebrew
บทนี้ตั้งใจทำให้เป็นบทสำหรับ Mac โดยเฉพาะครับ เป้าหมายไม่ใช่ “ต้องไปรู้จัก package manager ทุกตัว” แต่คือทำให้ช่วงเวลาที่มือใหม่เจอบ่อยมาก กลายเป็นเรื่องที่อธิบายได้:
- ลองพิมพ์
brew install ... - แล้วพบว่า
brewยังไม่มี - ติดตั้ง Homebrew
- installer จะพิมพ์ขั้นตอนตั้งค่า shell ต่อให้ด้วย
- เรารันขั้นตอนที่มันพิมพ์ให้ แทนการเดาเอง
- ไฟล์ของเครื่องมือไปอยู่บนดิสก์แล้ว
- แล้วใช้
whichพิสูจน์ว่า shell มองเห็นจริงไหม
พอเข้าใจลำดับนี้ คำว่า “command not found” จะไม่ดูสุ่มอีกต่อไป
ถ้าคุณใช้ Windows ไม่ต้องพยายามฝืนให้บทนี้ตรงกับเครื่องตัวเองแบบเป๊ะๆ ครับ ภาพจำหลักยังใช้ได้อยู่ แต่เครื่องมือติดตั้งจะเป็นคนละตัว อ่านเพื่อเข้าใจปัญหา shell setup/PATH ได้ แล้วค่อยไปต่อใน terminal 105 ที่จะอธิบาย PATH ตรงๆ ครับ
จบโพสต์นี้แล้วจะทำอะไรได้บ้าง
- เข้าใจว่า Homebrew คืออะไร และทำไมคนใช้ Mac ถึงหยิบมันมาใช้บ่อย
- ติดตั้ง Homebrew ด้วย installer ทางการ
- ทำตามขั้นตอน post-install สำหรับ shell ที่ installer พิมพ์ให้ แทนการเดาเอง
- ติดตั้งเครื่องมือด้วย
brew install - อัปเกรดเครื่องมือด้วย
brew upgrade - ใช้
whichเพื่อตรวจว่า shell มองเห็นคำสั่งนั้นจริงไหม - จำจุดพลาดสำคัญของบทนี้ได้: ไฟล์อาจติดตั้งลงดิสก์แล้ว แต่ shell ที่เปิดอยู่ยังมองไม่เห็น
โพสต์นี้เริ่มจากไหน
terminal 101, terminal 102, และ terminal 103 ครับ
ถ้า pwd, ls, cat, การต่อคำสั่งด้วย pipe และการเขียนผลลัพธ์ลงไฟล์เริ่มคุ้นแล้ว ก็ต่อบทนี้ได้เลย
5 คำสั่ง + 1 ภาพจำที่ต้องมี
บทนี้โฟกัสที่ macOS รุ่นปัจจุบันที่ใช้ zsh เป็นหลักครับ
ตัวเดโมจะใช้ path ของ Homebrew บน Apple Silicon:
/opt/homebrew/binถ้าคุณใช้ Intel Mac ส่วนใหญ่จะเห็นเป็น /usr/local/bin แทน
แนวคิดเหมือนกัน ต่างกันแค่โฟลเดอร์
| สิ่งที่อยากทำ | คำสั่ง | ความหมาย |
|---|---|---|
| ติดตั้ง Homebrew | /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" | รัน installer ทางการ |
| รัน shell setup ที่ installer พิมพ์ให้ | echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zprofile แล้ว eval "$(/opt/homebrew/bin/brew shellenv)" | บันทึก PATH ของ Homebrew และโหลดให้ shell ตอนนี้ |
เช็กว่า brew มองเห็นแล้วไหม | which brew | ถาม shell ว่า brew ชี้ไปที่ไหน |
| ติดตั้งเครื่องมือ | brew install ripgrep | ดาวน์โหลดและลิงก์คำสั่ง rg |
เช็กว่า rg มองเห็นแล้วไหม | which rg | ยืนยันว่า shell หา rg เจอแล้ว |
| อัปเกรดเครื่องมือภายหลัง | brew upgrade ripgrep | อัปเดต formula ที่ติดตั้งไว้ |
ตัวเดโมใช้ ~/.zprofile เพราะเป็น startup file ของ zsh ที่เจอบ่อยบน macOS ถ้า installer ของคุณพิมพ์ชื่อไฟล์อื่น ให้ใช้ไฟล์ที่มันพิมพ์ให้ครับ
แต่ภาพจำที่สำคัญกว่าตัวคำสั่ง คือ 3 ข้อนี้ครับ:
- การติดตั้งคือการเอาไฟล์ไปไว้บนดิสก์
- shell จะรันได้เฉพาะคำสั่งที่มันหาเจอผ่าน
PATH - installer อาจวางไฟล์ถูกแล้ว แต่ shell ยังไม่ได้โหลด PATH ชุดใหม่
ติดตั้งแล้ว ไม่ได้แปลว่า shell มองเห็นแล้ว
ถ้าคำว่า PATH ยังฟังดูเบลอๆ ไม่ต้องกังวลครับ terminal 105 จะอธิบายตรงๆ ในบทถัดไป
ลองเล่นดู
เดโมด้านล่างย่อประสบการณ์จริงของมือใหม่ให้อยู่ในพื้นที่ปลอดภัย กดตามลำดับนี้ได้เลย:
- กด
brew install ripgrepก่อน - ดูว่ามันพังเพราะยังไม่มี
brew - รัน Homebrew installer
- สังเกตว่า installer จะพิมพ์คำสั่งตั้งค่า shell ต่อด้วย
- กด
which brew - สังเกตจุดสำคัญ: Homebrew มีอยู่บนดิสก์แล้ว แต่ PATH ยังไม่ถูกตั้ง
- รัน shell setup ที่ installer พิมพ์ให้
- รัน
which brewอีกรอบ - รัน
brew install ripgrep - รัน
which rg - รัน
rg deploy ~/projects/launch-plan - เปิด shell ใหม่ แล้วรัน
which brewอีกครั้ง - รัน
brew upgrade ripgrep
หัวใจของบทนี้อยู่ระหว่างข้อ 5 ถึง 8:
- ติดตั้งสำเร็จ ไม่ได้แปลว่า shell ปัจจุบันจะเห็นคำสั่งใหม่ทันที
- บน macOS รุ่นปัจจุบัน installer จะพิมพ์คำสั่งตั้งค่า shell ที่ต้องใช้ ดังนั้นให้ทำตามข้อความที่มันพิมพ์ให้ แทนการเดาไฟล์เอง
- ถ้ายังขึ้นว่า “not found” ให้เช็กก่อนว่ามัน “ไม่มีอยู่จริง” หรือแค่ “shell นี้ยังมองไม่เห็นผ่าน
PATH”
Homebrew simulator
This compresses the real setup into one safe demo: fail once, install Homebrew, run the printed shell setup, then verify brew and rg with which.
what a package manager is, without the jargon
a package manager is just a tool that installs other tools in a consistent way.
instead of:
- downloading random zip files
- dragging binaries around by hand
- forgetting where you put them
you get one command:
brew install ripgrepHomebrew handles the fetch, the files, and the symlink that makes the command usable.
you do not need deeper internals than that yet. for a beginner, “one trusted install command instead of ad hoc downloads” is the important part.
why “installed” and “usable right now” are not the same thing
this is the exact confusion 104 is here to fix.
after Homebrew installs, two different things can both be true:
breworrgexists on disk- your current shell still cannot resolve it
that does not mean the install failed.
it usually means the Homebrew files exist, but the shell setup has not been loaded into this shell yet. the practical beginner move is simple:
- run
which breworwhich rg - if the installer printed shell-setup commands, run those exact commands
- open a new shell session if you want to prove the startup file loads next time
- try again before reinstalling everything
the post after this one explains why that works in more detail. for now, the reliable habit is enough.
why which is worth learning immediately
which is tiny, but it saves a lot of guessing.
which brew
which rgif it prints a path, your shell knows where the command lives.
if it prints nothing useful, you have a visibility problem, not a typing problem.
that one distinction prevents a lot of beginner thrashing.
one real-world task
on your real Mac, this is the practical flow:
- install Homebrew with the official installer
- read the post-install message and run the shell commands it prints
- open a fresh Terminal tab or window if you want to verify the setting persists
- run
which brew - run
brew install ripgrep - run
which rg - go to a small project folder
- run
rg TODO
that gives you a real payoff immediately:
- you install one useful tool
- you verify that it resolves
- you use it for a real text-search task instead of installing it “just in case”
common mistakes
you keep reinstalling instead of checking resolution. if brew or rg says “not found”, run which brew or which rg before you install again.
you assume the install failed because the shell cannot see brew yet. this is the big one. the files can be correct while the PATH setup has not been loaded.
you skip the installer’s shell step. current Homebrew docs say the installer prints the shell-config directions you need. on modern macOS, that usually means adding Homebrew’s shellenv setup to a startup file and loading it into the current shell.
you think the exact folder is the lesson. it isn’t. the simulator uses /opt/homebrew/bin because that’s the common modern Mac path. on Intel Macs, the path is usually /usr/local/bin. the idea is still “the shell searches folders in order.”
official references
what this unlocks
you can now install a tool on a Mac, verify that your shell can see it, and separate “the install is wrong” from “the shell setup has not loaded yet.”
that’s a serious jump in confidence, because a lot of terminal frustration lives exactly there.
next up: terminal 105 — PATH and environment variables. that’s where this stops being a troubleshooting trick and becomes a real mental model.
package manager แบบไม่ต้องลงศัพท์เยอะ
package manager ก็คือเครื่องมือที่เอาไว้ติดตั้ง “เครื่องมืออื่น” อย่างเป็นระบบครับ
แทนที่จะต้อง:
- โหลด zip จากหลายที่
- ลากไฟล์ไปวางเอง
- แล้วลืมว่าเก็บไว้ตรงไหน
คุณมีคำสั่งเดียว:
brew install ripgrepHomebrew จะจัดการเรื่องดาวน์โหลด ไฟล์ และลิงก์ที่ทำให้เรียกคำสั่งนั้นใช้งานได้
ตอนนี้ยังไม่ต้องรู้ลึกกว่านั้นก็พอครับ สำหรับมือใหม่ แค่เข้าใจว่า “ใช้คำสั่งที่เชื่อถือได้คำสั่งเดียว แทนการติดตั้งแบบจับแพะชนแกะ” ก็เพียงพอแล้ว
ทำไม “ติดตั้งแล้ว” กับ “ใช้ได้ตอนนี้” ถึงไม่ใช่เรื่องเดียวกัน
นี่คือความสับสนที่ 104 ตั้งใจมาแก้ตรงๆ เลยครับ
หลังติดตั้ง Homebrew เสร็จ จะมี 2 เรื่องที่อาจจริงพร้อมกันได้:
brewหรือrgอยู่บนดิสก์แล้ว- แต่ shell ที่เปิดอยู่ยังหาไม่เจอ
นั่น ไม่ได้แปลว่า ติดตั้งพังเสมอไปครับ
ส่วนใหญ่แปลว่าไฟล์ของ Homebrew มีอยู่แล้ว แต่ shell setup ยังไม่ได้ถูกโหลดเข้ามาใน shell นี้ วิธีคิดที่ใช้ได้จริงสำหรับมือใหม่คือ:
- รัน
which brewหรือwhich rg - ถ้า installer พิมพ์คำสั่งตั้งค่า shell มา ให้รันตามนั้นก่อน
- เปิด shell ใหม่ถ้าอยากพิสูจน์ว่า startup file โหลดค่าเดิมกลับมาได้
- แล้วลองใหม่อีกครั้งก่อนจะติดตั้งซ้ำ
บทถัดไปจะอธิบายให้ชัดขึ้นว่า ทำไม วิธีนี้ถึงได้ผล แต่สำหรับตอนนี้ แค่นิสัยนี้ก็ช่วยได้เยอะมากแล้วครับ
ทำไมควรรู้จัก which ตั้งแต่ตอนนี้
which เป็นคำสั่งเล็กมาก แต่ช่วยลดการเดาสุ่มได้เยอะครับ
which brew
which rgถ้ามันพิมพ์ path ออกมา แปลว่า shell รู้ว่าคำสั่งนั้นอยู่ที่ไหน
ถ้าไม่ขึ้นอะไรที่ใช้การได้ ปัญหาคือ “มองไม่เห็นคำสั่ง” ไม่ใช่ “พิมพ์ผิด”
แค่แยกสองกรณีนี้ออก ก็ช่วยลดการงงวนไปมาได้เยอะแล้ว
โจทย์จากโลกจริง
บน Mac ของจริง ลำดับที่ใช้ได้จริงจะประมาณนี้:
- ติดตั้ง Homebrew ด้วย installer ทางการ
- อ่านข้อความ post-install แล้วรันคำสั่งตั้งค่า shell ที่มันพิมพ์ให้
- เปิดแท็บหรือหน้าต่าง Terminal ใหม่ถ้าอยากเช็กว่าค่านี้อยู่ถาวร
- รัน
which brew - รัน
brew install ripgrep - รัน
which rg - เข้าไปที่โฟลเดอร์โปรเจ็กต์เล็กๆ สักอัน
- รัน
rg TODO
ผลตอบแทนของ flow นี้มาเร็วมากครับ:
- ติดตั้งเครื่องมือจริง 1 ตัว
- เช็กว่าระบบมองเห็นแล้ว
- แล้วเอาไปใช้กับงานค้นหาข้อความจริงทันที ไม่ใช่ติดไว้เฉยๆ
ข้อผิดพลาดที่พบบ่อย
ติดตั้งซ้ำไปเรื่อยๆ แทนที่จะเช็กก่อนว่า shell หาเจอไหม ถ้ายังขึ้นว่า brew หรือ rg “not found” ให้ลอง which brew หรือ which rg ก่อนจะติดตั้งใหม่
คิดว่าติดตั้งพัง ทั้งที่จริง shell ยังมองไม่เห็น brew อันนี้คือประเด็นหลักของบทนี้เลยครับ ไฟล์อาจถูกต้องหมดแล้ว แต่ PATH setup ยังไม่ได้ถูกโหลด
ข้ามขั้นตอนตั้งค่า shell ที่ installer พิมพ์ให้ เอกสารปัจจุบันของ Homebrew บอกว่า installer จะพิมพ์วิธีตั้งค่า shell ที่ต้องใช้ให้ และบน macOS รุ่นปัจจุบัน มักเป็นการเพิ่ม setup ของ Homebrew shellenv ลง startup file แล้วโหลดเข้ามาใน shell ปัจจุบัน
คิดว่าโฟลเดอร์เป๊ะๆ คือประเด็นสำคัญที่สุด ไม่ใช่ครับ ตัวเดโมใช้ /opt/homebrew/bin เพราะเป็น path ที่เจอบ่อยบน Mac รุ่นใหม่ ส่วน Intel Mac มักเป็น /usr/local/bin แต่แนวคิดเหมือนเดิม: shell จะค้นหาโฟลเดอร์ตามลำดับ
อ้างอิงทางการ
เปิดประตูไปต่อที่ไหน
ตอนนี้คุณติดตั้งเครื่องมือบน Mac ได้แล้ว เช็กได้แล้วว่า shell มองเห็นหรือยัง และแยกออกได้ว่า “ติดตั้งผิด” กับ “shell setup ยังไม่โหลด” เป็นคนละปัญหากัน
นี่เป็นก้าวเรื่องความมั่นใจที่ใหญ่มาก เพราะความหงุดหงิดของการใช้ terminal จำนวนมากมันอยู่ตรงนี้พอดี
ตอนต่อไปคือ terminal 105 — PATH และ environment variables ตรงนั้นสิ่งที่บทนี้ยังเป็นแค่เคล็ดแก้ปัญหา จะกลายเป็นภาพจำที่อธิบายได้ทั้งระบบมากขึ้นครับ