An environment variable is a named setting that stores information a program needs while it is running. It allows you to change how software behaves without changing its code.
Environment variables are commonly used for configuration, such as database addresses, API settings, ports, and information that may be different between development and production.
Why it matters
The same application often needs different settings depending on where it is running. Instead of putting those settings directly into the code, the application can read them from environment variables.
For example, a developer might connect to a test database on their computer, while the production system connects to the real database. The code stays the same, but the environment variables are different.
Environment variables are also commonly used for sensitive values such as passwords and API keys, although they still need to be protected carefully.
How it works
Environment variables are usually simple name-and-value pairs:
PORT=8080
DATABASE_HOST=db.example.com
LOG_LEVEL=info
The application can read these values when it starts. For example, instead of always running on port 8080, the code might read the PORT environment variable and use whatever value has been configured.
Environment variables can be set by your operating system, terminal, hosting platform, container, or deployment system.
Environment variable vs config file
Both environment variables and config files can be used to configure software.
A config file stores settings in a file, while environment variables are provided by the environment in which the program runs. Many applications use a combination of both.