I've tried but every time I restart my system my variables are gone.
How can I store my environment variables in a permanent way on Ubuntu WSL2?
I've tried but every time I restart my system my variables are gone.
How can I store my environment variables in a permanent way on Ubuntu WSL2?
So this is the way that worked for me:
~/.bash_profile is the correct file for permanent environment variables if using Bash on Ubuntu WSL2, however make sure you add the export keyword before your variables, like so:
export THISVAR=thisIsAVar
export ANOTHERVAR=anotherVar
To add the variable(s) use the command sudo nano ~/.bash_profile (if you prefer Nano editor) or sudo vim ~/.bash_profile (if you prefer Vim). After you have entered your variables into the .bash_profile save the file and then enter source ~/.bash_profile to have the variables available in the terminal.
There are a few ways to go about this.
First, and the normal method (as you've discovered), is by setting the environment variables in your shell startup scripts. For bash, this would be ~/.bash_profile, for zsh it would be ~/.zprofile, and for fish it would be ~/.config/fish/config.fish.
But let me also suggest that you consider trying out the fish shell, as it has a great feature in its ability to set "universal variables", which are automatically propagated to all other instances of the shell, both present and in the future.
E.g. set -Ux myvariable 42 will create a universal (and exported) variable that will persist even after you close and reopen the shell, without having to create config files.
Fish has a number of other great features that make it my go-to shell (after 15 years on zsh).
That said, I'm not sure what your goal is with WSL, but if it's to "learn Linux", then you might be better off starting with bash or zsh since they are more "traditional" shells, with bash, of course, being the defacto starndard.
add alias to .bashrc file
sudo nano ~/.bashrc
add this line end of the file (in my case I want to run laravel)
alias laravel='~/.config/composer/vendor/laravel/installer/bin/laravel'
then restart wsl shell
exit
If you wish a variable to persist after you close the shell session, you need to set it as an environmental variable permanently. You can choose between setting it for the current user or all users.
To set permanent environment variables for a single user, edit the .bashrc file:
sudo nano ~/.bashrc
Write a line for each variable you wish to add using the following syntax:
export [VARIABLE_NAME]=[variable_value]
now restart your terminal
this is a full link for it: https://phoenixnap.com/kb/linux-set-environment-variable