Want to chat with ChatGPT directly from your command line? ShellGPT—a Python-based CLI wrapper for OpenAI’s GPT—makes it easier to use ChatGPT In Terminal Ubuntu command line.
With this tool users can seamlessly send prompts, generate shell commands, code snippets, documentation, and even chat interactively—all from their terminal.
Before getting started, make sure you have:
- A Linux distribution (e.g. Ubuntu, Debian, Arch)
- python3, pip3 (Recommended) python3-venv for creating isolated environments
- An OpenAI API key (visit your OpenAI dashboard to generate one)
Step 1: Instal Python On Debian/Ubuntu:
sudo apt update -y
sudo apt install python3 python3-pip python3-venv -y
Step 2: Create and Activate a Virtual Environment
mkdir shellgpt && cd shellgpt
python3 -m venv venv
source venv/bin/activate
This keeps ShellGPT isolated from global Python packages—helpful for avoiding version conflicts.
Step 3: Install ShellGPT using Pip
Inside your activated venv:
pip install shell-gpt
This installs the sgpt CLI tool.
Step 4: Set Up Your OpenAI API Key
Temporarily export in your terminal:
export OPENAI_API_KEY="your_secret_key"
To make it persistent across sessions, add this line to your ~/.bashrc or ~/.zshrc, then run source ~/.bashrc:
echo 'export OPENAI_API_KEY="your_secret_key"' >> ~/.bashrc && source ~/.bashrc
Step 5: Use sgpt from the Command Line
You can run general queries and ShellGPT provides direct, human-like answers, saving you a web search:
sgpt "Where is Nginx default config file located?"
Users can also generate Shell Commands
sgpt --shell "Make all files in current directory read-only"
It will give you the following output:
chmod -R a-w .
Not only this, users can execute generated commands with confirmation and also generate code snippets
ShellGPT (sgpt) is a powerful CLI tool that embeds ChatGPT’s intelligence into your terminal. From generating code and shell commands to interactive chat, you can keep everything in your shell, speeding up development and operations.
CAUTION: Always review ShellGPT-generated commands before executing. Keep your API key private, and don’t share sensitive data via prompts.
