Different ways to set environment variable for .NET Core application

How to setup environment value for .NET Core application on Linux host

.NET Core comes with a great feature for loading different config file based on the environment. Environment is picked up from the host environment variables whether that is Windows on Linux based OS. Three environments (Development, Staging and Production) are fully supported out of the box, but you are free to use any other value that suits your need. For one of my cases I was using environment variable to set the region in which application is running in, so it is a lot flexible.


If environment variable ASPNETCORE_ENVIRONMENT is not set, .NET Core will fallback to default value which is always Production

Depending on which host and how you are running your application, there are various ways to set environment variable value. 

Linux host

Setting environment variable for the current session can be done directly from the bash command line


This command will set environment variable for the current running session. To set it permanently you have to update bash profile. Depending on which CLI you use and distro location may vary. I usually host on Debian based distro with apt package manager, so most of the snippets will be samples from Debian based machines.

To make it permanent you need to update your bash profile. Use the following command to open bash profile in nano editor.

sudo nano ~/.bashrc

Navigate to the end of the file and add the following line


Setting the variable in bash profile file will set it permanently only for the currently logged user. If you want to set it permanently on the system level you have to open and edit profile with your editor of choice. As I mentioned I use nano, so the following command will open nano with your system profile file opened

sudo nano /etc/profile

Paste the same line as for the bash profile file


Linux daemon

If you are running .NET Core application on a Linux host as a daemon, you can declare your environment variables in a .service file which is used to register a daemon

# Service environment varaibles

Another way to declare environment variables for your daemon/service is to have environment variables file which you link from .service file

# Service environment varaibles file

And the content of environment variables file would be in the following format



Windows host

Setting environment variable for the current session on Windows machine can be easily done from the Windows Command prompt.


This will set value for the environment variable for the current command process session. Once you close the command prompt this value will be lost. To set it permanently you have two options. 

To set Environment variable from the Windows UI you have to go to Control Panel/System/Advanced system settings and click on Environment Variables... button


You can also reach this UI dialog by running the following command from the Run dialog (Win+R)

rundll32 sysdm.cpl,EditEnvironmentVariables

After pasting the command above user CTRL+SHIFT+ENTER to run it as elevated to be able to access System variables sections of the dialog.

Run Env Variables

If you find your self more comfortable with command line execute the following line to set current use environment variable value


To set it on the system level you will have to run the same command with /M switch in the elevated (Run as Administrator) console


Docker container

If you have your application containerized you can simply set environment variable in docker-compose.yml file with environment section where you can set as many environment variables as you need

version: '3'

    image: MyCoreApplication_Image
    container_name: MyCoreApplication_Cotainer
      context: .
      - 80:80
      - ./Logs:/app/Logs


Dotnet CLI 

This way of setting environment variables if they are not containerized is my favorite because it is OS agnostic. Basically we start our application with environment option and our application will have the environment set to value of the environment argument value.

dotnet MyApplication.dll --environment=MyEnvironment

How do you set .NET Core environment value?


Purpose of the code contained in snippets or available for download in this article is solely for learning and demo purposes. Author will not be held responsible for any failure or damages caused due to any other usage.

About the author


Dejan is a passionate Software Architect/Developer. He is highly experienced in .NET programming platform including ASP.NET MVC and WebApi. He likes working on new technologies and exciting challenging projects

CONNECT WITH DEJAN  Loginlinkedin Logintwitter Logingoogleplus Logingoogleplus


read more


read more

Umbraco CMS

read more


read more

Comments for this article