Introduction

If you work in software development, system administration, or even basic IT troubleshooting, you’ve likely come across the term environment variables. These small but powerful system settings control how your computer runs applications, locates files, and manages configurations.

Set environment variables in Windows and Mac step-by-step guide illustration

Whether you’re setting up a development environment, configuring Java, Python, Node.js, or managing secure API keys, knowing how to properly set environment variables is essential.

In this complete guide, you’ll learn how to set environment variables on both Windows and macOS systems—step by step—with clear instructions, real-world examples, and troubleshooting tips.


Quick Answer

To set environment variables:

On Windows:

  1. Open System PropertiesAdvancedEnvironment Variables
  2. Click New or Edit
  3. Add variable name and value
  4. Save and restart apps

On macOS:

  1. Open Terminal
  2. Edit shell config file (.zshrc or .bash_profile)
  3. Add: export VARIABLE_NAME=value
  4. Save and run source command

Table of Contents

  • What Are Environment Variables?
  • Why Environment Variables Matter
  • Types of Environment Variables
  • How to Set Environment Variables in Windows
  • How to Set Environment Variables in macOS
  • Temporary vs Permanent Variables
  • Common Errors and Fixes
  • Best Practices / Pro Tips
  • Conclusion
  • FAQs

What Are Environment Variables?

Environment variables are key-value pairs used by operating systems to store configuration settings. They help applications understand:

  • Where to find system files
  • Which paths to use
  • What configurations to apply

For example:

  • PATH tells your system where to find executable files
  • JAVA_HOME points to the Java installation directory
  • NODE_ENV defines environment mode (development or production)

Why Environment Variables Matter

Environment variables are critical for:

  • Running development tools (Node.js, Python, Java)
  • Managing sensitive data like API keys
  • Configuring system-wide settings
  • Avoiding hardcoding values in applications

For IT professionals, they are essential for automation, deployment, and server management.


Types of Environment Variables

1. System Variables

  • Apply to all users on the computer
  • Require admin privileges

2. User Variables

  • Apply only to the current user
  • Safer for personal configurations

How to Set Environment Variables in Windows

https://images.openai.com/static-rsc-4/5Rz1YBmGuMIlhKE-nKbHBQ-T3A-gTrYKZIqhrVcXn4nQc8LK6FbRpw6wIlE_2jI_BlqUNWULB2AxPbDQEFLngwJZ6GnjejtblSGz3renAybLWfESd7Yxzcx6wx6-0A5toi7rGwVo5Wqz8cRRqwvEqhtZhLvhaKzypjc0q3j9-O1DkFbZQvwoMls3L0Kr4QMT?purpose=fullsize
https://images.openai.com/static-rsc-4/5Rz1YBmGuMIlhKE-nKbHBQ-T3A-gTrYKZIqhrVcXn4nQc8LK6FbRpw6wIlE_2jI_BlqUNWULB2AxPbDQEFLngwJZ6GnjejtblSGz3renAybLWfESd7Yxzcx6wx6-0A5toi7rGwVo5Wqz8cRRqwvEqhtZhLvhaKzypjc0q3j9-O1DkFbZQvwoMls3L0Kr4QMT?purpose=fullsize
https://images.openai.com/static-rsc-4/bKNm1dg2T2cQDppP9ABe6wKo92LG7WBrDePZsA38OCq6ymEa19ScMn07l0L2Ff6aT_c6BgB1tf2GsVtl1SQ0uld2_8qsB4M3LuXQFuyMdmtFCQvhdaU6h56823CYYBVuWy53FJlpS5Y6F4hmZGa91R-qqSdCdcgdWOgZ-W7NZgFQACDgW3sBrB0L3jGD83BB?purpose=fullsize

4

Method 1: Using GUI (Recommended)

  1. Press Windows + S and search for Environment Variables
  2. Click Edit the system environment variables
  3. In System Properties, click Environment Variables
  4. Choose:
    • User variables (for your account)
    • System variables (for all users)
  5. Click New or Edit
  6. Enter:
    • Variable Name (e.g., JAVA_HOME)
    • Variable Value (e.g., C:\Program Files\Java\jdk-17)
  7. Click OK and restart apps

Method 2: Using Command Prompt

setx VARIABLE_NAME "value"

Example:

setx NODE_ENV "production"

Note: Changes apply only to new sessions.


Method 3: Using PowerShell

[Environment]::SetEnvironmentVariable("VARIABLE_NAME", "value", "User")

