Getting Started with the Gemini CLI

1 Introduction to the Gemini CLI

The Gemini CLI is a command-line interface that allows you to interact with Google’s Gemini models directly from your terminal. It provides a convenient way to experiment with Gemini, test prompts, and build powerful shell scripts that leverage the capabilities of large language models.

This guide will walk you through the process of installing and configuring the Gemini CLI.

2 Prerequisites: Node.js Installation

The Gemini CLI is distributed as an npm package, so you need to have Node.js and npm installed on your system. The recommended way to install Node.js is by using the Node Version Manager (nvm).

Code
# Download and install nvm
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash

# Activate nvm for the current shell session
source ~/.nvm/nvm.sh

# Install the latest stable version of Node.js
nvm install 20

# Verify the installation
node -v
npm -v

3 Installing the Gemini CLI

Once you have Node.js and npm ready, you can install the Gemini CLI globally on your system.

Code
# Install the Gemini CLI package
npm install -g @google/gemini-cli

3.1 Upgrading the Gemini CLI

To ensure you have the latest features and bug fixes, you can upgrade the package from time to time.

Code
# Upgrade the Gemini CLI to the latest version
npm upgrade -g @google/gemini-cli

4 Configuration

To use the Gemini CLI, you need to authenticate with your Google account and configure your project.

4.1 Login with Your Google Account

You can either log in with your Google Cloud account or use an API key.

4.1.1 Option 1: Login with Google Cloud Account

Code
# Set your Google Cloud project ID
export GOOGLE_CLOUD_PROJECT="your-google-cloud-project-id"

or save the GOOGLE_CLOUD_PROJECT into environment variable.So that do not need to re enter everytime

4.1.1.1 check using zsh or bash

Code
echo $SHELL

4.1.1.2 for zsh

Code
echo 'export GOOGLE_CLOUD_PROJECT="your-google-cloud-project-id"' >> ~/.zshrc

source ~/.zshrc

4.1.1.3 for bash

Code
echo 'export GOOGLE_CLOUD_PROJECT="your-google-cloud-project-id"' >> ~/.bashrc

source ~/.bashrc

4.1.1.4 check wheather added or not

Code
echo $GOOGLE_CLOUD_PROJECT

4.1.2 Option 2: Login with API Key

Alternatively, you can use an API key for authentication.

Code
# Set your Gemini API key as an environment variable
export GEMINI_API_KEY="your-gemini-api-key"

4.2 Set the Location

You also need to specify the Google Cloud location where your resources will be managed.

Code
# Set the Google Cloud location
export GOOGLE_CLOUD_LOCATION='us-central1'

5 Running the Gemini CLI

Once everything is set up, you can run the Gemini CLI to start interacting with the models.

Code
# Run the Gemini CLI
gemini

6 Running the Gemini CLI with agreement to all access

Code
# Run the Gemini CLI
gemini --yolo

7 Conclusion

The Gemini CLI is a powerful tool for developers and enthusiasts who want to explore the capabilities of Google’s Gemini models from the command line. With its simple installation and configuration process, you can quickly start leveraging the power of generative AI in your daily workflows.

8 Reference