# Trang 2.3: Cài đặt toàn cục (Global Install) và cấu hình biến môi trường PATH.

## 1. Cài đặt toàn cục là gì?

**Cài đặt toàn cục (Global Install)** có nghĩa là bộ công cụ `ag-kit` được cài đặt ở cấp độ **toàn bộ hệ thống** thay vì chỉ trong một dự án cụ thể. Sau khi cài toàn cục, bạn có thể gõ lệnh `ag-kit` từ bất kỳ thư mục nào trên máy tính.

<div class="my-4 rounded-lg overflow-hidden border border-gray-500/20 [&_thead_tr:first-child_th:first-child]:border-t-0 [&_thead_tr:first-child_th:first-child]:border-l-0 [&_thead_tr:first-child_th:last-child]:border-t-0 [&_thead_tr:first-child_th:last-child]:border-r-0 [&_tbody_tr:last-child_td:first-child]:border-b-0 [&_tbody_tr:last-child_td:first-child]:border-l-0 [&_tbody_tr:last-child_td:last-child]:border-b-0 [&_tbody_tr:last-child_td:last-child]:border-r-0 [&_thead_tr:first-child_th]:border-t-0 [&_tbody_tr:last-child_td]:border-b-0 [&_th:first-child]:border-l-0 [&_td:first-child]:border-l-0 [&_th:last-child]:border-r-0 [&_td:last-child]:border-r-0" id="bkmrk-%C2%A0-c%C3%A0i-%C4%91%E1%BA%B7t-nhanh-%28npx"><table class="min-w-full border-separate border-spacing-0"><thead><tr><th class="bg-gray-500/20 px-3 py-2 text-left font-medium leading-snug border border-gray-500/20"> </th><th class="bg-gray-500/20 px-3 py-2 text-left font-medium leading-snug border border-gray-500/20">Cài đặt nhanh (`npx`)</th><th class="bg-gray-500/20 px-3 py-2 text-left font-medium leading-snug border border-gray-500/20">Cài đặt toàn cục (`npm -g`)</th></tr></thead><tbody><tr><td class="px-3 py-2 leading-snug border border-gray-500/20">**Tốc độ lần đầu**</td><td class="px-3 py-2 leading-snug border border-gray-500/20">Chậm hơn (phải tải mỗi lần)</td><td class="px-3 py-2 leading-snug border border-gray-500/20">Nhanh hơn (đã có sẵn)</td></tr><tr><td class="px-3 py-2 leading-snug border border-gray-500/20">**Cần cài trước**</td><td class="px-3 py-2 leading-snug border border-gray-500/20">Không</td><td class="px-3 py-2 leading-snug border border-gray-500/20">Có</td></tr><tr><td class="px-3 py-2 leading-snug border border-gray-500/20">**Phù hợp khi**</td><td class="px-3 py-2 leading-snug border border-gray-500/20">Dùng thỉnh thoảng</td><td class="px-3 py-2 leading-snug border border-gray-500/20">Dùng thường xuyên, nhiều dự án</td></tr><tr><td class="px-3 py-2 leading-snug border border-gray-500/20">**Cập nhật**</td><td class="px-3 py-2 leading-snug border border-gray-500/20">Tự động lấy bản mới nhất</td><td class="px-3 py-2 leading-snug border border-gray-500/20">Cần chạy lại lệnh cài</td></tr></tbody></table>

</div>---

## 2. Cài đặt toàn cục lên hệ thống

### Bước 1: Mở Terminal với quyền phù hợp

- **Windows:** Mở **PowerShell** hoặc **Command Prompt** (không cần quyền Admin nếu npm được cài bình thường).
- **macOS/Linux:** Mở **Terminal** thông thường.

### Bước 2: Chạy lệnh cài đặt toàn cục

<div class="relative whitespace-pre-wrap word-break-all my-2 rounded-lg bg-list-hover-subtle border border-gray-500/20" id="bkmrk--1" node="[object Object]"><div class="min-h-7 relative box-border flex flex-row items-center justify-between rounded-t border-b border-gray-500/20 px-2 py-0.5">  
</div><div class="p-3"><div class="w-full h-full text-xs cursor-text"><div class="code-block"><div class="code-line" data-line-end="1" data-line-number="1" data-line-start="1"><div class="line-content"></div></div></div></div></div></div>```bash
bash
npm install -g @vudovn/ag-kit
```

`-g` là tham số báo cho npm biết đây là cài đặt toàn cục (global).

### Bước 3: Xác nhận cài đặt thành công

<div class="relative whitespace-pre-wrap word-break-all my-2 rounded-lg bg-list-hover-subtle border border-gray-500/20" id="bkmrk--2" node="[object Object]"><div class="min-h-7 relative box-border flex flex-row items-center justify-between rounded-t border-b border-gray-500/20 px-2 py-0.5">  
</div><div class="p-3"><div class="w-full h-full text-xs cursor-text"><div class="code-block"><div class="code-line" data-line-end="1" data-line-number="1" data-line-start="1"><div class="line-content"></div></div></div></div></div></div>```bash
bash
ag-kit --version
```

