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.

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:
- Open System Properties → Advanced → Environment Variables
- Click New or Edit
- Add variable name and value
- Save and restart apps
On macOS:
- Open Terminal
- Edit shell config file (
.zshrcor.bash_profile) - Add:
export VARIABLE_NAME=value - Save and run
sourcecommand
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:
PATHtells your system where to find executable filesJAVA_HOMEpoints to the Java installation directoryNODE_ENVdefines 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
4
Method 1: Using GUI (Recommended)
- Press Windows + S and search for Environment Variables
- Click Edit the system environment variables
- In System Properties, click Environment Variables
- Choose:
- User variables (for your account)
- System variables (for all users)
- Click New or Edit
- Enter:
- Variable Name (e.g.,
JAVA_HOME) - Variable Value (e.g.,
C:\Program Files\Java\jdk-17)
- Variable Name (e.g.,
- 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
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
| Type | Scope | Persistence |
|---|---|---|
| Temporary | Current session | Lost after restart |
| Permanent | System/User | Saved 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:
- Transfer WhatsApp data securely:
https://multicaretechnical.com/how-to-transfer-whatsapp-to-a-new-phone-complete-guide - Move contacts to a new device:
https://multicaretechnical.com/how-to-transfer-contacts-to-a-new-phone-complete-guide - Set up Microsoft Authenticator on a new phone:
https://multicaretechnical.com/how-to-transfer-microsoft-authenticator-to-a-new-phone
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).