Gemini CLI Installation and Usage Explained in 10 Minutes
Before installing Gemini CLI, it's essential to have Node.js on your local machine. If you haven't done so, please navigate to the official Node.js website and download the appropriate version for your operating system.
Once Node.js is installed, execute the following command in your terminal:
npm install -g @google/gemini-cli
Paste the command into your terminal. You will be prompted to install the necessary packages; type 'yes' to proceed. During the initial installation, you might be asked for permission to access files in your document folders. You should allow this.
Once installed, you'll be prompted to select a theme. Several options are available, including Default Dark, GitHub Dark, Default Light, and Google Code. For this guide, we will proceed with the 'Default Dark' theme. For authentication, choose the 'Login with Google' option. Click the 'Sign in with Google' button. A successful sign-in indicates that you have authenticated the Gemini CLI.
Troubleshooting Login Issues
If you encounter a login error related to a missing Google Cloud project environment variable, you'll need to configure it. This involves setting an environment variable named GOOGLE_CLOUD_PROJECT
to your specific project ID from the Google Cloud console.
You can locate your project ID on the Google Cloud dashboard after creating a project. Copy this ID and set it in your terminal. After setting the environment variable, re-run the installation command.
Getting Started with the CLI
Once authenticated, you can begin using the Gemini CLI, which utilizes the Gemini 2.5 Pro model. To see a list of available commands, use the /help
command. This will display various commands, including those for MCP, memories, tools, and shell commands (prefixed with an exclamation mark!).
For instance, you can run shell commands directly. Using !pwd
will display the current directory path.
Keyboard Shortcuts
Several keyboard shortcuts are available for a smoother workflow: - Shift + Enter: New line - Up/Down Arrows: Cycle through prompt history - Escape: Cancel operation - Ctrl + C: Quit the application
Integrating with Visual Studio Code
For a more integrated development experience, you can use the Gemini CLI directly within a code editor like Visual Studio Code. Start by opening an empty project folder in your editor. Then, run the same Gemini CLI command in the integrated terminal.
Analyzing a GitHub Repository
One powerful feature is the ability to let Gemini analyze a GitHub repository to understand its structure and suggest modifications. For this example, we'll use the 'small-agent' repository, an agent that thinks in code.
First, clone the repository into your project folder:
bash
git clone https://github.com/example/small-agent.git
Back in the Gemini terminal, type @
followed by the folder name (e.g., small-agents
). You can use the Tab key for autocompletion. This specifies the path for Gemini to analyze. While you can target specific files, we will instruct it to read the entire repository.
Next, provide a prompt detailing your request. For example:
Analyze the overall architecture of this project, including the main modules, their responsibilities, the data flow, dependencies, use of design patterns, and potential architectural issues.
Understanding the Analysis
After running the command, Gemini provides a comprehensive analysis. The response typically includes: - Main Modules and Responsibilities: An overview of the project, such as identifying it as a Python library for building agents. - File-by-File Breakdown: Detailed explanations for each file, outlining core components like multi-step agents, code agents, and tool-calling agents. - Data Flow and Dependencies: Information on how data moves through the system and external dependencies like the Hugging Face Hub. - Design Patterns: A list of design patterns used in the codebase. - Potential Architectural Issues: Suggestions for improvement related to security, prompt logic coupling, and state management.
Leveraging MCP Tools
Now, let's explore how to use MCP (Multi-purpose Cooperative Platform) tools within the Gemini CLI. For this demonstration, we'll use the 'contact-7' MCP server, which can fetch up-to-date documentation for any code repository. This allows you to pull data, code examples, and documentation directly into your prompts.
You can check your list of configured MCP servers with the /mctp
command. If none are configured, you'll need to add them to your settings.json
file. The necessary connection settings can typically be found in the tool's repository.
To modify the file, navigate to the .gemini
configuration directory and open settings.json
with a text editor like Nano. You will then add an MCP server configuration object to the JSON file.
To make this demonstration more interesting, we'll add a second MCP server, 'Taskmaster AI', to show how multiple servers can be used. Taskmaster AI is an AI-powered task management system that integrates well with code editors.
Note: Before proceeding, ensure you have set the necessary environment variables, such as the API key for the large language model you are using.
Save the settings.json
file after making your changes (e.g., Ctrl+O
, then Ctrl+X
in Nano).
Querying with MCP Servers
After restarting the Gemini CLI, your newly configured MCP servers will be available. Running /mctp
again will now list the available servers.
Let's use the 'contact-7' MCP to search for the latest features in AutoGen. The tool may ask for clarification or permission to proceed with certain actions. Respond with 'yes' to continue. The MCP server will then begin its inquiry, extracting the relevant documentation.
The final response will provide a summary of the findings, such as a list of new features for both the .NET and Python versions of AutoGen.
Join the 10xdev Community
Subscribe and get 8+ free PDFs that contain detailed roadmaps with recommended learning periods for each programming language or field, along with links to free resources such as books, YouTube tutorials, and courses with certificates.