How to set environment variable through vi in Ubuntu 16.04
Vim steps would be:
-
From your terminal open document for editing with commands:
sudo vi /etc/environment
- Note that the
sudo
is needed since only users with elevated priveleges can edit files in the/etc
folder
-
To start editing the file from your keyboard use the following key combinations:
- Start to edit: i
- To move around, use keys h, j, k, and l
After edit to get out of edit mode [ which was entered with i ], use esc
After exiting to save: Shift + ;, then [wq | x], now press Enter
These are just basic steps, others exist so search and read up.
Quick tips:
Always use the esc to get out of vim modes.
I wrote something but don't want to keep it (provided you didn't do esc, Shift+;, w, Enter), use esc, then Shift+;, then q followed by !, then finally Enter.
Finished want to save and quit at the same time, use esc, then Shift+;, then x, now finish with Enter
user716691
Updated on September 18, 2022Comments
-
user716691 over 1 year
I'm trying to set my java environment variable but I am new to Ubuntu so don't know how to update it through Vi. I want to do the following things:
Edit the environment file
# vi /etc/environment
Update the existing
PATH
variable by adding the belowbin
folders, separated with a colon:
/usr/lib/jvm/jdk1.8.0_151/bin:/usr/lib/jvm/jdk1.8.0_151/db/bin:/usr/lib/jvm/jdk1.8.0_151/jre/bin
HOME
directory paths can be different based on version and update,here the version is1.8
and the update is151
. Add the below variables at the end of environment file, making changes for your specific version and update.J2SDKDIR="/usr/lib/jvm/jdk1.8.0_151" J2REDIR="/usr/lib/jvm/jdk1.8.0_151/jre" JAVA_HOME="/usr/lib/jvm/jdk1.8.0_151" DERBY_HOME="/usr/lib/jvm/jdk1.8.0_151/db"
The
environment
file should now be similar to this text:PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/jvm/jdk1.8.0_151/bin:/usr/lib/jvm/jdk1.8.0_151/db/bin:/usr/lib/jvm/jdk1.8.0_151/jre/bin" J2SDKDIR="/usr/lib/jvm/jdk1.8.0_151" J2REDIR="/usr/lib/jvm/jdk1.8.0_151/jre* JAVA_HOME="/usr/lib/jvm/jdk1.8.0_151" DERBY_HOME="/usr/lib/jvm/jdk1.8.0_151/db"
My problem is I don't know much about editing and saving in Vi; I tried to do it through some resources but then it start giving warning and I don't want to corrupt things so I thought I should ask for help from some experienced users, so kindly help me through guiding step by step on how to do above things in Vi through terminal. I am using Ubuntu 16.04 LTS. Thank you!
-
Zanna about 6 yearsyou do not have to use Vi. You can use any text editor, for example run
sudo nano
if you know nano. To edit and save a file using Vi(m), see How do I save files edited with vim? -
user716691 about 6 years@Zanna Thank you so much for telling me ,through
sudo nano
it was quiet easy :)
-
-
user716691 about 6 yearsThank you so much for your detailed response , I have a further query after updating I was using update-alternatives to inform ubuntu about installed java paths and it gave me following response :
update-alternatives: using /usr/lib/jvm/jdk1.8.0_162/bin/java to provide /usr/bin/java (java) in auto mode
So is it all good ,I am actually doing it first time so asking thank you ! -
user716691 about 6 yearsand my initial command for update-alternatives was following:
sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/lib/jvm/jdk1.8.0_162/bin/javac" 0
-
George Udosen about 6 yearsUse this to see what you have:
update-alternatives --list java
-
user716691 about 6 yearsthanks ,i did that already and its showing
/usr/lib/jvm/jdk1.8.0_162/bin/java
Actually was confused when it said "in auto mode
" so suspected as if I'm wrong somewhere because don't know what auto mode really is ,that's why asked . -
George Udosen about 6 yearsPlease ask a fresh question!
-
user716691 about 6 yearsOk I was actually asking that after setting my variable I have given this command :
sudo update-alternatives --install "/usr/bin/java" "javac" "/usr/lib/jvm/jdk1.8.0_162/bin/java" 0
and it responded with thisupdate-alternatives: using /usr/lib/jvm/jdk1.8.0_162/bin/java to provide /usr/bin/java (java) in auto mode
I want to know what0
stands for in my first given command and what it really meant in its response when it saidin auto mode
Hope I am clear now ,thank you!