How to set environment variable in ubuntu?

Member

by johann , in category: General Help , 2 years ago

How to set environment variable in ubuntu?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

Member

by jewel , a year ago

@johann 

To set an environment variable in Ubuntu, you can use the export command in the terminal. For example, to set the environment variable VARNAME to varvalue, you can use the following command:

1
export VARNAME=varvalue


This will set the environment variable for the current terminal session. To make the environment variable persist across terminal sessions, you can add the export command to the .bashrc file in your home directory. To do this, open the .bashrc file in a text editor:

1
nano ~/.bashrc


Then, add the export command at the end of the file and save it:

1
export VARNAME=varvalue


The next time you open a terminal, the environment variable will be set.


Alternatively, you can also set an environment variable system-wide by adding the export command to the /etc/environment file. However, this method is not recommended as it may affect other system processes and could potentially cause issues.


It's also worth noting that environment variables are case-sensitive, so make sure to use the correct capitalization when setting and referencing them.

Member

by tina , 5 months ago

@johann 

To set an environment variable permanently for all users in Ubuntu, you can follow these steps:

  1. Open a terminal window.
  2. Open the .bashrc file in your home directory using a text editor. You can use the following command to open it with the nano text editor:
1
sudo nano ~/.bashrc


  1. Scroll to the end of the file using the arrow keys.
  2. Add the following line at the end of the file, replacing VARNAME with the name of the environment variable you want to set and varvalue with the desired value:
1
export VARNAME=varvalue


  1. Press Ctrl + X to exit nano. It will prompt you to save the modified buffer. Press Y to save the changes, and then press Enter to overwrite the existing file.
  2. Back in the terminal, run the following command to reload the .bashrc file:
1
source ~/.bashrc


Now, the environment variable will be set for all users, and it will persist across terminal sessions. You can test it by running the echo command followed by the variable name to display its value:

1
echo $VARNAME