How to Use ChatGPT in Linux Terminal (2023)

Leaving the recognition of NFTs and the metaverse within the mud, AI has emerged as the brand new buzzword within the know-how world. ChatGPT and its alternate options are flourishing on the web, serving to customers get on a regular basis duties finished with ease. Earlier, we printed guides on how you can get began … The post How to Use ChatGPT in Linux Terminal (2023) appeared first on Ferdja.

May 3, 2023 - 11:00
How to Use ChatGPT in Linux Terminal (2023)

Leaving the recognition of NFTs and the metaverse within the mud, AI has emerged as the brand new buzzword within the know-how world. ChatGPT and its alternate options are flourishing on the web, serving to customers get on a regular basis duties finished with ease. Earlier, we printed guides on how you can get began with making your individual ChatGPT-like chatbot, utilizing Bing AI in any browser, and extra. However, what if you’re a Linux person and need to convey the wits of this AI chatbot to your system? Nicely, that is the place ShellGPT steps into the image. It brings the facility of ChatGPT proper to your Linux Terminal. ShellGPT supplies solutions, clever options, and writes instructions and code for you, serving to you navigate the command line effectively. On this article, we’ll present you how you can set up the ShellGPT command line instrument and use a ChatGPT-like AI bot within the Linux command line.

Run ChatGPT in Linux Terminal (2023)

What Is ShellGPT for Linux?

shellgpt linux
Picture Courtesy: Farkhod Sadykov (Github)

Developed by Farkhod Sadykov (together with two different contributors, specifically Eric and Loïc Coyle), ShellGPT is a command line model of ChatGPT that permits customers to interact with the AI chatbot of their Linux terminal. It’s based mostly on OpenAI’s GPT massive language mannequin (learn extra about OpenAI’s new GPT-4 mannequin proper right here).

ShellGPT can present clever options and suggestions and might even execute shell instructions based mostly in your textual content enter. It additionally learns out of your interactions and turns into extra correct over time. With this ChatGPT instrument baked into the command line, customers don’t should sort lengthy instructions or keep in mind difficult Linux Terminal instructions. They’ll save precious time whereas minimizing errors, getting ChatGPT to do a few of their menial work for them.

Conditions to Set up ChatGPT in Linux CLI

Listed below are a few of the stipulations it’s essential fulfill earlier than you’ll be able to set up ShellGPT in your Linux system. These are usually not too difficult instruments and could be put in with some easy instructions.

Set up Python

Python is an indispensable instrument, and like many different Linux instruments and libraries, ShellGPT (command line model of ChatGPT) can be constructed on Python. In truth, ChatGPT can be constructed utilizing Python together with different languages. Typically, Python comes preinstalled on most newest Linux distros. So first, verify the python model put in in your Linux PC with this command:

python3 --version

python installed version check

If the above command outputs any errors, it means python will not be put in or the deprecated python2.7 is put in in your system. Then, you need to use our information on how you can set up python in Linux.

Set up Pip Bundle Supervisor

Pip is a cross-platform bundle supervisor for Python. It helps handle numerous Python packages and libraries required to convey ChatGPT to Linux. You’ll be able to set up, improve, and uninstall the required packages utilizing a single command. It usually comes preinstalled with Python in most Linux distributions, but when it isn’t put in, you’ll be able to set up it with this command:

sudo apt-get -y set up python3-pip

installing pip

As soon as put in you’ll be able to verify the Pip model put in in Linux with the next command:

pip3 --version

check pip version installed

Set up Venv Module

This module will not be crucial for ShellGPT or ChatGPT, however we advocate you put in it to create an remoted digital surroundings in Linux to forestall any battle with different libraries. Putting in any library or bundle will set up many background dependencies that may intervene with different libraries. To create a digital surroundings for a challenge, you want the “venv” module, which could be put in utilizing the command beneath:

sudo apt set up python3-venv

installing venv module

Set Up ShellGPT to Use ChatGPT in Linux Terminal

We’ve divided the directions to put in ShellGPT in your Linux PC into 4 totally different sections to make it simpler so that you can perceive. Let’s begin with organising the surroundings, adopted by getting the OpenAI API key, and putting in the chatbot within the Terminal.

