Easily Set Up and Use ChatGPT in the Linux Terminal

Licio Lentimo
5 min readMar 21, 2023

ChatGPT is a conversational AI model developed by OpenAI that launched in November 2022 and took the orld by storm. It is designed to respond to text-based queries and generate natural language responses. The model is based on the Generative Pretrained Transformer 3 (GPT-3) architecture, and most recently GPT-4 has been released, which is one of the largest and most advanced language models ever made.

For this article, e are going to leverage ShellGPT, a command-line tool powered by ChatGPT.

Prerequisites

For this command line tool to work, you need to install a couple of things on your Linux machine.

Python

Generally Python comes pre-installed on most Linux distros so you should probably check the version installed.

To check the Python version you have installed on your machine, run the command below:

python3 — version

Python version

If there are any errors or the version does not output, kindly run the command below to install Python 3.8:

sudo apt-get install python3.8

Pip

Pip is a standard package management tool used to install and manage software packages ritten in Python. You can check if pip is installed on your machine by running:

pip3 --version

pip version

If pip is not installed in your system you can install it by running the command below:

sudo apt-get install python3-pip

Venv

Installing a virtual environment is necessary as it installs Python packages in an isolated environment so as not to interfere with your local files. We shall be installing the venv module for this project by running the command below:

sudo apt install python3-venv

Now let us set up our virtual environment for use. Create a new directory/folder where we shall install all our dependencies.

mkdir chat-gpt

Now let us navigate into the new directory we just created:

cd chat-gpt

Let us now create our virtual environment by running the command below, where we have named ours myvenv:

python3 -m venv myvenv

We now need to activate our virtual environment for use:

source myvenv/bin/activate

We are now all set to begin.

Set up your virtual environment

Get Your OpenAI API Key

In order for this to work, we need to acquire an OpenAI API Key. First you need to register for an Open AI account.

Sign up if you haven’t and login if you already have an account.

Sign up or login to OpenAI

Once you login, you need to click on the Create New Secret Key button.

Create new API key

A pop up will appear prompting you to copy your API key. Copy your API key in a secure location as you can only view it once.

Next, head over to your terminal while still in your virtual environment and create an environment variable for our API key by running the command below:

export OPENAI_API_KEY=<paste_your_openai_key_here>

Replace <paste_your_openai_key_here> placeholder with your actual API key.

Since this is a virtual environment, this variable is only valid for the current session and will expire once we exit the environment. To store this variable permanently and avoid creating a new API key every now and then, we shall save it in the .bashrc file. This file is a script file that is executed when the user logs in and contains configurations for the terminal session. This includes settings for coloring, command history, command aliases and more.

Open a new terminal and run the command as root:

sudo nano .bashrc

The command above opens the nano text editor. Scroll to the bottom of the file and add the export command with your API key as shown below:

export OPENAI_API_KEY=<paste_your_openai_key_here>

Once, that is complete, exit the nano text editor by clicking on CTRL+X and then Y to save and press Enter.

Run the command below for changes to take effect:

source .bashrc

Installing and Running ShellGPT

Now we need to install ShellGPT in our virtual environment. run the command below to install Shell GPT:

pip3 install shell-gpt

Install ShellGPT in our virtual environment

Now that we have ShellGPT installed, we can run commands using the sgpt command. For example, I ran the command below:

Command asking for the capital city of Kenya

You can also use the --chat option to trigger a more conversational response as shown below where I asked the AI how many time 7 appears between 1 and 1000:

Response on how many time 7 appears between 1–1000

We can also generate code by using the --code option as shown below:

Generating fibonacci sequence in Python

So as you can see, this is a fun and interactive way of working with the power of ChatGPT in the terminal. Not only does this make it easier, but also there are so many options to choose from.

--

--

Licio Lentimo

I write content on Android and Web technologies. Currently focusing on Cybersecurity. Find me on liciolentimo.com