...

How to Set Environment Variables Permanently in Windows, Linux, and Mac

In this article, I will show you how to set environment variables (permanently) in Windows, Linux, and Mac.

Firstly, why is this useful?

Often, we have to store sensitive information, like API keys, passwords, etc. so that they can be accessed by our software.

As an example, we might be using the OpenAI API, and to use it, we must have an API key, which ties all of your API requests to your account (so you can be billed accordingly for your usage).

The OpenAI API looks for the API key inside an environment variable called OPENAI_API_KEY.

One (bad) alternative is to set your API key in your code directly. However, this is considered bad practice because you are exposing your API key to the world, where it can be stolen and used by others (potentially costing you thousands of dollars).

Yes, even though these days you can have private Github repositories, you don’t want to take the chance. Just don’t put API keys or any other sensitive information in your code base!

The standard way to store such information is inside an environment variable.

 

What is an environment variable?

An environment variable is a dynamic, named value stored within the operating system that can affect the behavior of software processes and applications running on that system. These variables are part of the environment in which processes run, hence the name “environment variables.”

Environment variables contain information such as system paths, configuration settings, user preferences, and more. They serve as a means for software to access and utilize specific values without hardcoding them into the program itself. This makes environment variables highly flexible and adaptable, allowing users to customize their computing environment according to their needs.

Some common uses of environment variables include specifying system paths for executable files, defining default settings for applications, storing authentication credentials, and configuring runtime behavior for software frameworks.

In essence, environment variables provide a standardized way for software to interact with the operating system and access various system resources and settings. They play a crucial role in system configuration, software development, and overall system functionality.

 

How to Set Environment Variables on Windows

The easiest way to set an environment variable in Windows is to use the command line.

Go into the search bar, and type in “cmd” to search for the command line tool.

Let’s assume you want to set the variable OPENAI_API_KEY to the value "abc".

Once the command line program open, run the following command:

setx OPENAI_API_KEY "abc"

And that’s it!

This automatically sets the environment variable permanently.

 

How to Set Environment Variables on Linux and Mac

Generally, to set an environment variable on Linux and Mac, you would use the export command. An example is below:

export VARIABLE_NAME="variable_value"

 

However, note that this is not permanent!

If you want it to be permanent, you’ll have to modify the file ~/.bashrc or ~/.bash_profile (whichever exists on your system).

Use the vi or nano text editor to add the above line to the file.

For example:

vi ~/.bashrc

Once inside the text editor, use the appropriate commands to add the export line to the file.

But you’re still not done!

Once you’ve finished editing the file, you’ll have to run the commands contained in the file.

That is accomplished by using the source command.

For example:

source ~/.bashrc
source ~/.bashprofile