This guide provides instructions on how to install and set up the Gemini CLI to interact with Gemini services from your terminal.
Before you begin, ensure you have the following installed on your system:
- A compatible operating system: Linux, macOS, or Windows (with WSL).
- A package manager: Depending on the installation method, you will need either
npm(which comes with Node.js) orpip(which comes with Python).
sudo apt-get update && sudo apt-get upgrade -y
sudo apt install curl iptables build-essential git wget lz4 jq make gcc nano automake autoconf tmux htop nvme-cli libgbm1 pkg-config libssl-dev libleveldb-dev tar clang bsdmainutils ncdu unzip libleveldb-dev -yInstall Nodejs & npm in one go
# 1️⃣ Remove any existing node + npm (optional if you want a clean slate)
sudo apt remove -y nodejs npm
# 2️⃣ Update and install Node.js 22 from NodeSource
curl -fsSL https://deb.nodesource.com/setup_22.x | sudo -E bash -
sudo apt install -y nodejs
# 3️⃣ Verify installation
node -v # should show v22.x
npm -v # should show ~10.x (latest stable)Choose one of the following installation methods. The appropriate method depends on how the Gemini CLI is packaged and distributed.
Install the CLI: Open your terminal and run the following command. The -g flag installs it globally, making the gemini command available system-wide.
npm install -g @google/gemini-cli@latestIf the Gemini CLI is distributed as a Python package, you can install it using pip.
- Install Python: If you don't have Python, download and install it from python.org. Version 3.8 or higher is recommended.
- Install the CLI: Open your terminal and run the following command:
pip install google-gemini-cliAfter the installation is complete, verify that the CLI is accessible from your terminal by checking its version.
source ~/.bashrc
gemini --version
- Visit Google Studio and sign in or sign up with your Google account.
- Attempt creating an API Key, it might not let you without you creating a default project on Google Console. If it does prompt you to do so? You would need to visit Google Console, sign in or sign up with your Google account, then at the top left of your screen you should see a button "Select Project" or as in my screenshot where
testxis at the top. - Click it then on the dialog box that poos up, click "Create a new project" on the top right. Name the project whatever you like, don't input a location, just "Create".
- If you have done this, while still on the Google Console homepage, click on the navigation menu at the top-left of the page.
Navigate this way:
APIs & Services >> Enabled APIs & services - Click on the
+ Enable APIs & servicesbutton to enable an API. - Search for
Gemini API, it should be the first option on the list, click it & enable it. - Now, go back to
Google Studioand refresh the page, you should now be able to get an API key by choosing your project which you created inGoogle Consoleand creating an API key. Copy the key and do this in your terminal.
echo 'export GOOGLE_API_KEY="YOUR_API_KEY"' >> ~/.bashrc
source ~/.bashrcReplace YOUR_API_KEY with the API Key you copied from Google Studio
NB: You must always usesource ~/.bashrcwhenever you restart your terminal so you don't meet the login interface.
NB: If you ever exhaust your daily quota of requests you either use another Google mail to generate a new API Key or you wait for the day to reset. 😂😂😂
NB: If your terminal restarts or you quitGeminithe CLI loses memory, so always make sure you have a good internet connection and try to document your queries so you can easily remind it about what it had done for you in the past.
geminiYou run this command from either your root directory or the particular folder you are trying to work with. Now, Gemini recommends the latter because it helps it to pinpoint your needs and reduces waste of time and resources.
The first time you run the command or any time you start Gemini without running source ~/.bashrc after previously exporting your API Key to the .bashrc file, you will be prompted to choose between three (3) methods you would prefer to use to firing up the Gemini CLI service.
Recommended Option: 2. Use Gemini API Key (Based on how this guide is tailored)
Follow the on-screen instructions using your arrow keys to choose an option amongst the 3 you were given, to complete the authentication process.
Welcome! This guide will help you effectively use the Gemini CLI agent for your software engineering tasks.
The primary goal is to help you with your development tasks safely and efficiently. The interaction process follows a consistent pattern:
- You Give a Command: You ask Gemini to do something (e.g., "Refactor this file," "Find all usages of this function," "Run the tests").
- It Analyzes & Plans: Gemini will use its tools to understand your codebase, the context of your request, and form a step-by-step plan. It will often check for tests, project dependencies, and existing code conventions.
- It Propose an Action: It will show you the exact command or code change it intends to make.
- You Approve or Deny: No action is taken without your explicit approval. You will be prompted to confirm each step, giving you complete control over your system and codebase.
- It Executes & Verifies: Once you approve, it runs the command. For code changes, it will try to run tests or linters to ensure the changes are safe and correct.
You can ask Gemini to perform a wide range of tasks.
- Read & Analyze Code: "What does the
calculate_totalsfunction insrc/utils.pydo?" - Write & Refactor Code: "Add a new function to
UserService.tsthat deletes a user." or "Refactordata_processor.jsto use async/await instead of promises." - Search the Codebase: "Where is the
API_ENDPOINTvariable defined?" or "Find all files that import therequestslibrary." - Write Tests: "Write a unit test for the
format_datefunction inutils.js."
- Run Shell Commands: "List all files in the
scriptsdirectory." or "Run the database migration script." - Run Tests, Linters, and Builds: "Run the test suite." or "Run
npm run lintand fix any errors." - Manage Files: "Create a new file named
config.jsonin thesrc/directory." or "Delete all.logfiles."
- Answer Technical Questions: "What's the difference between a
letandconstin JavaScript?" - Explain Complex Code: "Can you explain the authentication flow in this project?"
- Create New Applications: "Scaffold a new React application with TypeScript and a Node.js backend."
To get the best results, follow these tips:
- Be Specific: The more detailed your request, the better it can help. Instead of "Fix the bug," try "The login button isn't working. I think the error is in
auth.js. Please investigate and fix it." - Provide Context: Mention relevant filenames, function names, or project goals. This helps it narrow down the search and understand your intent.
- Trust the Process: Gemini is designed to be cautious. It will ask clarifying questions and present a plan before making changes. Work with it through this process to ensure the best outcome.
- Use the
/helpcommand if you need a refresher on what it can do.
Here’s a realistic example of how you both might work together:
You:
"Refactor
src/api/http.jsto use theaxioslibrary instead of the nativefetchAPI."
Me (Gemini):
Okay, I can do that. First, I'll check if
axiosis already a project dependency.[TOOL CALL: read_file for /root/project/package.json]
(After seeing axios is in package.json)
Great,
axiosis available. Now, I'll read the contents of the file to be changed.[TOOL CALL: read_file for /root/project/src/api/http.js]
(After analyzing the file)
I have a plan to refactor the file. I will replace the
fetchcall with anaxios.getcall and adjust the error handling. I will then run the project's linter to ensure style consistency.[TOOL CALL: replace in /root/project/src/api/http.js with the new code]
(You approve the change)
Me (Gemini):
The file has been updated. Now, I'll run the linter to verify the changes.
[TOOL CALL: run_shell_command for 'npm run lint']
(After tests pass)
All checks passed. The refactoring is complete.
I hope this guide is useful to your development process. 🫡