Getting Started with Gemini CLI

1. Installation

Official installation methods for Gemini CLI:

Method 1: Run directly from GitHub (no installation required)

npx @google/gemini-cli

Method 2: Global installation (recommended)

npm install -g @google/gemini-cli

After installation, you can use it directly:

gemini

Note:

  • Make sure your Node.js version is 18 or higher
  • You might need to use sudo on Linux/Mac if you encounter permission issues
  • If you have problems running directly from GitHub, try the second installation method

2. Getting Your API Key

To use Gemini CLI, you'll need an API key from Google AI Studio:

  1. Go to Google AI Studio
  2. Sign in with your Google account
  3. Navigate to the API keys section
  4. Create a new API key

Once you have your key, set it in your environment:

export GEMINI_API_KEY=your_api_key_here

For Windows users:

set GEMINI_API_KEY=your_api_key_here

For persistent configuration, add it to your shell profile or use the CLI config:

gemini config set apiKey your_api_key_here

3. Verify Installation

Verify that Gemini CLI is installed correctly:

gemini --version

When running for the first time, you will see the following steps:

  1. Choose a color theme (you can select from multiple options that suit your terminal)
  2. Authenticate (you can use either your personal Google account or an API key)

Simple test:

gemini

Then at the prompt, enter:

> Hello, what can you do for me?

4. Real-world Examples

Exploring a New Codebase

cd your-project-directory
gemini
> Describe the main pieces of this system's architecture.

Working with Existing Code

cd your-project-directory
gemini
> Help me migrate this codebase to the latest version of Java. Start with a plan.

Automating Workflows

gemini
> Make me a slide deck showing the git history from the last 7 days, grouped by feature and team member.

Basic Commands

Chat Mode

Start an interactive chat session:

gemini chat

This opens an interactive session where you can have a conversation with Gemini.

One-off Questions

For quick questions without starting a chat:

gemini ask "How do I check disk space on Linux?"

Code Generation

Generate code directly:

gemini code "Create a Python function to fetch JSON data from an API"

Summarizing Text

Summarize text content:

gemini summarize --file document.txt

Or pipe content:

cat document.txt | gemini summarize

Advanced Usage

Working with Files

Process file content:

gemini ask --file code.js "Explain what this code does"

Generate content to a file:

gemini code "Create a React component for a login form" > LoginForm.jsx

Custom Model Selection

Choose specific Gemini models:

gemini ask --model gemini-pro-vision "Describe this image" --file image.jpg

Streaming Responses

Enable streaming for real-time responses:

gemini ask --stream "Write a short story about a robot"

Shell Integration

Use Gemini to help with shell commands:

gemini shell "How to find all files modified in the last 7 days"

Power Features

MCP Servers (Model Context Protocol)

Connect to MCP servers to extend Gemini's capabilities:

gemini chat

Inside the chat session:

/mcp connect http://localhost:3000

You can also specify MCP servers at startup:

gemini chat --mcp http://localhost:3000

MCP servers help Gemini integrate with services like:

  • Image generation with Imagen
  • Video generation with Veo
  • Music generation with Lyria
  • Custom enterprise services

Built-in Tools

Gemini CLI includes powerful built-in tools:

  • grep: Search for patterns in files
  • terminal: Execute shell commands
  • file read/write: Manipulate file contents
  • web search: Search the internet for information
  • web fetch: Download content from URLs

To see available tools in chat:

/tools list

Special Commands

Gemini CLI supports special commands in chat mode:

  • /memory: Work with Gemini's long-term memory
  • /stats: Display statistics about your current session
  • /tools: Manage and use built-in tools
  • /mcp: Connect and manage MCP servers

For example, to save something to memory:

/memory save "My project uses React 18 and TypeScript 5"

Context Window

Gemini CLI can handle large codebases with its 1M token context window:

gemini chat

Then ask about your codebase:

What's the architecture of this project?

Gemini will analyze your files and provide insights, all while keeping the context in memory.

Customization

Configuration Settings

View current configuration:

gemini config list

Set a configuration value:

gemini config set temperature 0.7

Custom Aliases

Create command aliases for frequent operations:

gemini alias add explain="ask 'Explain the following code:'"

Then use your alias:

gemini explain --file script.py

Creating Custom Instructions

Set persistent system instructions:

gemini config set systemInstruction "You are a helpful assistant specializing in JavaScript development."

Tips & Tricks

Saving Chat History

Export your chat history to a file:

gemini chat --save chat_history.json

Continue from a previous chat:

gemini chat --load chat_history.json

Combining with Other Tools

Pipe from other commands:

git diff | gemini ask "Summarize these code changes"

Improving Response Quality

Be specific in your prompts for better results:

gemini ask "Write a Python function to parse CSV files that handles quoted fields and escaped quotes"

Instead of just:

gemini ask "Write a CSV parser"

Ready to Try Gemini CLI?

Start using Gemini CLI today and transform your terminal into an AI-powered productivity hub.

Get Started Now