Gemini CLI authentication setup
To use Gemini CLI, you’ll need to authenticate with Google. This guide helps you quickly find the best way to sign in based on your account type and how you’re using the CLI.
For most users, we recommend starting Gemini CLI and logging in with your personal Google account.
Choose your authentication method
Section titled “Choose your authentication method ”Select the authentication method that matches your situation in the table below:
| User Type / Scenario | Recommended Authentication Method | Google Cloud Project Required |
|---|---|---|
| Individual Google accounts | Sign in with Google | No, with exceptions |
| Organization users with a company, school, or Google Workspace account | Sign in with Google | Yes |
| AI Studio user with a Gemini API key | Use Gemini API Key | No |
| Google Cloud Vertex AI user | Vertex AI | Yes |
| Headless mode | Use Gemini API Key or Vertex AI | No (for Gemini API Key) Yes (for Vertex AI) |
What is my Google account type?
Section titled “What is my Google account type?”-
Individual Google accounts: Includes all free tier accounts such as Gemini Code Assist for individuals, as well as paid subscriptions for Google AI Pro and Ultra.
-
Organization accounts: Accounts using paid licenses through an organization such as a company, school, or Google Workspace. Includes Google AI Ultra for Business subscriptions.
(Recommended) Sign in with Google
Section titled “(Recommended) Sign in with Google ”If you run Gemini CLI on your local machine, the simplest authentication method is logging in with your Google account. This method requires a web browser on a machine that can communicate with the terminal running Gemini CLI (for example, your local machine).
If you are a Google AI Pro or Google AI Ultra subscriber, use the Google account associated with your subscription.
To authenticate and use Gemini CLI:
-
Start the CLI:
Terminal window gemini -
Select Sign in with Google. Gemini CLI opens a sign in prompt using your web browser. Follow the on-screen instructions. Your credentials will be cached locally for future sessions.
Do I need to set my Google Cloud project?
Section titled “Do I need to set my Google Cloud project?”Most individual Google accounts (free and paid) don’t require a Google Cloud project for authentication. However, you’ll need to set a Google Cloud project when you meet at least one of the following conditions:
- You are using a company, school, or Google Workspace account.
- You are using a Gemini Code Assist license from the Google Developer Program.
- You are using a license from a Gemini Code Assist subscription.
For instructions, see Set your Google Cloud Project.
Use Gemini API key
Section titled “Use Gemini API key ”If you don’t want to authenticate using your Google account, you can use an API key from Google AI Studio.
To authenticate and use Gemini CLI with a Gemini API key:
-
Obtain your API key from Google AI Studio.
-
Set the
GEMINI_API_KEYenvironment variable to your key. For example:macOS/Linux
Terminal window # Replace YOUR_GEMINI_API_KEY with the key from AI Studioexport GEMINI_API_KEY="YOUR_GEMINI_API_KEY"Windows (PowerShell)
Terminal window # Replace YOUR_GEMINI_API_KEY with the key from AI Studio$env:GEMINI_API_KEY="YOUR_GEMINI_API_KEY"To make this setting persistent, see Persisting Environment Variables.
-
Start the CLI:
Terminal window gemini -
Select Use Gemini API key.
Use Vertex AI
Section titled “Use Vertex AI ”To use Gemini CLI with Google Cloud’s Vertex AI platform, choose from the following authentication options:
- A. Application Default Credentials (ADC) using
gcloud. - B. Service account JSON key.
- C. Google Cloud API key.
Regardless of your authentication method for Vertex AI, you’ll need to set
GOOGLE_CLOUD_PROJECT to your Google Cloud project ID with the Vertex AI API
enabled, and GOOGLE_CLOUD_LOCATION to the location of your Vertex AI resources
or the location where you want to run your jobs.
For example:
macOS/Linux
# Replace with your project ID and desired location (for example, us-central1)export GOOGLE_CLOUD_PROJECT="YOUR_PROJECT_ID"export GOOGLE_CLOUD_LOCATION="YOUR_PROJECT_LOCATION"Windows (PowerShell)
# Replace with your project ID and desired location (for example, us-central1)$env:GOOGLE_CLOUD_PROJECT="YOUR_PROJECT_ID"$env:GOOGLE_CLOUD_LOCATION="YOUR_PROJECT_LOCATION"To make any Vertex AI environment variable settings persistent, see Persisting Environment Variables.
A. Vertex AI - application default credentials (ADC) using gcloud
Section titled “A. Vertex AI - application default credentials (ADC) using gcloud”Consider this authentication method if you have Google Cloud CLI installed.
If you have previously set GOOGLE_API_KEY or GEMINI_API_KEY, you must unset
them to use ADC.
macOS/Linux
unset GOOGLE_API_KEY GEMINI_API_KEYWindows (PowerShell)
Remove-Item Env:\GOOGLE_API_KEY, Env:\GEMINI_API_KEY -ErrorAction Ignore-
Verify you have a Google Cloud project and Vertex AI API is enabled.
-
Log in to Google Cloud:
Terminal window gcloud auth application-default login -
Start the CLI:
Terminal window gemini -
Select Vertex AI.
B. Vertex AI - service account JSON key
Section titled “B. Vertex AI - service account JSON key”Consider this method of authentication in non-interactive environments, CI/CD pipelines, or if your organization restricts user-based ADC or API key creation.
If you have previously set GOOGLE_API_KEY or GEMINI_API_KEY, you must unset
them:
macOS/Linux
unset GOOGLE_API_KEY GEMINI_API_KEYWindows (PowerShell)
Remove-Item Env:\GOOGLE_API_KEY, Env:\GEMINI_API_KEY -ErrorAction Ignore-
Create a service account and key and download the provided JSON file. Assign the “Vertex AI User” role to the service account.
-
Set the
GOOGLE_APPLICATION_CREDENTIALSenvironment variable to the JSON file’s absolute path. For example:macOS/Linux
Terminal window # Replace /path/to/your/keyfile.json with the actual pathexport GOOGLE_APPLICATION_CREDENTIALS="/path/to/your/keyfile.json"Windows (PowerShell)
Terminal window # Replace C:\path\to\your\keyfile.json with the actual path$env:GOOGLE_APPLICATION_CREDENTIALS="C:\path\to\your\keyfile.json" -
Start the CLI:
Terminal window gemini -
Select Vertex AI.
C. Vertex AI - Google Cloud API key
Section titled “C. Vertex AI - Google Cloud API key”-
Obtain a Google Cloud API key: Get an API Key.
-
Set the
GOOGLE_API_KEYenvironment variable:macOS/Linux
Terminal window # Replace YOUR_GOOGLE_API_KEY with your Vertex AI API keyexport GOOGLE_API_KEY="YOUR_GOOGLE_API_KEY"Windows (PowerShell)
Terminal window # Replace YOUR_GOOGLE_API_KEY with your Vertex AI API key$env:GOOGLE_API_KEY="YOUR_GOOGLE_API_KEY"If you see errors like
"API keys are not supported by this API...", your organization might restrict API key usage for this service. Try the other Vertex AI authentication methods instead. -
Start the CLI:
Terminal window gemini -
Select Vertex AI.
Set your Google Cloud project
Section titled “Set your Google Cloud project ”When you sign in using your Google account, you may need to configure a Google Cloud project for Gemini CLI to use. This applies when you meet at least one of the following conditions:
- You are using a Company, School, or Google Workspace account.
- You are using a Gemini Code Assist license from the Google Developer Program.
- You are using a license from a Gemini Code Assist subscription.
To configure Gemini CLI to use a Google Cloud project, do the following:
-
Configure your environment variables. Set either the
GOOGLE_CLOUD_PROJECTorGOOGLE_CLOUD_PROJECT_IDvariable to the project ID to use with Gemini CLI. Gemini CLI checks forGOOGLE_CLOUD_PROJECTfirst, then falls back toGOOGLE_CLOUD_PROJECT_ID.For example, to set the
GOOGLE_CLOUD_PROJECT_IDvariable:macOS/Linux
Terminal window # Replace YOUR_PROJECT_ID with your actual Google Cloud project IDexport GOOGLE_CLOUD_PROJECT="YOUR_PROJECT_ID"Windows (PowerShell)
Terminal window # Replace YOUR_PROJECT_ID with your actual Google Cloud project ID$env:GOOGLE_CLOUD_PROJECT="YOUR_PROJECT_ID"To make this setting persistent, see Persisting Environment Variables.
Persisting environment variables
Section titled “Persisting environment variables ”To avoid setting environment variables for every terminal session, you can persist them with the following methods:
-
Add your environment variables to your shell configuration file: Append the environment variable commands to your shell’s startup file.
macOS/Linux (for example,
~/.bashrc,~/.zshrc, or~/.profile):Terminal window echo 'export GOOGLE_CLOUD_PROJECT="YOUR_PROJECT_ID"' >> ~/.bashrcsource ~/.bashrcWindows (PowerShell) (for example,
$PROFILE):Terminal window Add-Content -Path $PROFILE -Value '$env:GOOGLE_CLOUD_PROJECT="YOUR_PROJECT_ID"'. $PROFILE
-
Use a
.envfile: Create a.gemini/.envfile in your project directory or home directory. Gemini CLI automatically loads variables from the first.envfile it finds, searching up from the current directory, then in your home directory’s.gemini/.env(for example,~/.gemini/.envor%USERPROFILE%\.gemini\.env).Example for user-wide settings:
macOS/Linux
Terminal window mkdir -p ~/.geminicat >> ~/.gemini/.env <<'EOF'GOOGLE_CLOUD_PROJECT="your-project-id"# Add other variables like GEMINI_API_KEY as neededEOFWindows (PowerShell)
Terminal window New-Item -ItemType Directory -Force -Path "$env:USERPROFILE\.gemini"@"GOOGLE_CLOUD_PROJECT="your-project-id"# Add other variables like GEMINI_API_KEY as needed"@ | Out-File -FilePath "$env:USERPROFILE\.gemini\.env" -Encoding utf8 -Append
Variables are loaded from the first file found, not merged.
Running in Google Cloud environments
Section titled “Running in Google Cloud environments ”When running Gemini CLI within certain Google Cloud environments, authentication is automatic.
In a Google Cloud Shell environment, Gemini CLI typically authenticates automatically using your Cloud Shell credentials. In Compute Engine environments, Gemini CLI automatically uses Application Default Credentials (ADC) from the environment’s metadata server.
If automatic authentication fails, use one of the interactive methods described on this page.
Running in headless mode
Section titled “Running in headless mode ”Headless mode will use your existing authentication method, if an existing authentication credential is cached.
If you have not already signed in with an authentication credential, you must configure authentication using environment variables:
What’s next?
Section titled “What’s next?”Your authentication method affects your quotas, pricing, Terms of Service, and privacy notices. Review the following pages to learn more: