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 options are flourishing on the web, serving to customers get on a regular basis duties achieved with ease. Earlier, we revealed guides on how to get began with making your individual ChatGPT-like chatbot, using Bing AI in any browser, and more. But, what if you’re a Linux consumer and wish to deliver the wits of this AI chatbot to your system? Well, that is the place ShellGPT steps into the image. It brings the facility of ChatGPT proper to your Linux Terminal. ShellGPT offers solutions, clever ideas, and writes instructions and code for you, serving to you navigate the command line effectively. In this text, we’ll present you the way to put in the ShellGPT command line software and use a ChatGPT-like AI bot within the Linux command line.
Run ChatGPT in Linux Terminal (2023)
What Is ShellGPT for Linux?

Developed by Farkhod Sadykov (together with two different contributors, specifically Eric and Loïc Coyle), ShellGPT is a command line model of ChatGPT that allows customers to interact with the AI chatbot of their Linux terminal. It relies on OpenAI’s GPT massive language mannequin (learn more about OpenAI’s new GPT-4 mannequin proper right here).
ShellGPT can present clever ideas and proposals and might even execute shell instructions based mostly in your textual content enter. It additionally learns out of your interactions and turns into more correct over time. With this ChatGPT software baked into the command line, customers don’t must kind lengthy instructions or keep in mind sophisticated Linux Terminal instructions. They can save priceless time whereas minimizing errors, getting ChatGPT to do a few of their menial work for them.
Prerequisites to Install ChatGPT in Linux CLI
Here are among the conditions it is advisable to fulfill earlier than you possibly can set up ShellGPT in your Linux system. These are usually not too sophisticated instruments and might be put in with some easy instructions.
Install Python
Python is an indispensable software, and like many different Linux instruments and libraries, ShellGPT (command line model of ChatGPT) can be constructed on Python. In reality, ChatGPT can be constructed using Python together with different languages. Generally, Python comes preinstalled on most latest Linux distros. So first, test the python model put in in your Linux PC with this command:
python3 --version

If the above command outputs any errors, it means python shouldn’t be put in or the deprecated python2.7 is put in in your system. Then, you need to use our information on how to put in python in Linux.
Install Pip Package Manager
Pip is a cross-platform bundle supervisor for Python. It helps handle numerous Python packages and libraries required to deliver ChatGPT to Linux. You can set up, improve, and uninstall the required packages using a single command. It usually comes preinstalled with Python in most Linux distributions, but when it’s not put in, you possibly can set up it with this command:
sudo apt-get -y set up python3-pip

Once put in you possibly can test the Pip model put in in Linux with the next command:
pip3 --version

Install Venv Module
This module shouldn’t be obligatory for ShellGPT or ChatGPT, however we suggest you put in it to create an remoted digital surroundings in Linux to forestall any battle with different libraries. Installing 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 might be put in using the command beneath:
sudo apt set up python3-venv

Set Up ShellGPT to Use ChatGPT in Linux Terminal
We have divided the directions to put in ShellGPT in your Linux PC into 4 completely different sections to make it simpler so that you can perceive. Let’s begin with establishing the surroundings, adopted by getting the OpenAI API key, and putting in the chatbot within the Terminal.
Set up the Environment
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 keep all of the information organized.
mkdir <your_directory_name>
For instance, we’ve created a brand new listing with the identify “commandline-chatgpt” with the command beneath:
mkdir commandline-chatgpt

3. Then, change over to the brand new listing you simply created with the “change directory” command:
cd <new_directory_name>
In the instance beneath, we’ve 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

4. Now, use the command beneath to create a digital surroundings with the venv module. We have used the “chatgpt_cli” identify for our digital surroundings.
python3 -m venv
<virtual_environment_name>
For instance: python3 -m venv chatgpt_cli

5. The digital surroundings you simply created can be deactivated by default. To activate the surroundings, use this command:
supply <virtual_environment_name>/bin/activate
Once you execute the above command, the shell immediate will now show the identify of the digital surroundings in brackets, like this:
(<virtual_environment_name>)<username>@<system_name>
As you possibly can see within the image beneath, the default Linux shell immediate modified to (chatgpt_cli) remote2@remotehost
upon operating the instructions above.

Get Your OpenAI API Key
In order to make use of ChatGPT’s providers in Linux, you will want an OpenAI API key. Currently, OpenAI is providing $5 credit for trial use. Once you exhaust the credit, it is advisable to 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 (go to) and create a brand new OpenAI account. If you have already got an account, merely log in and transfer to the following step.
2. Next, click on in your profile image on the prime proper nook and choose “View API keys” from the drop-down menu.

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

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

