Ultimate Resource for AI, Software, Hosting & Marketing Solutions

Using ChatGPT in Linux Terminal – Beginner’s Guide Tutoriel

How to Set up and Use ChatGPT in Linux Terminal

AI has replaced NFTs and the metaverse as the newest technical buzzwords, overtaking both of them in popularity. Online, ChatGPT and its variants are thriving, assisting users in carrying out routine chores with ease. In the past, we have written tutorials on how to create your own ChatGPT-like chatbot, use Bing AI in any browser, and more. What if, however, you run Linux and wish to install an AI chatbot on your system? So, ShellGPT comes into play in this situation. It directly integrates ChatGPT’s capabilities into your Linux Terminal. ShellGPT assists you in effectively navigating the command line by providing solutions, insightful ideas, and writing commands and code for you. In this post, we’ll demonstrate how to set up the ShellGPT command-line tool and utilize a Linux command-line AI bot that resembles ChatGPT.

What Is ShellGPT for Linux?

ShellGPT is a command line variant of ChatGPT that enables users to communicate with the AI chatbot in their Linux terminal. It was created by Farkhod Sadykov (together with two more developers, notably Eric and Loc Coyle). It is based on the OpenAI GPT big language model (learn more about the latest GPT-4 model here).

Based on the text you provide, ShellGPT may make intelligent recommendations, perform shell commands, and more. Moreover, it gains knowledge from your interactions and improves in accuracy over time. Users no longer need to input lengthy instructions or memorize difficult Linux Terminal commands thanks to the ChatGPT application included into the command line. By having ChatGPT do part of their tedious labor on their behalf, they may reduce mistakes while saving critical time.

Conditions for Installing ChatGPT in the Linux CLI

The following requirements must be met before you may install ShellGPT on your Linux system. These are straightforward utilities that may be installed using a few straightforward instructions.

Set up Python

Python is a crucial tool, and ShellGPT (ChatGPT’s command line counterpart) is built on Python much like many other Linux tools and libraries. In reality, Python is one of the languages used in ChatGPT’s development along with others. Python is often preinstalled on the majority of recent Linux distributions. Hence, use this program to first determine which version of Python is installed on your Linux computer:

python3 --version

If the above command outputs any errors, it means python is not installed or the deprecated python2.7 is installed on your system. Then, you can use our guide on how to install python in Linux.

Install Pip Package Manager

Python’s cross-platform package manager is called Pip. It assists in managing the many Python packages and libraries necessary to make ChatGPT available on Linux. The necessary packages may be installed, upgraded, and uninstalled with a single command. In most Linux versions, it is typically preloaded with Python, but if it is not, you may install it using the following command:

sudo apt-get -y install python3-pip

Once installed you can check the Pip version installed in Linux with the following command:

pip3 --version

Install Venv Module

Although it is not required for ShellGPT or ChatGPT, we advise installing this module to set up a virtual environment that is separated under Linux to avoid any potential conflicts with other libraries. Each library or package installation includes a number of background dependencies that may conflict with other libraries. The “venv” module is required to build a virtual environment for a project and may be installed with the following command:

sudo apt install python3-venv

Set Up ShellGPT to Use ChatGPT in Linux Terminal

To make it simpler for you to comprehend, we’ve broken up the directions for installing ShellGPT on your Linux computer into four distinct sections. Setting up the environment first, then obtaining the OpenAI API key, and then installing the chatbot in the Terminal are the next steps.

Set up the Environment

Now that you have the dependencies covered, let’s create the environment to access ChatGPT from your Linux terminal with the following steps:

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

2. Now, use the command below to create a new directory and keep all the files organized.

mkdir <your_directory_name>

For example, we have created a new directory with the name “commandline-chatgpt” with the command below:

mkdir commandline-chatgpt

3. Then, switch over to the new directory you just created with the “change directory” command:

cd <new_directory_name>

In the example below, we have move to the commandline-chatgpt directory, where we will create the environment to use ChatGPT in the Linux terminal:

cd commandline-chatgpt

4. Now, use the command below to create a virtual environment with the venv module. We have used the “chatgpt_cli” name for our virtual environment.

python3 -m venv <virtual_environment_name>

For example: python3 -m venv chatgpt_cli

5. The virtual environment you just created will be deactivated by default. To activate the environment, use this command:

source <virtual_environment_name>/bin/activate

Once you execute the above command, the shell prompt will now display the name of the virtual environment in brackets, like this:

(<virtual_environment_name>)<username>@<system_name>

As you can see in the image below, the default Linux shell prompt changed to (chatgpt_cli) remote2@remotehost upon running the commands above.

Get Your OpenAI API Key

You need an OpenAI API key to access ChatGPT’s services on Linux. OpenAI is now giving away $5 in trial credits. After using up all of your credits, you must pay to use the API. In light of the foregoing, this is how to obtain an OpenAI API key for this ChatGPT command line chatbot:

1. Navigate to OpenAI’s website (visit) and create a new OpenAI account. If you already have an account, simply log in and move to the next step.