Arrange the Setting

Now that you’ve the dependencies lined, let’s create the surroundings to entry ChatGPT out of your Linux terminal with the next steps:

1. First, open the terminal from the app menu or use the “Ctrl + Alt + T” keyboard shortcut.

2. Now, use the command beneath to create a brand new listing and preserve all of the recordsdata organized.


For instance, we now have created a brand new listing with the identify “commandline-chatgpt” with the command beneath:

mkdir commandline-chatgpt

How to Set up and Use ChatGPT in Linux Terminal

3. Then, swap over to the brand new listing you simply created with the “change listing” command:


Within the instance beneath, we now have transfer to the commandline-chatgpt listing, the place we’ll create the surroundings to make use of ChatGPT within the Linux terminal:

cd commandline-chatgpt

switching over to new directory - chatgpt linux

4. Now, use the command beneath to create a digital surroundings with the venv module. We’ve used the “chatgpt_cli” identify for our digital surroundings.

python3 -m venv

For instance: python3 -m venv chatgpt_cli

creating new virtual environment for ChatGPT in Linux

5. The digital surroundings you simply created can be deactivated by default. To activate the surroundings, use this command:

supply /bin/activate

When you execute the above command, the shell immediate will now show the identify of the digital surroundings in brackets, like this:


As you’ll be able to see within the picture beneath, the default Linux shell immediate modified to (chatgpt_cli) remote2@remotehost upon operating the instructions above.

activating virtual environment to install ChatGPT in Linux

Get Your OpenAI API Key

With a purpose to use ChatGPT’s companies in Linux, you’ll need an OpenAI API key. At present, OpenAI is providing $5 credit for trial use. When you exhaust the credit, it’s essential pay for entry to the API. That mentioned, right here’s how one can get an OpenAI API key for this command line ChatGPT chatbot:

1. Navigate to OpenAI’s web site (visit) and create a brand new OpenAI account. If you have already got an account, merely log in and transfer to the following step.

2. Subsequent, click on in your profile picture on the high proper nook and choose “View API keys” from the drop-down menu.

viewing OpenAI api keys

3. Right here, you will note all of the beforehand generated API Keys if any. To generate a brand new API key, click on the “Create new secret key” button.

Generating new OpenAI API Keys

4. A brand new pop-up field will open together with your API key. Don’t share this API key with anybody or share it publicly. Copy your API key in a safe file or location, as you’ll be able to solely view it as soon as. You gained’t have the ability to copy the API key after you click on “OK” right here.

openAI API key - chatgpt linux

5. Now, create an surroundings variable for this API key with the command beneath. In Linux, you’ll be able to create an surroundings variable utilizing the “export” command. Exchange placeholder with the precise API key you generated to make use of ChatGPT within the Linux terminal.


creating environment variable for OpenAI API Key

6. Confirm the surroundings variable by itemizing it with the env command:


verify the environment variable for OpenAI API Key

7. This variable is just quickly saved for the present session. To retailer the API key completely, open the .bashrc file within the textual content editor of your alternative and add the variable on the finish of the file.


adding the OpenAI api key to the .bashrc file - chatgpt linux

8. Save the file and exit the textual content editor after you may have added the OpenAI API key. Now, run this command for the adjustments to take impact:

supply .bashrc

9. Lastly, confirm the adjustments with the env command:


checking for OpenAI API Key inside environment variables

Set up ShellGPT to Use ChatGPT

Having completed organising the surroundings, now you can proceed to put in the command line model of ChatGPT in Linux. It’s good to omit the –person flag if you’re putting in it in a digital surroundings. Now, use the command beneath to put in ShellGPT in your PC:

pip3 set up shell-gpt --user

installing ShellGPT using pip - chatgpt linux

ShellGPT: Syntax & Choices

Now that you’ve put in ShellGPT, you should be keen to make use of it for numerous duties. However earlier than that, let’s take a look at the syntax and a few choices that we will use to make our outputs attention-grabbing. Utilizing ShellGPT for a number of duties is straightforward, due to its easy syntax:


A few of the choices you need to use with the ShellGPT (sgpt) chatbot are:

