Environment Variables Setup
Configure Gemini CLI using environment variables for secure and flexible configuration management.
Why Use Environment Variables?
- ✓Security: Keep sensitive data like API keys out of your code
- ✓Flexibility: Different settings for development, staging, and production
- ✓Portability: Works across different systems and deployment environments
- ✓Team Collaboration: Each team member can have their own configuration
Core Environment Variables
GOOGLE_AI_API_KEY
Your Google AI API key for authentication
export GOOGLE_AI_API_KEY="AIza..."
Required for all Gemini CLI operations.Get your API key →
GEMINI_MODEL
Default model to use for requests
export GEMINI_MODEL="gemini-pro"
# or gemini-pro-vision, gemini-ultra
GEMINI_TEMPERATURE
Control randomness in responses (0.0 to 1.0)
export GEMINI_TEMPERATURE="0.7"
# 0.0 = deterministic, 1.0 = creative
GEMINI_MAX_TOKENS
Maximum tokens in response
export GEMINI_MAX_TOKENS="2048"
GEMINI_CONFIG_PATH
Custom path for configuration file
export GEMINI_CONFIG_PATH="/path/to/your/config.json"
Platform-Specific Setup
🐧Linux
Temporary (current session):
export GOOGLE_AI_API_KEY="your-key"
Permanent (bash):
echo 'export GOOGLE_AI_API_KEY="your-key"' >> ~/.bashrc
source ~/.bashrc
🍎macOS
Temporary:
export GOOGLE_AI_API_KEY="your-key"
Permanent (zsh):
echo 'export GOOGLE_AI_API_KEY="your-key"' >> ~/.zshrc
source ~/.zshrc
🪟Windows
Command Prompt:
set GOOGLE_AI_API_KEY=your-key
PowerShell:
$env:GOOGLE_AI_API_KEY="your-key"
Permanent: System Properties → Environment Variables
Using .env Files
For project-specific configuration, use .env files:
1. Create .env file:
# .env
GOOGLE_AI_API_KEY=your-api-key-here
GEMINI_MODEL=gemini-pro
GEMINI_TEMPERATURE=0.7
GEMINI_MAX_TOKENS=2048
2. Load .env file:
# Load environment variables from .env
set -a && source .env && set +a
Important: Add .env to your .gitignore file to avoid committing sensitive data to version control.
Verify Your Setup
Check if variables are set:
# Unix/Linux/macOS
echo $GOOGLE_AI_API_KEY
env | grep GEMINI
# Windows
echo %GOOGLE_AI_API_KEY%
Test Gemini CLI with environment variables:
gemini "Hello, world!"
Best Practices
✅ Do
- • Use different API keys for different environments
- • Keep API keys in environment variables, not in code
- • Use .env files for local development
- • Document required environment variables
- • Use validation scripts to check setup
❌ Don't
- • Commit .env files to version control
- • Share API keys in chat or email
- • Use production keys in development
- • Hardcode sensitive values in scripts
- • Ignore environment variable validation
Next Steps
Now that you have environment variables set up, configure your API key: