Guides

ShellGPT for Linux: Here is how to set up and use ChatGPT on Linux

ShellGPT is a powerful tool that brings the intelligence of ChatGPT to the Linux command line. With its ability to provide answers, intelligent suggestions, and even write commands and code, ShellGPT is an invaluable tool for Linux users who want to navigate the command line with ease. In this article, we’ll walk you through the steps to install ShellGPT on your Linux system and show you how to use a ChatGPT-like AI bot in the command line. Whether you’re a seasoned Linux user or just getting started, ShellGPT can help streamline your workflow and make working in the command line a breeze.

AI has become the latest buzzword in the technology world, overshadowing the popularity of NFTs and the metaverse. ChatGPT and similar AI tools have gained immense popularity on the internet, assisting users in completing daily tasks effortlessly.

If you are a Linux user and wish to integrate the wit of this AI chatbot into your system, you can do so with the help of ShellGPT. This tool enables ChatGPT’s capabilities directly on your Linux Terminal, providing answers, generating intelligent suggestions, and even writing code and commands to enhance your efficiency while navigating the command line. In this article, we will explain the process of installing the ShellGPT command line tool and utilizing a ChatGPT-like AI bot in the Linux command line.

What Is ShellGPT for Linux?

ShellGPT is a free, open-source command line tool for Linux that integrates ChatGPT’s AI capabilities to generate shell commands, code snippets, and comments, and engage in AI chat using the terminal. Its development is community-driven, and the project’s source code is available on GitHub. The tool was created to assist users who prefer using the command line and want to save time by avoiding Google searches for solutions to problems. ShellGPT can perform tasks effortlessly with a brief description of the desired Shell commands or scripts.

ShellGPT is highly flexible and customizable, with users able to fine-tune its behaviour and output to suit their specific requirements. One unique feature of the tool is its ability to comprehend natural language input and offer context-aware Shell command suggestions.

To provide an example, ShellGPT can give you the default Nginx path on your Ubuntu or other Linux distributions. Additionally, if you need to create a Docker container running with an Apache web server and with 80 or 443 ports of container mapped to host ports, ShellGPT can handle it.

ShellGPT uses the GPT-3.5 language model, which is renowned for its natural language understanding and context-awareness capabilities. This tool is an excellent solution for Linux users seeking to streamline their workflow and improve efficiency. In this article, we will discuss how to install and utilize ShellGPT on our Linux systems.

How to set up and use ChatGPT on Linux

Before you can install ChatGPT on your Linux system via the command line interface, there are a few prerequisites that need to be met. These prerequisites are simple tools that can be easily installed using basic commands.

One of the prerequisites is to install Python, which is an essential tool used to build many Linux tools and libraries, including ShellGPT (the command-line version of ChatGPT). Python is also one of the languages used to build ChatGPT. Most of the latest Linux distributions come with Python preinstalled. Therefore, before proceeding with the installation process, you should check the version of Python installed on your Linux PC by using the following command:

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. You will find a guide here on how to install Python on Linux. https://opensource.com/article/20/4/install-python-linux

Another prerequisite to installing ChatGPT on your Linux system is to have Pip installed. Pip is a package manager for Python that allows for easy management of various Python packages and libraries required to bring ChatGPT to Linux. With Pip, you can install, upgrade, and uninstall the necessary packages using a single command. Most Linux distributions come with Pip preinstalled with Python. However, if you do not have Pip installed, you can easily install it by running 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

Although not a mandatory requirement, it is recommended to install the “venv” module before installing ShellGPT or ChatGPT on your Linux system. This module allows you to create an isolated virtual environment, which can prevent conflicts with other libraries that may already be installed on your system. When you install any library or package, it can install several background dependencies that may interfere with other libraries. Therefore, to create a virtual environment for your project, you need the “venv” module, which can be installed on Linux using the following command:

sudo apt install python3-venv

Setting Up ShellGPT to Use ChatGPT in Linux Terminal

OpenAI API

To utilize ChatGPT’s services in Linux, you’ll require an OpenAI API key. OpenAI currently offers a $5 credit for trial usage, after which you’ll need to purchase access to the API. Here’s how you can obtain an OpenAI API key for the ChatGPT command-line chatbot:

  1. Visit the OpenAI website and create a new account if you don’t already have one. If you already have an account, simply log in and proceed to the next step.
  2. Click on your profile image in the top right corner and choose “View API keys” from the drop-down menu.

Environment set up

With all the necessary dependencies in place, you can now proceed to create the environment required to access ChatGPT from your Linux terminal. Here are the steps to create the environment for accessing ChatGPT from your Linux terminal:

  1. Open the terminal by either selecting it from the app menu or using the keyboard shortcut “Ctrl + Alt + T”.
  2. Next, use the following command to create a new directory and keep all the files organized:
mkdir <your_directory_name>

Remember to change the <your_directory_name> to that of your designed directory name.

Next, You will have to 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 command line-chatgpt directory, where we will create the environment to use ChatGPT in the Linux terminal:

cd commandline-chatgpt

To create a virtual environment using the venv module, use the following command. In this example, we have named the virtual environment “chatgpt_cli”:

python3 -m venv <virtual_environment_name>
For example: python3 -m venv chatgpt_cli

By default, the virtual environment that you have just created will be deactivated. To activate the environment, use the following command:

source <virtual_environment_name>/bin/activate

After running the command mentioned above, the shell prompt will show the virtual environment’s name in brackets, in the following format:

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

After executing the aforementioned commands, the image below indicates that the default Linux shell prompt was modified to display “(chatgpt_cli) remote2@remotehost”.

Adding your OpenAI API

Next, you need to create an environment variable in Linux for the API key you obtained earlier using the following command, replacing the placeholder <your_OpenAI_API_key_here> with the actual API key:

export OPENAI_API_KEY=<your_OpenAI_API_key_here>

Next, Verify the environment variable by listing it with the env command:

Please note that the environment variable created earlier is only stored temporarily for the current session. To store the API key permanently, you can open the “.bashrc” file in any text editor and add the variable at the end of the file.

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

Finally, verify the changes with the env command:

env

Installing the ShellGPT to Use ChatGPT

With the environment now set up, you can proceed to install the command line version of ChatGPT in Linux. However, if you’re installing it within a virtual environment, you should omit the “–user” flag. Use the command below to install ShellGPT on your PC:

pip3 install shell-gpt --user

After installing ShellGPT, you may be excited to utilize it for different tasks. However, before proceeding, let’s review the syntax and available options to enhance our outputs. The syntax of ShellGPT is simple, making it effortless to use for multiple tasks.

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

With the CLI-based ChatGPT tool, you can also utilize it to resolve coding issues or generate code snippets. Just add the “–code” flag to produce code for your prompt, as illustrated below:

sgpt --code "<input_prompt>"

Conclusion

ShellGPT allows you to access the capabilities of ChatGPT within your Linux terminal. This tool not only simplifies command line usage for beginners and experienced users but also provides added functionalities. It continuously learns from users and becomes increasingly beneficial over time. However, it’s important to avoid sharing confidential information or proprietary code from your company with any AI model.

Back to top button

Adblock Detected!

Hello, we detected you are using an Adblocker to access this website. We do display some Ads to make the revenue required to keep this site running. please disable your Adblock to continue.