How To Use ChatGPT In Linux From Terminal

How To Use ChatGPT In Linux Terminal

ChatGPT is everywhere these days. Thanks to some of the amazing technology out there for creating AI tool like ChatGPT that has made our life somehow easy. If you are a Linux users and wants to use ChatGPT from your Linux terminal than you are at the right blog post.

In this blog post, we will help you to set up and use ChatGPT in Linux based operating systems.

How To Use ChatGPT In Linux From Terminal

In order to use ChatGPT in Linux, we need to install a tool called ShellGPT in Linux to. As there is not any official ChatGPT tool for Linux,  ShellGPT will be handy.

Before installing ShellGPT in Linux, you need to have python installed in your Linux. Python comes preinstalled on most latest Linux based operating system.

Run the following command to know whether Python is installed or not.

python3 --version

If you see any errors, then the python is not installed or there is the old version of Python. Run the following command to install Python:

For Ubuntu:

sudo apt update && sudo apt upgrade -y
sudo apt install python3

If an older version of Python is installed, run the following command to update Python to the latest version.

sudo apt --only-upgrade install python3

How to Install Pip Package Manager in Linux

It generally comes preinstalled with Python in most Linux distributions. Just in case if there is not Pip installed then run the following command.

sudo apt-get -y install python3-pip

How to Install ShellGPT on Linux

At first, you need to create a directory.

mkdir <newdirectoryname>

After creating a directory, switch over to the new directory

cd <newdirectoryname>

Now, Create a new virtual environment. First, install the venv model.

sudo apt install python3-venv
python3 -m venv  

Run the following command to activate the virtual environment that you have created above:

source /bin/activate

Get Your OpenAI API Key

  • Browse OpenAI’s website and create a new OpenAI account or If you already have an account, simply log in.
  • Click on your profile image at the top right corner and select “View API keys” from the drop-down menu.
  • Click the “Create new secret key” button to generate a new API key. Do not share this API key with anyone or share it publicly.

Now, create an environment variable for this API key with the command below.

export OPENAI_API_KEY=<your_OpenAI_API_key_here_API key you generated to use ChatGPT>

Now, run the following env command to verify the environment variable:


Open the .bashrc file in the text editor and add the variable at the end of the file to store the API key permanently.

export OPENAI_API_KEY=<your_OpenAI_API_key_here_API key you generated to use ChatGPT>

Save the file and exit the text editor.

Now run the following  command to install ShellGPT:

pip3 install shell-gpt --user

How to Use ChatGPT in Linux with Examples

Run the following command to get the information using ChatGPT in Linux.  For example, if you need to know the run scored by sachin, use this command:

sgpt " run scored by sachin"

Leave a Comment