How to Run ChatGPT in Terminal on Linux

How to Run ChatGPT in Terminal on Linux

Did you always want to know how to run ChatGPT in Terminal on Linux? You’ll be surprised to know that you actually can. Let’s show you how.

ChatGPT has started a revolution in the AI world. So many people are trying to push it to its limit by using innovative, weird, and whacky prompts. Linux enthusiasts, on the other hand, are trying to find ways to run this application from their most familiar environment—the terminal.

In this guide, we will show you different ways to run ChatGPT directly from the Linux terminal

Let’s get started!

How to Run ChatGPT in Terminal on Linux Using ChatGPT Wrapper

ChatGPT Wrapper is a tool made in Python for interacting with ChatGPT. It’s open-source, and you can find the code on GitHub.

Before you can install this unofficial Python API, you need to have setup tools on your Linux device. To install it, you need to ensure you can run both Python and pip from your terminal. In other words, you need to have them as well.

Generally, Python comes installed by default in Linux systems (pip, too, if you’re using Python 3.4 or later). But to keep things comprehensive, we’ll show you how to install both.

To install Python on Linux, run this command:

$ sudo apt-get update
$ sudo apt-get install python3.11

Then install pip using this command:

$ sudo apt install python3-pip

When both are installed, it’s time to install setuptools. For that, use this command:

$ pip install setuptools
install setuptools on Linux terminal

As you can see, we already had it installed. Next, you have to install the Wrapper from Github. We will use git for that. In case you don’t have git, install it by running this command:

$ sudo apt-get install git-all

We need some more Python modules before we can install the wrapper—playwright, rich, and pyreadline3.

Install playwright using this command:

$ pip install pytest-playwright
install playwright on Linux terminal

Install rich using the below command:

$ python3 -m pip install rich
install rich on Linux terminal

Now you have to install Firefox using playwright. Do so by issuing this command:

$ python3 -m playwright install firefox
install playwright firefox on Linux terminal

After that, install pyreadline3 with this command:

$ pip install pyreadline3
install pyreadline3 on Linux terminal

Once all of that is done, issue this command to install ChatGPT Wrapper:

$ pip install git+https://github.com/mmabrouk/chatgpt-wrapper
install chatgpt wrapper on Linux terminal

Lastly, set the program in install mode with this command:

$ chatgpt install
install chatgpt on Linux terminal

A browser window will pop up, asking you to log in. Login using the same credentials you used to open an OpenAI account. And if you don’t have an account, create one for free.

After signing in, close the window. Close the Terminal and restart it. Now run this command:

$ chatgpt

You should get the below message:

$ chatgpt
Provide a prompt for ChatGPT, or type !help or ? to list commands.
1>
How to Run ChatGPT in Terminal on Linux

As the screenshot says, type either command to see the list of built-in commands in the app.

How to Run ChatGPT in Terminal on Linux

To get an instant answer, follow up the chatgpt command with a question, like this:

$ chatgpt What is two plus two?
How to Run ChatGPT in Terminal on Linux

Useful Commands For the ChatGPT Wrapper

Other than having a usual conversation, you could use any of these built-in commands:

!new: For starting a new conversation.

!nav: For navigating to a past point in the conversation.

!log: Allow logging to a file.

!context: For loading old contexts from the log. It takes one parameter; a context string from logs.

!stream: To toggle between streaming mode (streams the raw response from ChatGPT) and markdown rendering (which cannot stream).

!read: For reading multi-line input.

!file: For reading the content of a file.

!session: To refresh your session information. This can resolve errors under certain scenarios.

How to Turn ChatGPT Into a Linux Terminal?

Technically, it doesn’t really become a Terminal. But you can simulate the Linux terminal environment from within ChatGPT. To do that, you have to enter the “magic prompt”. Okay, not a magic prompt per se. Though this carefully worded prompt is essential. Otherwise, ChatGPT may refuse your request. So here is the prompt:

“I want you to act as a Linux terminal. I will type commands and you will reply with what the terminal should show. I want you to only reply with the terminal output inside one unique code block, and nothing else. Do not write explanations. Do not type commands unless I instruct you to do so. When I need to tell you something in English I will do so by putting text inside curly brackets {like this}. My first command is pwd.”
prompt for chatgpt to become Linux terminal

Awesome, right? You can start using ChatGPT as if it’s a Command Line Interface (CLI). Use different types of Linux commands and experiment with them. For example, here are some common Linux commands you can try out:

ls – prints the available directories and files in the current directory.

ls command

cd – changes the current directory to the one specified. As expected, you won’t get any output from it. Though ChatGPT mentions this explicitly, which it shouldn’t have done

cd command

touch – create new files

touch command

mkdir – create a new directory

mkdir command

echo and catecho can be used for writing inside a text file while cat is used for reading the content

echo command

whoami – Output the user name 

whoami command

So how is this useful? 

As we said, you’re not using a real Linux system. ChatGPT is only acting like one. But it does give you a real feeling of using a Linux Terminal. So if you’ve always been interested in using the Command Line but weren’t sure about leaping to Linux, this is a great way to get a taste of it.

If you’re still new to Linux, you can use ChatGPT to brush up your skills before you move to the real thing. You can also use it to test out dangerous Linux commands you shouldn’t use in a real device, such as these:

asking chatgpt to run malicious Linux code

So next time someone on the internet asks you to put a random Linux command in your Terminal, try it out on ChatGPT first.

Final Thoughts

This guide shows you how to run ChatGPT from the comfort of your Linux terminal. Of course, the project still has a long way to go. But at least, it’s a brilliant start. Having the flexibility to use this Python API to use ChatGPT in your projects sounds like a fantastic idea.

So, did you face any problems running ChatGPT on your Linux terminal? Let us know in the comments below.

If this guide helped you, please share it.

Related Posts