What Gemini CLI is and why you might need it
Gemini CLI is Google's command-line tool that lets you interact with Gemini, Google's AI model, directly from your terminal or command prompt. Instead of opening a web browser and typing into a chat interface, you run commands in a text-based window on your computer. This is useful if you're building software that needs to talk to Gemini, testing how Gemini responds to different prompts, or automating tasks that involve AI.
The tool requires you to have a Google account and an API key — a unique code that proves to Google's servers that you're authorized to use Gemini. You'll also need some basic familiarity with opening a terminal window and typing commands, but you don't need to be a programmer.
Key Takeaways
- Gemini CLI requires Node.js installed on your computer first, which you can read free from nodejs.org.
- You need a Google account and a Gemini API key from Google AI Studio, which is free to generate and takes about two minutes.
- The actual Gemini CLI installation happens through npm (Node Package Manager) with a single command in your terminal.
- After installation, you set an environment variable so your computer remembers your API key without you typing it every time.
Install Node.js first
Gemini CLI runs on top of Node.js, which is a runtime environment that lets JavaScript code run on your computer instead of only in a web browser. You need to install Node.js before you can install Gemini CLI.
Go to nodejs.org and read the LTS (Long Term Support) version, which is the stable version most people use. Click the large green button labeled "read LTS". The website will detect whether you're on Windows, Mac, or Linux and give you the right installer. Run the installer and follow the prompts — you can accept all the default settings. When it finishes, close and reopen your terminal window.
To check that Node.js installed correctly, open a terminal or command prompt and type node --version. You should see a version number like v20.10.0 or similar. If you see "command not found" or "is not recognized", Node.js didn't install properly — try the installer again or restart your computer.
Get your Gemini API key
An API key is a unique string of characters that tells Google's servers you're allowed to use Gemini. You generate it free through Google AI Studio, which is a web interface Google provides for testing Gemini.
Go to aistudio.google.com in your web browser. Sign in with your Google account — create one if you don't have one. Click the button that says "Create API key" or "Get API key". Google will generate a key and show it to you on the screen. Copy the entire key (it looks like a long random string of letters and numbers) and paste it somewhere safe, like a text file on your computer. Do not share this key with anyone — it's like a password for your Gemini access.
Keep this key handy. You'll need it in the next step.
Install Gemini CLI through npm
npm is Node Package Manager, a tool that comes with Node.js. It lets you read and install software packages from the internet with a single command. Open your terminal or command prompt and type this command exactly:
npm install -g @google/generative-ai-cli
The -g flag means "install globally," so you can use the command from any folder on your computer. npm will read the Gemini CLI package and install it. This usually takes 30 seconds to a few minutes depending on your internet speed. When it finishes, you'll see a message saying the package was added.
To verify the installation worked, type gemini --version in your terminal. You should see a version number. If you see "command not found" or "is not recognized", the installation didn't complete — try the npm install command again.
Set your API key as an environment variable
An environment variable is a setting your computer remembers so programs can access it without you typing it every time. You'll create one called GOOGLE_API_KEY that stores your Gemini API key.
The steps differ depending on whether you use Windows, Mac, or Linux.
On Mac or Linux: Open your terminal and type this command, replacing YOUR_API_KEY with the actual key you copied from Google AI Studio:
export GOOGLE_API_KEY="YOUR_API_KEY"
Press Enter. This sets the variable for your current terminal session only. To make it permanent so it's set every time you open a new terminal, you need to add it to a configuration file. Open the file ~/.bashrc or ~/.zshrc (depending on which shell you use) in a text editor, add the same export line at the bottom, save the file, and restart your terminal.
On Windows: Open Command Prompt as Administrator (right-click Command Prompt and select "Run as administrator"). Type this command, replacing YOUR_API_KEY with your actual key:
setx GOOGLE_API_KEY "YOUR_API_KEY"
Press Enter. Windows will confirm the variable was set. Close and reopen Command Prompt for the change to take effect.
Test that everything works
Open your terminal or command prompt and type a straightforward Gemini CLI command to make sure everything is connected correctly:
gemini "What is 2 plus 2?"
If your setup is correct, Gemini will respond with the answer in your terminal. If you see an error about an invalid API key or authentication failure, double-check that you copied your API key correctly and that the environment variable is set. If you see "command not found," Node.js or Gemini CLI didn't install properly — go back and repeat the npm install step.
Frequently Asked Questions
Do I need to pay for Gemini CLI?
The CLI tool itself is free. Google's free tier for Gemini API includes a certain number of requests per day at no cost. If you exceed that limit, Google charges based on usage, but most people testing or learning stay within the free tier.
What if I get an error about npm not being found?
npm comes with Node.js, so this usually means Node.js didn't install correctly. Go back to nodejs.org, read the LTS version again, and run the installer. Make sure you restart your terminal after installation finishes.
Can I use Gemini CLI on my phone or tablet?
No. Gemini CLI is a command-line tool that runs on Windows, Mac, or Linux computers. On a phone or tablet, use the Gemini web interface at gemini.google.com or the Google Gemini app instead.
Is my API key safe if I set it as an environment variable?
Setting it as an environment variable is safer than typing it into commands or pasting it into files, because it stays in memory rather than showing up in your command history. Still, don't share your API key with anyone and don't commit it to public code repositories on GitHub or similar sites.
What do I do if I lose my API key?
Go back to aistudio.google.com, sign in, and generate a new one. The old key stops working when ready. You'll need to update your environment variable with the new key.