关于 environment-setup-guide
Environment Setup Guide 是面向开发者的环境配置技能,指导识别需求、安装工具、配置设置并验证环境是否可用,也涵盖常见问题排查。
主要功能
- 01识别项目先决条件
- 02检查工具和版本
- 03提供平台对应的安装步骤
- 04配置变量和设置
- 05验证环境功能
- 06排查常见错误
适用场景
- 01准备新项目环境
- 02帮助团队成员入门
- 03在另一台机器重建环境
- 04记录或排查配置步骤
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ davila7/ claude-code-templates --skill environment-setup-guide安装后在 Claude Code 中输入/environment-setup-guide确认已加载。
详细介绍
环境搭建指南
概述
帮助开发者从零开始搭建完整的开发环境。本技能提供分步指导,涵盖安装工具、配置依赖、设置环境变量以及验证环境是否正确可用。
何时使用本技能
- 在启动新项目并需要搭建开发环境时使用
- 在让新团队成员加入项目时使用
- 在更换新机器或操作系统时使用
- 在排查与环境相关的问题时使用
- 在为项目编写搭建说明文档时使用
- 在创建开发环境文档时使用
工作原理
第 1 步:确定需求
我会帮你确定需要安装哪些内容:
- 编程语言及版本(Node.js、Python、Go 等)
- 包管理器(npm、pip、cargo 等)
- 数据库系统(PostgreSQL、MongoDB、Redis 等)
- 开发工具(Git、Docker、IDE 扩展等)
- 环境变量与配置文件
第 2 步:检查当前环境
在安装任何内容之前,我会帮你检查已经安装了哪些内容:
# Check versions of installed tools
node --version
python --version
git --version
docker --version
第 3 步:提供安装说明
我会给出针对不同平台的安装命令:
- macOS: 使用 Homebrew
- Linux: 使用 apt、yum 或包管理器
- Windows: 使用 Chocolatey、Scoop 或直接安装程序
第 4 步:配置环境
帮助设置:
- 环境变量(.env 文件)
- 配置文件(.gitconfig、.npmrc 等)
- IDE 设置(VS Code、IntelliJ 等)
- Shell 配置(.bashrc、.zshrc 等)
第 5 步:验证安装
提供验证步骤,确保一切正常工作:
- 运行版本检查
- 测试基本命令
- 验证数据库连接
- 检查环境变量是否已加载
示例
示例 1:Node.js 项目搭建
## Setting Up Node.js Development Environment
### Prerequisites
- macOS, Linux, or Windows
- Terminal/Command Prompt access
- Internet connection
### Step 1: Install Node.js
**macOS (using Homebrew):**
\`\`\`bash
# Install Homebrew if not installed
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# Install Node.js
brew install node
\`\`\`
**Linux (Ubuntu/Debian):**
\`\`\`bash
# Update package list
sudo apt update
# Install Node.js and npm
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt install -y nodejs
\`\`\`
**Windows (using Chocolatey):**
\`\`\`powershell
# Install Chocolatey if not installed
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
# Install Node.js
choco install nodejs
\`\`\`
### Step 2: Verify Installation
\`\`\`bash
node --version # Should show v20.x.x or higher
npm --version # Should show 10.x.x or higher
\`\`\`
### Step 3: Install Project Dependencies
\`\`\`bash
# Clone the repository
git clone https://github.com/your-repo/project.git
cd project
# Install dependencies
npm install
\`\`\`
### Step 4: Set Up Environment Variables
Create a \`.env\` file:
\`\`\`bash
# Copy example environment file
cp .env.example .env
# Edit with your values
nano .env
\`\`\`
Example \`.env\` content:
\`\`\`
NODE_ENV=development
PORT=3000
DATABASE_URL=postgresql://localhost:5432/mydb
API_KEY=your-api-key-here
\`\`\`
### Step 5: Run the Project
\`\`\`bash
# Start development server
npm run dev
# Should see: Server running on http://localhost:3000
\`\`\`
### Troubleshooting
**Problem:** "node: command not found"
**Solution:** Restart your terminal or run \`source ~/.bashrc\` (Linux) or \`source ~/.zshrc\` (macOS)
**Problem:** "Permission denied" errors
**Solution:** Don't use sudo with npm. Fix permissions:
\`\`\`bash
mkdir ~/.npm-global
npm config set prefix '~/.npm-global'
echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.bashrc
source ~/.bashrc
\`\`\`
示例 2:Python 项目搭建
## Setting Up Python Development Environment
### Step 1: Install Python
**macOS:**
\`\`\`bash
brew install [email protected]
\`\`\`
**Linux:**
\`\`\`bash
sudo apt update
sudo apt install python3.11 python3.11-venv python3-pip
\`\`\`
**Windows:**
\`\`\`powershell
choco install python --version=3.11
\`\`\`
### Step 2: Verify Installation
\`\`\`bash
python3 --version # Should show Python 3.11.x
pip3 --version # Should show pip 23.x.x
\`\`\`
### Step 3: Create Virtual Environment
\`\`\`bash
# Navigate to project directory
cd my-project
# Create virtual environment
python3 -m venv venv
# Activate virtual environment
# macOS/Linux:
source venv/bin/activate
# Windows:
venv\Scripts\activate
\`\`\`
### Step 4: Install Dependencies
\`\`\`bash
# Install from requirements.txt
pip install -r requirements.txt
# Or install packages individually
pip install flask sqlalchemy python-dotenv
\`\`\`
### Step 5: Set Up Environment Variables
Create \`.env\` file:
\`\`\`
FLASK_APP=app.py
FLASK_ENV=development
DATABASE_URL=sqlite:///app.db
SECRET_KEY=your-secret-key-here
\`\`\`
### Step 6: Run the Application
\`\`\`bash
# Run Flask app
flask run
# Should see: Running on http://127.0.0.1:5000
\`\`\`
示例 3:Docker 开发环境
## Setting Up Docker Development Environment
### Step 1: Install Docker
**macOS:**
\`\`\`bash
brew install --cask docker
# Or download Docker Desktop from docker.com
\`\`\`
**Linux:**
\`\`\`bash
# Install Docker
curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh
# Add user to docker group
sudo usermod -aG docker $USER
newgrp docker
\`\`\`
**Windows:**
Download Docker Desktop from docker.com
### Step 2: Verify Installation
\`\`\`bash
docker --version # Should show Docker version 24.x.x
docker-compose --version # Should show Docker Compose version 2.x.x
\`\`\`
### Step 3: Create docker-compose.yml
\`\`\`yaml
version: '3.8'
services:
app:
build: .
ports:
- "3000:3000"
environment:
- NODE_ENV=development
- DATABASE_URL=postgresql://postgres:password@db:5432/mydb
volumes:
- .:/app
- /app/node_modules
depends_on:
- db
db:
image: postgres:15
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=password
- POSTGRES_DB=mydb
ports:
- "5432:5432"
volumes:
- postgres_data:/var/lib/postgresql/data
volumes:
postgres_data:
\`\`\`
### Step 4: Start Services
\`\`\`bash
# Build and start containers
docker-compose up -d
# View logs
docker-compose logs -f
# Stop services
docker-compose down
\`\`\`
### Step 5: Verify Services
\`\`\`bash
# Check running containers
docker ps
# Test database connection
docker-compose exec db psql -U postgres -d mydb
\`\`\`
最佳实践
✅ 应该这样做
- 记录一切 - 编写清晰的搭建说明
- 使用版本管理器 - Node 用 nvm,Python 用 pyenv
- 创建 .env.example - 展示必需的环境变量
- 在干净系统上测试 - 验证说明能否从零开始顺利执行
- 包含故障排查 - 记录常见问题及解决方案
- 使用 Docker - 让不同机器上的环境保持一致
- 固定版本 - 在包文件中指定确切版本
- 自动化搭建 - 尽可能创建安装脚本
- 检查前置条件 - 开始前列出所需工具
- 提供验证步骤 - 帮助用户确认搭建成功
❌ 不要这样做
- 不要假定工具已安装 - 始终检查并提供安装说明
- 不要跳过环境变量 - 记录所有必需变量
- 不要用 sudo 运行 npm - 应修复权限问题
- 不要忽略平台差异 - 提供针对不同操作系统的说明
- 不要省略验证 - 始终包含测试步骤
- 不要使用全局安装 - 优先使用本地/虚拟环境
- 不要忽视错误 - 记录如何处理常见错误
- 不要跳过数据库设置 - 包含数据库初始化步骤
常见陷阱
问题:安装后出现 "Command not found"
症状: 已安装工具,但终端无法识别它 解决方案:
- 重启终端或重新加载 shell 配置
- 检查 PATH 环境变量
- 验证安装位置
# Check PATH
echo $PATH
# Add to PATH (example)
export PATH="/usr/local/bin:$PATH"
问题:npm/pip 权限错误
症状: 出现 "EACCES" 或 "Permission denied" 错误 解决方案:
- 不要使用 sudo
- 修复 npm 权限或改用 nvm
- 为 Python 使用虚拟环境
# Fix npm permissions
mkdir ~/.npm-global
npm config set prefix '~/.npm-global'
echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.bashrc
问题:端口已被占用
症状: 出现 "Port 3000 is already in use" 解决方案:
- 找到并结束占用该端口的进程
- 换用其他端口
# Find process on port 3000
lsof -i :3000
# Kill process
kill -9 <PID>
# Or use different port
PORT=3001 npm start
问题:数据库连接失败
症状: "Connection refused" 或 "Authentication failed" 解决方案:
- 确认数据库正在运行
- 检查连接字符串
- 验证凭据
# Check if PostgreSQL is running
sudo systemctl status postgresql
# Test connection
psql -h localhost -U postgres -d mydb
安装脚本模板
创建一个 setup.sh 脚本来自动化安装:
#!/bin/bash
echo "🚀 Setting up development environment..."
# Check prerequisites
command -v node >/dev/null 2>&1 || { echo "❌ Node.js not installed"; exit 1; }
command -v git >/dev/null 2>&1 || { echo "❌ Git not installed"; exit 1; }
echo "✅ Prerequisites check passed"
# Install dependencies
echo "📦 Installing dependencies..."
npm install
# Copy environment file
if [ ! -f .env ]; then
echo "📝 Creating .env file..."
cp .env.example .env
echo "⚠️ Please edit .env with your configuration"
fi
# Run database migrations
echo "🗄️ Running database migrations..."
npm run migrate
# Verify setup
echo "🔍 Verifying setup..."
npm run test:setup
echo "✅ Setup complete! Run 'npm run dev' to start"
相关技能
@brainstorming- 在安装前规划环境需求@systematic-debugging- 调试环境问题@doc-coauthoring- 创建安装文档@git-pushing- 设置 Git 配置
其他资源
- Node.js 安装指南
- Python 虚拟环境
- Docker 文档
- Homebrew(macOS)
- Chocolatey(Windows)
- nvm(Node Version Manager)
- pyenv(Python Version Manager)
专业提示: 创建一个 setup.sh 或 setup.ps1 脚本来自动化整个安装过程。在干净的系统中测试它以确保其正常工作!
评论
支持 Markdown