Nếu hiển thị số phiên bản (ví dụ: `2.0.2`), cài đặt đã thành công.

### Bước 4: Khởi tạo cho từng dự án

Mỗi khi cần dùng cho một dự án mới, chỉ cần:

<div class="relative whitespace-pre-wrap word-break-all my-2 rounded-lg bg-list-hover-subtle border border-gray-500/20" id="bkmrk--3" node="[object Object]"><div class="min-h-7 relative box-border flex flex-row items-center justify-between rounded-t border-b border-gray-500/20 px-2 py-0.5">  
</div><div class="p-3"><div class="w-full h-full text-xs cursor-text"><div class="code-block"><div class="code-line" data-line-end="2" data-line-number="2" data-line-start="2"><div class="line-content"></div></div></div></div></div></div>```bash
bash
cd đường/dẫn/dự-án
ag-kit init
```

---

## 3. Biến môi trường PATH là gì và tại sao quan trọng?

**PATH** là một biến môi trường của hệ thống. Nó chứa danh sách các thư mục mà hệ điều hành sẽ tự động tìm kiếm khi bạn gõ một lệnh trong Terminal.

**Ví dụ dễ hiểu:** Khi bạn gõ `ag-kit init`, hệ điều hành sẽ duyệt qua từng thư mục trong biến PATH để tìm xem có file thực thi tên `ag-kit` ở đâu không. Nếu tìm thấy → chạy. Nếu không → báo lỗi "command not found".

**Vì sao điều này quan trọng với Antigravity Kit?**

- Lệnh `ag-kit` cần có trong PATH để bạn gọi được từ mọi nơi.
- Lệnh `python` cần có trong PATH để các **script kiểm tra tự động** của Kit có thể chạy được.

---

## 4. Kiểm tra và cấu hình PATH

### Kiểm tra PATH hiện tại

**Windows (PowerShell):**

<div class="relative whitespace-pre-wrap word-break-all my-2 rounded-lg bg-list-hover-subtle border border-gray-500/20" id="bkmrk--6" node="[object Object]"><div class="min-h-7 relative box-border flex flex-row items-center justify-between rounded-t border-b border-gray-500/20 px-2 py-0.5">  
</div><div class="p-3"><div class="w-full h-full text-xs cursor-text"><div class="code-block"><div class="code-line" data-line-end="1" data-line-number="1" data-line-start="1"><div class="line-content"></div></div></div></div></div></div>```powershell
powershell
$env:PATH -split ";"
```

**macOS / Linux:**

<div class="relative whitespace-pre-wrap word-break-all my-2 rounded-lg bg-list-hover-subtle border border-gray-500/20" id="bkmrk--7" node="[object Object]"><div class="min-h-7 relative box-border flex flex-row items-center justify-between rounded-t border-b border-gray-500/20 px-2 py-0.5">  
</div><div class="p-3"><div class="w-full h-full text-xs cursor-text"><div class="code-block"><div class="code-line" data-line-end="1" data-line-number="1" data-line-start="1"><div class="line-content"></div></div></div></div></div></div>```bash
bash
echo $PATH
```

### Kiểm tra `ag-kit` có trong PATH chưa

<div class="relative whitespace-pre-wrap word-break-all my-2 rounded-lg bg-list-hover-subtle border border-gray-500/20" id="bkmrk--8" node="[object Object]"><div class="min-h-7 relative box-border flex flex-row items-center justify-between rounded-t border-b border-gray-500/20 px-2 py-0.5">  
</div><div class="p-3"><div class="w-full h-full text-xs cursor-text"><div class="code-block"><div class="code-line" data-line-end="3" data-line-number="3" data-line-start="3"><div class="line-content"></div></div></div></div></div></div>```bash
bash
# Tất cả hệ điều hành
where ag-kit # Windows
which ag-kit # macOS / Linux
```

Nếu lệnh trên trả về đường dẫn (ví dụ: `C:\Users\ten-ban\AppData\Roaming\npm\ag-kit`), bạn đã cấu hình đúng.

### Kiểm tra `python` có trong PATH chưa

<div class="relative whitespace-pre-wrap word-break-all my-2 rounded-lg bg-list-hover-subtle border border-gray-500/20" id="bkmrk--9" node="[object Object]"><div class="min-h-7 relative box-border flex flex-row items-center justify-between rounded-t border-b border-gray-500/20 px-2 py-0.5">  
</div><div class="p-3"><div class="w-full h-full text-xs cursor-text"><div class="code-block"><div class="code-line" data-line-end="2" data-line-number="2" data-line-start="2"><div class="line-content"></div></div></div></div></div></div>```bash
bash
where python # Windows
which python # macOS / Linux
```

---

## 5. Cấu hình PATH thủ công (khi gặp lỗi)

### Windows — Thêm thư mục npm global vào PATH

**B1:** Tìm thư mục npm global:

