Set Up and Use ChatGPT in Linux Terminal

Setting Up ChatGPT in Linux

This post will demonstrate the set up and use ChatGPT in Linux Terminal. ShellGPT gives you the same ChatGPT AI bot in the Linux command line. We are in the era of Artificial Intelligence (AI) and we have seen how it has revolutionized the way we work. The advent of ChatGPT by OpenAI further enhances how we work, interact, and get work done quickly. Please see How to work with Azure Cognitive Service, How to find disabled Active Directory User accounts, and the Benefits of Azure API Management and how to create an API instance, How to setup PowerShell on a Linux server.

In the realm of technology, AI has become the newest buzzword. Online, ChatGPT and its variants are thriving, assisting users in carrying out routine chores with ease. In the past, we have written guidelines on how to get started with creating your own ChatGPT-like chatbot, utilizing Bing AI in any browser, and more. See how to Use Wiki from Linux Terminal also.

What if you run Linux and wish to install an AI chatbot on your system? Well, ShellGPT comes into play in this situation. It directly integrates ChatGPT’s power into your Linux Terminal. ShellGPT assists you efficiently. It helps you navigate the command line by providing solutions, insightful suggestions, and writing commands and code. You can also learn how to associate SSH Public key with Azure Linux VM. The security of your system is paramount, see how to set Two-Factor Authentication for SSH in Linux

Setting Up ChatGPT in Linux Terminal

To set up ChatGPT on your Linux Terminal, follow the below steps: Please see GNS3 Setup: Error opening file for writing, click on Abort to stop installation, Retry to try again, or Ignore to skip this file. How to modify a Linux EBS Volume from the Console, and How to install Docker Engine on Ubuntu.

Step 1: Install Python

To install Python, open a terminal and type in the following commands to check Python’s version:

python3 & pip --version
Checking Python3 Version

If the above command returns the numeric version output of Python3 and PIP available on the system, you’re good to go. The system will display the error below if you do not have PIP installed.

Command 'pip' not found, but can be installed with:

You can run the below command to install Python3 and PIP depending on the Linux Distributions you’re using:

$sudo apt install python3
$sudo apt install python3-pip
$sudo yum –y install python3
$sudo yum –y install python3-pip

Step 2: Install Venv Module

Venv module is necessary to help create an isolated virtual environment to prevent conflict with other libraries. To install Venv, run:

sudo apt install python3-venv -y
Installing Venu Module

Step 3: Set Up the Virtual Environment

After installing the virtual environment, you can configure it to support ShellGPT commands easily.

Create a new directory and use the change directory command to navigate to the new directory as shown below:

sudo mkdir shellgpt-cli && cd shellgpt-cli
Creating New Directory

The next thing is to create a new virtual environment with the venv command, followed by an environment name as shown below:

python3 -m venv cli-shellgpt

The virtual environment is not enabled by default, so you must manually enable it with the below command:

source cli-shellgpt/bin/activate
Creating and Enabling a Virtual Environment

Step 4: Generate an OpenAPI Key

Now that the virtual environment is ready, to run ShellGPT, you must connect your Ubuntu machine to the OpenAI services. For this purpose, you must visit OpenAI’s website to create an account. You can simply log in if you have an account already.

After you have successfully created an account or logged in, click on  API Keys, followed by Create new secret key.

Generating Open AI API Key
Note: This key should not be shared with anyone because the connection is private and should only be utilized on your machine.

After generating the API key, copy it to a text editor somewhere as you will need it later.

To create a connection on your Ubuntu machine, create an environment variable with the export command as shown below:

export OPENAI_API_KEY=<paste your key details here>

Adding and Verifying the API Key in a Virtual Environment Variable

When you execute the API key in this manner, Linux will only utilize it once. However, you can save it in the .bashrc file, if you want to make the process permanent. To do this, run:

$sudo nano .bashrc
export OPENAI_API_KEY=<past key details here>
Creating a .bashrc file

When you are done, save and exit the editor and use the source command to enable the changes.

. ~/.bashrc

Alternatively, you can run:

source ./bashrc
Applying Changes

To verify the environment, run:

Verifying Environment

Your OPENAI_API_KEY environment variable should be displayed in the output as shown above.

Step 5: Installing ShellGPT

To utilize the function, you must install ShellGPT by running the below command:

$sudo pip3 install shell-gpt
Installing ShellGPT
Step 6: Useing ShellGPT for Queries

To run queries using SheelGPT directly on your Linux Terminal, run:

sgpt "<your-query>"
Running Queries

You can run as many queries as you want.

You can also ShellGPT to generate codes as shown below:

Running Queries to Generate Codes

In this post, you have learned everything you need to get started with setting up ShellGPT which is s based on the same concept as ChatGPT on your Linux machine.

Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x