2. Next, click on your profile image at the top right corner and select “View API keys” from the drop-down menu.

view_api_key
view_api_key

3. Here, you will see all the previously generated API Keys if any. To generate a new API key, click the “Create new secret key” button.

4. A new pop-up box will open with your API key. Do not share this API key with anyone or share it publicly. Copy your API key in a secure file or location, as you can only view it once. You won’t be able to copy the API key after you click “OK” here.

5. Now, create an environment variable for this API key with the command below. In Linux, you can create an environment variable using the “export” command. Replace <your_OpenAI_API_key_here> placeholder with the actual API key you generated to use ChatGPT in the Linux terminal.

export OPENAI_API_KEY=<your_OpenAI_API_key_here>

6. Verify the environment variable by listing it with the env command:

env

7. This variable is only temporarily stored for the current session. To store the API key permanently, open the .bashrc file in the text editor of your choice and add the variable at the end of the file.

export OPENAI_API_KEY=<your_OpenAI_API_key_here>

8. Save the file and exit the text editor after you have added the OpenAI API key. Now, run this command for the changes to take effect:

source .bashrc

9. Finally, verify the changes with the env command:

env

Install ShellGPT to Use ChatGPT

After completing the environment setup, you can now install ChatGPT for Linux using the command line. If you’re setting up the installation in a virtual environment, you must remove the -user parameter. Use the following command to now install ShellGPT on your computer:

pip3 install shell-gpt --user

ShellGPT: Syntax & Options

Now that you have installed ShellGPT, you must be eager to use it for various tasks. But before that, let’s check out the syntax and some options that we can use to make our outputs interesting. Using ShellGPT for multiple tasks is easy, thanks to its straightforward syntax:

sgpt <options> <input_query>

Some of the options you can use with the ShellGPT (sgpt) chatbot are:

OptionDescription
--temperatureChanges the randomness of the output
--top-probablityLimits to only the highest probable tokens or words
--chatUsed to have a conversation with a unique name
--shellUsed to get shell commands as output
--executeExecutes the commands received as output from --shell option
--codeUsed to get code as output

Using ChatGPT in the Linux Terminal (Examples)

1. Use ShellGPT for Queries

You can use ShellGPT as a search engine for any sort of query. Since it is an AI chatbot, you get results with more human-like answers and not a series of ranked web pages like most search engines. The syntax to use ShellGPT to get answers to your questions is:

sgpt "<your_query>"

For example, if you need to know the mass of the sun, use this command:

sgpt "mass of sun"

You see a nice animation in the Terminal while ShellGPT tries to get a response. The response we received is highlighted underneath. You might get a similar reply depending on how the model is trained:

The mass of the Sun is approximately 1.989 x 10^30 kilograms.

2. ChatGPT Chatbot Mode

If you had used ChatGPT for chatting, you must have felt that it is responses are no less than a human. Now with the help of ShellGPT, you can even use ChatGPT right from your Linux terminal. Simply use the --chat option followed by a unique session name and a prompt.

sgpt --chat <unique_chat_session_name> <input_prompt>

For instance, if you want to ask ChatGPT to tell you a joke, use this command:

sgpt --chat joke "can you tell me a joke?"

Once you run the above command, you will get the following result as output:

Sure, here's one:

Why couldn't the bicycle stand up by itself?

Because it was two-tired!

3. Generate Code

You can even use the CLI-based ChatGPT tool to solve coding problems or generate code snippets. Simply use the --code flag to generate code for your prompt, as shown here:

sgpt --code "<input_prompt>"

For example, if you want to solve the classic Fizz Buzz problem using python, use this prompt:

sgpt --code "Solve classic fizz buzz problem using Python"

ChatGPT will quickly give you an output, which you can then paste into your code editor and test it out:

for i in range(1, 101):
    if i % 3 == 0 and 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 can be a powerful tool to execute complex commands and automate tasks, it can sometimes be difficult for novice users to remember the syntax and options of various Linux commands. With ChatGPT in your command line, you can not only get the syntax of a Linux command but also get the exact command with the parameters and options required. Simply use the --shell flag as:

sgpt --shell "<input_prompt>"

For example, if you need to set the read-only permission for all files in the current directory, use the command:

sgpt --shell "Make all files in the current directory read-only"

As expected, ChatGPT delivers and returns this as the output:

chmod -R a-w .

Furthermore, if you use the --execute and the --shell flag together, you can even execute the shell command generated directly. As for the above example, here’s what the syntax looks like:

sgpt --shell --execute "make all files in current directory read-only"

You will get the following output:

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

Using the Linux Command Line with ChatGPT

Therefore, ShellGPT effectively integrates ChatGPT capability into your Linux terminal. It provides you more capabilities in addition to making using the command line simple for both novice and experienced users. Also, as we already indicated, because it is made to learn from users, its usefulness increases with time. But, keep in mind that no AI model should receive any sensitive information or data, particularly confidential corporate code. Having said that, please share your thoughts on this command-line AI tool in the comments section below.

Exit mobile version