How to Generate Images with Gemini CLI and Nano Banana Extension
This tutorial will guide you through installing and using the Nano Banana extension for the Gemini CLI, enabling you to generate images directly from your terminal. You’ll learn how to set up the extension, configure API keys, and use new commands to create various types of images for your projects.
Prerequisites
- Gemini CLI installed
- An API key for Google AI Studio (the same one used for Gemini CLI authentication can be reused)
- Node.js and npm installed (for global extension installation)
Step 1: Install the Nano Banana Extension
The first step is to install the Nano Banana extension. This extension adds new image generation capabilities to your Gemini CLI. You’ll install it globally, not just within a specific project.
- Open your terminal.
- Visit the Gemini CLI extensions page and search for the “Nano Banana” extension.
- Locate the installation command for the extension. It will typically look like this:
gemini install nanobanana - Copy this command.
- Paste the command into your terminal and press Enter.
- After the installation completes, you should see a new folder for the nanobanana extension appear in your global Gemini extensions directory (often found within a
.geminifolder on your system).
Step 2: Configure Your API Key
The Nano Banana extension requires an API key to function, as image generation services have associated costs. You can use the same API key you generated for the Gemini CLI.
- Ensure you have your Google AI Studio API key. If you don’t have one, follow the instructions from earlier lessons (like Lesson 6) to create one.
- Locate your project’s
.envfile. If it doesn’t exist, create it in the root of your project. - Add a new environment variable to the
.envfile for the Nano Banana API key. Name itNANOBANANA_GEMINI_API_KEY(all uppercase). - Paste your API key as the value for this variable.
Example:
NANOBANANA_GEMINI_API_KEY=YOUR_API_KEY_HERE - Save the
.envfile.
Expert Note: Be cautious about sharing your API key. The tutorial creator plans to delete their key after recording, and you should take similar precautions to avoid unauthorized usage and potential charges.
Step 3: Explore Nano Banana Commands
Once the extension is installed and configured, new commands become available within the Gemini CLI. You can access these by initiating a Gemini chat session.
- Open your terminal and start a new Gemini chat instance by typing
gemini. - In the chat interface, type a forward slash (
/). - Press the up arrow key (
↑) or scroll through the available commands. You will see new commands added by the Nano Banana extension, such as:/diagram: To generate diagrams./edit: To edit existing images./generate: To generate new images from prompts./icon: To create icons.
Step 4: Generate Images with a Prompt
Now you can use the /generate command to create images. You can also provide context from your project files to help Gemini understand your request better.
- In the Gemini chat, select the relevant file(s) or code snippets you want to provide context for. For example, if you want to generate images for combo cards on a homepage, select the homepage file.
- Type the
/generatecommand. - Follow the command with a clear prompt describing the images you want. For instance:
I need three images for the food combinations on the homepage. One for cheese and marmite, one for crisps and chocolate, and one for strawberries and pepper. - Press Enter.
- Gemini will likely ask for permission to use the Nano Banana MCP (Multi-modal Content Processing) tool. Choose to allow it, either for this instance or always.
- After processing, Gemini will indicate that the images have been generated and saved, usually in a specific output directory like
nano_banana_output.
Step 5: Review Generated Images
Check the output directory to see the images created by the Nano Banana extension.
- Navigate to the output directory specified by Gemini (e.g.,
nano_banana_output) using your file explorer. - You should find the generated images, often named or organized based on your prompt (e.g., one for cheese and marmite, one for crisps and chocolate, etc.).
- Open the images to review their quality and relevance to your prompt.
Tip: The cost per image typically ranges from 15 to 20 cents, depending on the image quality and resolution.
Step 6: Use Generated Images in Your Project
Once you are satisfied with the generated images, you can instruct Gemini to use them within your project files.
- Re-open the Gemini chat interface.
- Ensure the relevant project file (e.g., your homepage file) is in context.
- Ask Gemini to incorporate the newly generated images. For example:
Can you add the three new images to the combo cards on the homepage? - Gemini will process this request, potentially moving the images to appropriate directories (like
public) and updating your code with image URLs or references. - You might encounter an
MCP error context 7. This is a known issue that can usually be resolved by restarting your Gemini chat session.
Step 7: Preview Your Changes
Finally, check your project in a web browser to see the generated images integrated into your website.
- Open your project in a web browser.
- Navigate to the section where the images were supposed to be added (e.g., the combo cards on the homepage).
- Verify that the images have been correctly displayed.
Congratulations! You have successfully installed and used the Nano Banana extension to generate and integrate images into your project using the Gemini CLI.
Source: Gemini CLI Tutorial #10 – Using the Nanobanana Extension (YouTube)