How to Set Environment Variables in macOS

https://images.openai.com/static-rsc-4/nnCqHb2NmnD9EUfpKiw_JkSCPpNtJskzFhEBUTCZvzqJxyyrJ0ihL5LET7IZHajgk2TgEMZaOviUfjrP7AARaQX18104c5rKIDXjQyt78ZqFXV7wmQGibLUcaQl5Zyouo8MH-mDQ9-MNNP5whcluRrco6Ugb9PIMrWHXIq0us4_guQZoJsgq3uJiQakS8DFl?purpose=fullsize
https://images.openai.com/static-rsc-4/ZPJpqT6hMmuNRXwITe2G6SdQXGuGtoya961r1jrbMJgYspb-5BGJotM4l39aJmFg0ROwuiZg31JDiCSnSGJFLMr3nVLx5qUMCzbsIIR7klMIcXnSiRgnSsRFyS7g9dkNmui90N3Up8LxN6t0B96zIx_BOYva0f80WA8QVBkHat7q6ZUEOI5n1DIL07camzwa?purpose=fullsize
https://images.openai.com/static-rsc-4/82MmvmOzanqJiHDZQG3TGHcp4fHTk-lPAHD7XTWjlv-usZ6P6LwKo_UvOwib3Jex0iTj3zRjZkeCgn0Tx5vPVcnLUzqhOZKkWJz5okcHxeu3tq9nnwRtGMQANoRddXoraIjnkXNh5M1vgIrwUD1u10f97OfUf2m6mHYJdraoHh0vT39UOMWkrzKFo7X7y9Vi?purpose=fullsize

4

Step 1: Open Terminal

Press Command + Space, type Terminal, and open it.


Step 2: Identify Your Shell

Most modern macOS versions use zsh.

Check using:

echo $SHELL

Step 3: Edit Configuration File

For zsh:

nano ~/.zshrc

For bash:

nano ~/.bash_profile

Step 4: Add Environment Variable

export VARIABLE_NAME="value"

Example:

export JAVA_HOME="/Library/Java/JavaVirtualMachines/jdk-17.jdk/Contents/Home"

Step 5: Save and Apply Changes

  • Press CTRL + X, then Y, then Enter
  • Run:
source ~/.zshrc

Temporary vs Permanent Variables

TypeScopePersistence
TemporaryCurrent sessionLost after restart
PermanentSystem/UserSaved across reboots

Temporary Example (macOS/Linux):

export TEMP_VAR="test"

Common Errors and Fixes

1. Variable Not Recognized

Cause: Incorrect PATH or variable not applied
Fix: Restart terminal or system


2. Incorrect Path Value

Cause: Typo or wrong directory
Fix: Double-check file path


3. Changes Not Applied

Cause: Session not refreshed
Fix: Run source ~/.zshrc or restart system


4. Permission Denied (macOS)

Cause: Editing wrong file or lack of permissions
Fix: Use correct config file or sudo carefully


5. Duplicate PATH Entries

Cause: Adding same path multiple times
Fix: Clean up PATH variable


Best Practices / Pro Tips

1. Use Descriptive Names

Avoid vague names like VAR1. Use clear names like DATABASE_URL.


2. Avoid Hardcoding Sensitive Data

Use environment variables for:

  • API keys
  • Database credentials

3. Keep PATH Clean

Too many entries can slow down your system.


4. Document Your Variables

Especially in team environments—maintain a .env.example file.


5. Backup Before Changes

Export or note down existing variables before modifying them.


Helpful Resources

While configuring your system, you may also need these guides:


Conclusion

Setting environment variables is a foundational skill for developers and IT professionals. Whether you’re working on Windows or macOS, the process is straightforward once you understand the basics.

By following the steps in this guide, you can configure your system efficiently, avoid common mistakes, and improve your workflow. Environment variables not only simplify development but also enhance security and flexibility.

Take a few minutes to set them up correctly—and your future self will thank you.


FAQs

1. What is an environment variable in simple terms?

It’s a system setting that stores information used by programs and the operating system.


2. How do I check environment variables in Windows?

Open Command Prompt and type:

set

3. How do I view environment variables on macOS?

Run:

printenv

4. Are environment variables case-sensitive?

  • Windows: No
  • macOS/Linux: Yes

5. Can I delete an environment variable?

Yes. Use the same settings panel (Windows) or remove it from config files (macOS).

Leave a Reply

Your email address will not be published. Required fields are marked *