5. Now, create an surroundings variable for this API key with the command beneath. In Linux, you possibly can create an surroundings variable using the “export” command. Replace <your_OpenAI_API_key_here>
placeholder with the precise API key you generated to make use of ChatGPT within the Linux terminal.
export OPENAI_API_KEY=<your_OpenAI_API_key_here>

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

7. This variable is simply briefly saved for the present session. To retailer the API key completely, open the .bashrc file within the textual content editor of your selection and add the variable on the finish of the file.
export OPENAI_API_KEY=<your_OpenAI_API_key_here>

8. Save the file and exit the textual content editor after you’ve got added the OpenAI API key. Now, run this command for the adjustments to take impact:
supply .bashrc
9. Finally, confirm the adjustments with the env command:
env

Install ShellGPT to Use ChatGPT
Having completed establishing the surroundings, now you can proceed to put in the command line model of ChatGPT in Linux. You can use the command beneath to put in ShellGPT in your PC:
pip3 set up shell-gpt --user

ShellGPT: Syntax & Options
Now that you’ve put in ShellGPT, you should be keen to make use of it for numerous duties. But earlier than that, let’s try the syntax and a few choices that we will use to make our outputs attention-grabbing. Using ShellGPT for a number of duties is straightforward, because of its easy syntax:
sgpt <choices> <input_query>
Some of the choices you need to use with the ShellGPT (sgpt) chatbot are:
Option | Description |
---|---|
--temperature |
Changes the randomness of the output |
--top-probablity |
Limits to solely the very 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 choice |
--code |
Used to get code as output |
How to Use ChatGPT in Linux Terminal (Examples)
1. Use ShellGPT for Queries
You can use ShellGPT as a search engine for any type of question. Since it’s an AI chatbot, you get outcomes with more 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 "<your_query>"
For instance, if it is advisable to know the mass of the solar, use this command:
sgpt "mass of sun"
You see a pleasant animation within the Terminal whereas ShellGPT tries to get a response. The response we obtained is highlighted beneath. You may get the same reply relying on how the mannequin is educated:
The mass of the Sun is roughly 1.989 x 10^30 kilograms.
2. ChatGPT Chatbot Mode
If you had used ChatGPT for chatting, you need to have felt that it’s responses are a minimum of a human. Now with the assistance of ShellGPT, you possibly can even use ChatGPT proper out of your Linux terminal. Simply use the --chat
choice adopted by a singular session identify and a immediate.
sgpt --chat <unique_chat_session_name> <input_prompt>
For instance, if you wish to ask ChatGPT to let you know a joke, use this command:
sgpt --chat joke "can you tell me a joke?"
Once you run the above command, you’ll get the next outcome as output:
Sure, this is one:
Why could not the bicycle rise up by itself?
Because it was two-tired!

3. Generate Code
You may even use the CLI-based ChatGPT software to resolve coding issues or generate code snippets. Simply use the --code
flag to generate code on your immediate, as proven right here:
sgpt --code "<input_prompt>"
For instance, if you wish to resolve the traditional Fizz Buzz downside using python, use this immediate:
sgpt --code "Solve classic fizz buzz problem using 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:
print("FizzBuzz")
elif i % 3 == 0:
print("Fizz")
elif i % 5 == 0:
print("Buzz")
else:
print(i)

4. Generate Shell Commands
While the Terminal could be a highly effective software to execute advanced instructions and automate duties, it might probably typically be tough for novice customers to recollect the syntax and choices of assorted Linux instructions. With ChatGPT in your command line, you cannot solely get the syntax of a Linux command but in addition get the precise command with the parameters and choices required. Simply use the --shell
flag as:
sgpt --shell "<input_prompt>"
For instance, if it is advisable to set the read-only permission for all information within the present listing, use the command:
sgpt --shell "Make all files in the current directory read-only"

As anticipated, ChatGPT delivers and returns this because the output:
chmod -R a-w .
Furthermore, if you happen to use the --execute
and the --shell
flag collectively, you possibly can even execute the shell command generated immediately. As for the above instance, right here’s what the syntax appears to be like like:
sgpt --shell --execute "make all files in current directory read-only"

You will get the next output:
chmod -R a-w .
Execute shell command? [y/N]: y
Using 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 in addition brings you extra options. And as we talked about above, it turns into more helpful over time as it’s designed to be taught from customers. But keep in mind to not share any delicate data or knowledge, particularly proprietary code out of your firm, with any form of AI mannequin. That mentioned, do tell us what you consider this AI command line software within the feedback beneath.
Check out more article on – How-To tutorial and latest highlights on – Technical News