8

I am new to Ubuntu!
And I am trying to setup manually Maven by adding Maven directory into $PATH. I created .bash_profile file in my home directory. The file contains this:

export PATH=/opt/devel/tools/apache-maven-3.3.3/bin:$PATH

And then on a terminal, I run

source .bash_profile

Everything works fine and I can see the version after running mvn -version. But after rebooting laptop, running mvn gets the following error:

The program 'mvn' can be found in the following packages:
 * maven
 * maven2
Try: sudo apt-get install <selected package>

Could you tell me what I am missing please? Any help would be appropriate!

Edit 1
The output of echo $PATH is:

tuandang@Inspiron-N4030:~$ echo $PATH

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
Maythux
  • 84,289
Tuna
  • 195

3 Answers3

7

First Please note that adding envs to the .bash_profile is not a temporary as indicated in other answer, but your problem is adding in non-suitable place since .bash_profile is called when you login from console which I don't think your case. Please Read the rest and find your solution:

Quoted from http://www.joshstaiger.org/archives/2005/07/bash_profile_vs.html:

When you login (type username and password) via console, either sitting at the machine, or remotely via ssh: .bash_profile is executed to configure your shell before the initial command prompt.

But, if you’ve already logged into your machine and open a new terminal window (xterm) inside Gnome or KDE, then .bashrc is executed before the window command prompt. .bashrc is also run when you start a new bash instance by typing /bin/bash in a terminal.

So as I suppose you logged in and use the terminal from inside then you should use the .bashrc instead. run this command:

echo 'export PATH=/opt/devel/tools/apache-maven-3.3.3/bin:$PATH' >>~/.bashrc

Then source it:

source .bashrc

For more information please read this

If you want your variables to be used in .bash_profile also you can do this trick. Add all of your variables in .bashrc then source it from .bash_profile. Add this to your bash_profile:

if [ -f ~/.bashrc ]; then
   source ~/.bashrc
fi

Now when you login to your system whenever it's from a console or GUI you'll get your environment.

Maythux
  • 84,289
2

You can add your PATH to ~/.profile

~./bash_profile does not affect terminal emulators, like gnome-terminal, that are started after you log into system.

As an option you can setup PATH in /etc/environment globally.

Pilot6
  • 90,100
  • 91
  • 213
  • 324
1

Maythux is correct, the variable was declared local, but for it to be seen as a global variable by the system it would have to be exported.

if [ -f ~/.bashrc ]; then 
    . ~/.bashrc 
fi 

Also works in .bash_profile to source $HOME/.bashrc

Maythux
  • 84,289
oOpSgEo
  • 539
  • A word of warning: when you start sourcing these numerous rc/profile files from one another madness ensues. Do the research and get things in the correct files. You'll be glad you did later. Nothing worse than finding your path containing the same routes multiple times. – RichieHH Sep 08 '18 at 11:51