Choice Description
--temperature Adjustments the randomness of the output
--top-probablity Limits to solely the best possible tokens or phrases
--chat Used to have a dialog with a singular identify
--shell Used to get shell instructions as output
--execute Executes the instructions obtained as output from --shell possibility
--code Used to get code as output

The best way to Use ChatGPT in Linux Terminal (Examples)

1. Use ShellGPT for Queries

You should use ShellGPT as a search engine for any form of question. Since it’s an AI chatbot, you get outcomes with extra human-like solutions and never a sequence of ranked internet pages like most engines like google. The syntax to make use of ShellGPT to get solutions to your questions is:

sgpt ""

For instance, if it’s essential know the mass of the solar, use this command:

sgpt "mass of solar"

You see a pleasant animation within the Terminal whereas ShellGPT tries to get a response. The response we obtained is highlighted beneath. You would possibly get an identical reply relying on how the mannequin is skilled:

The mass of the Solar is roughly 1.989 x 10^30 kilograms.

  • How to Set up and Use ChatGPT in Linux Terminal
  • How to Set up and Use ChatGPT in Linux Terminal

2. ChatGPT Chatbot Mode

In case you had used ChatGPT for chatting, it’s essential to have felt that it’s responses are at least a human. Now with the assistance of ShellGPT, you’ll be able to even use ChatGPT proper out of your Linux terminal. Merely use the --chat possibility adopted by a singular session identify and a immediate.

sgpt --chat

For example, if you wish to ask ChatGPT to inform you a joke, use this command:

sgpt --chat joke "are you able to inform me a joke?"

When you run the above command, you’ll get the next consequence as output:

Positive, here is one:

Why could not the bicycle rise up by itself?

As a result of it was two-tired!
Chatting with ChatGPT from Linux terminal

3. Generate Code

You’ll be able to even use the CLI-based ChatGPT instrument to unravel coding issues or generate code snippets. Merely use the --code flag to generate code to your immediate, as proven right here:

sgpt --code ""

For instance, if you wish to resolve the basic Fizz Buzz drawback utilizing python, use this immediate:

sgpt --code "Remedy basic fizz buzz drawback utilizing Python"

ChatGPT will rapidly offer you an output, which you’ll then paste into your code editor and try it out:

for i in vary(1, 101):
    if i % 3 == 0 and that i % 5 == 0:
    elif i % 3 == 0:
    elif i % 5 == 0:
generating code for fizz buzz problem with ChatGPT in Linux Terminal

4. Generate Shell Instructions

Whereas the Terminal is usually a highly effective instrument to execute complicated instructions and automate duties, it will possibly typically be tough for novice customers to recollect the syntax and choices of assorted Linux commands. With ChatGPT in your command line, you cannot solely get the syntax of a Linux command but additionally get the precise command with the parameters and choices required. Merely use the --shell flag as:

sgpt --shell ""

For instance, if it’s essential set the read-only permission for all recordsdata within the present listing, use the command:

sgpt --shell "Make all recordsdata within the present listing read-only"

Generating Linux shell command using ChatGPT

As anticipated, ChatGPT delivers and returns this because the output:

chmod -R a-w .

Moreover, when you use the --execute and the --shell flag collectively, you’ll be able to even execute the shell command generated instantly. As for the above instance, right here’s what the syntax seems to be like:

sgpt --shell --execute "make all recordsdata in present listing read-only"

executing the Linux shell command generated by ChatGPT

You’ll get the next output:

chmod -R a-w .
Execute shell command? [y/N]: y

Utilizing ChatGPT in Linux Command Line

So yeah, ShellGPT brings the facility of ChatGPT proper into your Linux terminal. It not solely makes working with the command line easy for each newbies and seasoned customers but additionally brings you further options. And as we talked about above, it turns into extra helpful over time as it’s designed to be taught from customers. However keep in mind to not share any delicate info or knowledge, particularly proprietary code out of your firm, with any sort of AI mannequin. That mentioned, do tell us what you consider this AI command line instrument within the feedback beneath.

The post How to Use ChatGPT in Linux Terminal (2023) appeared first on Ferdja.