How to use ChatGPT from Ubuntu Terminal using ShellGPT

Artificial intelligence has emerged as a phenomenon in the new age. With new features added daily, there's a lot to look forward to.

People use this universal, versatile tool to tell jokes, write code, and even answer the most random questions. It's easy to use ChatGPT on other operating systems, so as a Linux user, are you willing to be left behind?

You can also enjoy a version of ChatGPT for Linux, ShellGPT. Here's how to install and use this AI tool from Ubuntu Terminal with a few simple steps.

Step 1: Install Python and PIP on the machine

Like most AI-powered tools, even ShellGPT runs on Python. Although Python is usually installed by default on most Linux distributions, you can check its installation through the version information. If Python is not available on your machine, you must install it before moving on to the next steps.

Open a Terminal and enter the following commands to check the version of Python:

python3 --version

If the command returns the version number output, you can rest assured that everything will be ready to go. However, if you encounter any errors, you should install Python on Ubuntu, before installing PIP.

Now that you have Python installed on your machine, it's time to install PIP, Python's native package manager.

Although PIP is usually pre-installed with the Python package, it is best to check the version first for its installation status. Run the following command to check if it is installed on your machine:

 

pip --version

If you get an error after executing, you need to install the package manager:

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

You can use the following command to install it:

sudo apt install python3-pip

How to use ChatGPT from Ubuntu Terminal using ShellGPT Picture 1How to use ChatGPT from Ubuntu Terminal using ShellGPT Picture 1

After installation, you can use the --version command again to check if the installation was successful.

Step 2: Install and set up a virtual environment using Python

Now that Python and PIP are ready, you can set up Python's virtual environment to make it convenient to install and run ShellGPT. Virtual environments are ideal for running isolated programs as they can avoid library conflicts.

With the included virtual environment, you can limit the interaction between the system's programs and the virtual environment while performing various executables in the silo.

Note : Using virtual environment is an optional step, to avoid any unforeseen risks while installing and using Python libraries.

 

To create a virtual environment using Python, you need to install the venv module:

sudo apt install python3-venv -y

How to use ChatGPT from Ubuntu Terminal using ShellGPT Picture 2How to use ChatGPT from Ubuntu Terminal using ShellGPT Picture 2

Set up virtual environment

After installing the virtual environment, you can set up the virtual environment so that it can seamlessly support ShellGPT commands.

First, create a new folder to organize and store files. You can use the mkdir command, followed by the directory name, like this:

mkdir cli-shellgpt

Navigate to this newly created directory with the cd command:

cd cli-shellgpt

Then, create a new virtual environment with the venv command, followed by the environment name:

python3 -m venv cli-shellgpt

Since the virtual environment is not enabled by default, you must enable it manually with the activate script :

source cli-shellgpt/bin/activate

As soon as you execute the above command, you will notice the default Linux shell prompt of Linux changes, as shown below:

(cli-shellgpt) sahil@vm:

How to use ChatGPT from Ubuntu Terminal using ShellGPT Picture 3How to use ChatGPT from Ubuntu Terminal using ShellGPT Picture 3

 

Step 3: Generate OpenAPI key

Since the virtual environment is ready, you must connect your OpenAI services and Ubuntu machine to run ShellGPT. For this to work, you must navigate to the OpenAI website and create an account there.

If you already have an account, you can log in with your credentials and navigate to your profile picture, located at the top right of the website.

Click View API Keys , then click Create new secret key .

How to use ChatGPT from Ubuntu Terminal using ShellGPT Picture 4How to use ChatGPT from Ubuntu Terminal using ShellGPT Picture 4

Do not share this key with anyone because the connection is private and only used on your machine.

Copy the API key from the dialog and save it somewhere because you won't be able to see the same key again.

How to use ChatGPT from Ubuntu Terminal using ShellGPT Picture 5How to use ChatGPT from Ubuntu Terminal using ShellGPT Picture 5

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

export OPENAI_API_KEY= 

Add and verify the API key to the virtual environment variable

When you execute the API key this way, Linux will only use it for a single instance. However, if you want the executable to be permanent, save it to the .bashrc file.

All you have to do is enter the first command and enter the next in a text editor:

 

nano ./bashrc export OPENAI_API_KEY= 

How to use ChatGPT from Ubuntu Terminal using ShellGPT Picture 6How to use ChatGPT from Ubuntu Terminal using ShellGPT Picture 6

Save and exit the editor. Then use the source command to trigger the changes.

source ./bashrc

Finally, verify the API key with the env command.

env

How to use ChatGPT from Ubuntu Terminal using ShellGPT Picture 7How to use ChatGPT from Ubuntu Terminal using ShellGPT Picture 7

Your OPENAI_API_KEY environment variable should be listed in the output.

Step 5: Install ShellGPT on Ubuntu

Once all the installation procedures are complete, you can simply move on to the best part, installing ShellGPT. The installation steps are quite simple and you can install the tool with the following command:

pip3 install shell-gpt

How to use ChatGPT from Ubuntu Terminal using ShellGPT Picture 8How to use ChatGPT from Ubuntu Terminal using ShellGPT Picture 8

Let the installation finish; meanwhile, you can see some cool ways to use AI tools through your Terminal window.

Use ShellGPT to run queries via Terminal

The whole purpose of installing ShellGPT is to make your life easier. To use Terminal as a search engine and run some queries, you can use the sgpt command, followed by your query in quotes:

 

sgpt "How many galaxies exist within the universe"

How to use ChatGPT from Ubuntu Terminal using ShellGPT Picture 9How to use ChatGPT from Ubuntu Terminal using ShellGPT Picture 9

Use ShellGPT to generate code

You can also use ShellGPT to generate code. You can do that by passing the correct command with the sgpt command :

sgpt --code "print the Fibonacci series"

How to use ChatGPT from Ubuntu Terminal using ShellGPT Picture 10How to use ChatGPT from Ubuntu Terminal using ShellGPT Picture 10

The output includes code that you can execute in Python to generate the Fibonacci sequence.

4 ★ | 1 Vote