<div class="relative whitespace-pre-wrap word-break-all my-2 rounded-lg bg-list-hover-subtle border border-gray-500/20" id="bkmrk--11" node="[object Object]"><div class="min-h-7 relative box-border flex flex-row items-center justify-between rounded-t border-b border-gray-500/20 px-2 py-0.5">  
</div><div class="p-3"><div class="w-full h-full text-xs cursor-text"><div class="code-block"><div class="code-line" data-line-end="1" data-line-number="1" data-line-start="1"><div class="line-content"></div></div></div></div></div></div>```powershell
powershell
npm config get prefix
```

Kết quả thường là: `C:\Users\ten-ban\AppData\Roaming\npm`

**B2:** Thêm vào PATH:

1. Mở **Settings** → tìm kiếm "Environment Variables".
2. Nhấn "**Edit the system environment variables**".
3. Chọn biến **Path** trong phần "User variables" → nhấn **Edit**.
4. Nhấn **New** → dán đường dẫn từ Bước 1 vào.
5. Nhấn **OK** và **khởi động lại Terminal**.

**B3:** Xác nhận lại:

<div class="relative whitespace-pre-wrap word-break-all my-2 rounded-lg bg-list-hover-subtle border border-gray-500/20" id="bkmrk--12" node="[object Object]"><div class="min-h-7 relative box-border flex flex-row items-center justify-between rounded-t border-b border-gray-500/20 px-2 py-0.5">  
</div><div class="p-3"><div class="w-full h-full text-xs cursor-text"><div class="code-block"><div class="code-line" data-line-end="1" data-line-number="1" data-line-start="1"><div class="line-content"></div></div></div></div></div></div>```powershell
powershell
ag-kit --version
```

### macOS / Linux — Thêm vào `.bashrc` hoặc `.zshrc`

<div class="relative whitespace-pre-wrap word-break-all my-2 rounded-lg bg-list-hover-subtle border border-gray-500/20" id="bkmrk--13" node="[object Object]"><div class="min-h-7 relative box-border flex flex-row items-center justify-between rounded-t border-b border-gray-500/20 px-2 py-0.5">  
</div><div class="p-3"><div class="w-full h-full text-xs cursor-text"><div class="code-block"><div class="code-line" data-line-end="11" data-line-number="11" data-line-start="11"><div class="line-content"></div></div></div></div></div></div>```bash
bash
# Tìm thư mục npm global
npm config get prefix
# Thường là: /usr/local hoặc /home/ten-ban/.npm-global


# Thêm vào file cấu hình shell
echo 'export PATH="$(npm config get prefix)/bin:$PATH"' >> ~/.zshrc
# Hoặc cho bash:
echo 'export PATH="$(npm config get prefix)/bin:$PATH"' >> ~/.bashrc


# Áp dụng thay đổi
source ~/.zshrc # hoặc source ~/.bashrc
```

---

## 6. Quy trình làm việc điển hình với nhiều dự án

Đây là quy trình khuyến nghị khi bạn làm việc với nhiều dự án trên cùng một máy:

<div class="relative whitespace-pre-wrap word-break-all my-2 rounded-lg bg-list-hover-subtle border border-gray-500/20" id="bkmrk--15" node="[object Object]"><div class="min-h-7 relative box-border flex flex-row items-center justify-between rounded-t border-b border-gray-500/20 px-2 py-0.5"><div class="font-sans text-sm text-ide-text-color opacity-60">  
</div><div class="flex flex-row gap-2 justify-end">  
</div></div><div class="p-3"><div class="w-full h-full text-xs cursor-text"><div class="code-block"><div class="code-line" data-line-end="1" data-line-number="1" data-line-start="1">  
</div><div class="code-line" data-line-end="9" data-line-number="9" data-line-start="9"><div class="line-content"></div></div></div></div></div></div>```html
1. Cài đặt một lần:
npm install -g @vudovn/ag-kit


2. Cho mỗi dự án mới:
cd my-new-project
ag-kit init


3. Cập nhật định kỳ (không làm thường xuyên):
ag-kit update ← chạy trong thư mục dự án
```

---

## 7. Gỡ cài đặt toàn cục (khi cần)

Nếu bạn muốn gỡ bỏ cài đặt toàn cục:

<div class="relative whitespace-pre-wrap word-break-all my-2 rounded-lg bg-list-hover-subtle border border-gray-500/20" id="bkmrk--17" node="[object Object]"><div class="min-h-7 relative box-border flex flex-row items-center justify-between rounded-t border-b border-gray-500/20 px-2 py-0.5">  
</div><div class="p-3"><div class="w-full h-full text-xs cursor-text"><div class="code-block"><div class="code-line" data-line-end="1" data-line-number="1" data-line-start="1"><div class="line-content"></div></div></div></div></div></div>```html
bash
npm uninstall -g @vudovn/ag-kit
```

> **Lưu ý:** Gỡ cài đặt toàn cục không xóa bất kỳ thư mục `.agent/` nào đã được tạo trong các dự án. Để xóa khỏi một dự án cụ thể, hãy xóa thủ công thư mục `.agent/` đó.