Restart Shell in Vagrant - node.js

I'm provisioning a VirtualBox server running Ubuntu Trusty using Vagrant and installing nvm. After installation I am given the following instructions:
Close and reopen your terminal to start using nvm
When I install manually this is simple (I log out and back in) but how can I automate this in the vagrantfile?

It's probably a shorthand for "restart your shell". Starting a new login shell with something like exec bash --login (with suitable adaptations for other shells) should accomplish the same thing, but isn't easily scriptable (that is, the currently executing script will terminate).
In reality, I would guess sourcing the updated dot files should suffice. I.e. if $HOME/.bashrc was updated,
. $HOME/.bashrc
would bring in the changes to the current script in the vast majority of cases. (If something was removed, sourcing will obviously not remove that setting from the current environment in the typical case.)
Of course, if the process which would print this prompt has already terminated, new scripts you run should already receive the updated environment.

I was actually looking for the exact same thing. Facing the same issue with nvm.
Unfortunately, I was unable to find anything so far with regards to 'restarting' the terminal, but I followed the suggestion from the nvm install script and it seems to have worked. I was able to install node.
My Vagrantfile looks like this:
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/focal64"
config.vm.box_version = "20220517.0.0"
config.vm.provision :shell, path: "setup.sh", privileged: false
end
Where the setup.sh contains:
#!/usr/bin/env bash
# Get nvm
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
# Install Node 16
nvm install 16
Result
As mentioned, have not yet found a way to 'restart' the terminal - but would also like to know how that's done.

Related

How can I call "nvm use" in a bash script, and have the selected node version retained after the script executes?

For a build process that requires updating from Node.js 12 to 14, I'd like a bash script to detect whether nvm is installed, and, if so, do nvm use v14 (or nvm install v14 if necessary), and then I want the nvm-selected node version to stick at 14 after the bash script terminates, not just for the duration of the script.
I can switch to v14 with this script, but after the script has terminated, the shell environment remains at v12:
#!/bin/bash
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
source ~/.bashrc
nvm --version
echo $NVM_BIN
node --version
nvm use v14
echo $NVM_BIN
node --version
Just executing the nvm command in a bash script is a pain because nvm isn't a true command, but a shell function, and the script has to use the first three lines to set up nvm for the script.
The output is:
0.33.11
/home/pi/.nvm/versions/node/v12.21.0/bin
v12.21.0
Now using node v14.16.0 (npm v6.14.11)
/home/pi/.nvm/versions/node/v14.16.0/bin
v14.16.0
When the script is finished, however:
I think the trick might be making sure the environment variable NVM_BIN persists at the v14 path when the script exits, but I don't know how to do that. In fact, I think it's generally not allowed for the shell in which a script executes to change environment variables in the parent shell.
The nvm commmand, however, is itself a shell script, and whatever it does is persistent after it's done executing. There should be some way for me to make this happen too.
I think if you set an alias with nvm for default,
nvm alias default 14.0.0
the node version will persist across any new terminal instances.
To have the selected node version after the script executes you can run it like this:
. change-node-version.sh
script
node --version
nvm use 14.18.0
node --version
then:
node --version
➜ v14.18.0

By restarting system terminal does not recognize npm and node commands

I have installed node and npm by nvm; At the end of instaling nvm, below message was shown:
Close and reopen your terminal to start using nvm or run the following to use it now:
export NVM_DIR="$HOME/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
So I run that command and use nvm, install node versions and npm.
But when I restart my system, I have to run above command otherwise terminal says command not found.
What is the problem and how can I fix it?
The nvm installation should have added a couple of lines to your shell initialization script that makes sure to initialize nvm in every new shell, but for some reason this seems to have not happened.
Assuming that you're using bash, try adding these lines to your ~/.bash_profile
export NVM_DIR="$HOME/.nvm"
. "$(brew --prefix nvm)/nvm.sh"
Then start a new terminal session and see if that does the trick.
Another thing that could be worth double-checking is to see if you have any installed node version aliased as the default one. I'm not sure what nvm does if you don't have a default version specified (if that's even possible). Run nvm ls and see you you have default pointing to any installed version of Node. If not, then set that up by running nvm alias default [version-number].

"nvm" command not recognized in new command line windows/tabs despite presence of "NVM_DIR" variable in both ~/.bashrc and ~/.bash_profile

My colleague recently installed Node Version Manager on his Macbook using Homebrew, and ran the two commands suggested at the end of the install script:
export NVM_DIR=~/.nvm
source $(brew --prefix nvm)/nvm.sh
Everything works fine in the terminal window in which the install took place, but if he opens up a new terminal window or even a new tab, he has to install NVM all over again.
My level of command-line experience is relatively low (I don't know what I don't know), so I'm not sure where to start diagnosing the problem. Suggestions are welcome.
The instructions from brew after installing nvm are:
Add the following to $HOME/.bashrc, $HOME/.zshrc, or your shell's equivalent configuration file:
source $(brew --prefix nvm)/nvm.sh
On OS X with default settings, you actually want $HOME/.profile (or ~/.profile). Just add the line above to the end of that file.
Running that line once will set up nvm in that shell session. But if you add it to your .profile file, it will be run at the beginning of every shell session.
If you don't want to edit the .bash_profile
You could also export the nvm() function to your current shell enviroment:
source $(brew --prefix nvm)/nvm.sh
to check that is working:
nvm -v
If you want to edit the .bash_profile
sudo vim ~/.bash_profile
Type i to insert, and add:
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
Press wq! to save and exit and finally restart the terminal application.
Never Give Up! ✌️

Installing NVM / NodeJs / EmberJS inside a Vagrant box

I am having all sorts of fun with this.
The initial issue is that the terminal needs to be restarted after installing NVM so that I can re-initialise with the .bashrc settings and then install NodeJS - so my thinking was that I would build a base box with NVM already installed. That way the terminal will already be initialised with the NVM stuff.
Not the case... Apparently packaging a basebox using Vagrant ignores everything in the /home/vagrant folder. ARRRRRRRGGGGHHHHH!!
REALLY?!!1one
Has anyone had any luck with this? Getting NVM installed inside a vagrant box? or even NodeJs without sudo? It's a horrid rabbit hole atm and I want out!
I suggest you go back to the shell provisioning strategy , I've also went trough hell with this one but is definitely doable.
After a lot of googling I found that there are two very vaguely documented settings you require for this to work:
First and the most important part is that you need to enable the creation of symlinks on the VirtualBox instance with this line on your config.vm.provider block, without this NVM just won't work (look in here):
config.vm.provider "virtualbox" do |vb|
# (...)
vb.customize ["setextradata", :id, "VBoxInternal2/SharedFoldersEnableSymlinksCreate/vagrant","1"]
end
Next you'll have to separate your provisioning script in two parts, one that will run the normal apt/git/whatever stuff as root... and another that will run as the default 'vagrant' user:
$rootScript = <<SCRIPT
# some rooty stuff (just don't forget to include git and curl here)
SCRIPT
## This is the script that will install nvm as the default 'vagrant' user
$userScript = <<SCRIPT
cd /home/vagrant
# Installing nvm
wget -qO- https://raw.github.com/creationix/nvm/master/install.sh | sh
# This enables NVM without a logout/login
export NVM_DIR="/home/vagrant/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm
# Install a node and alias
nvm install 0.10.33
nvm alias default 0.10.33
# You can also install other stuff here
npm install -g bower ember-cli
SCRIPT
Last, you need to tell vagrant to run the second script with just user privileges (as almost entirely undocumented here):
config.vm.provision "shell", inline: $rootScript
config.vm.provision "shell", inline: $userScript, privileged: false
That should do. Is not pretty but it works.
Check out this working gist here, and good luck!

How to configure user environment to use the latest node runtime

We initialize node.js using nvm use 0.10.0
How to configure user environment to use the latest node runtime, so that the bash shell gets initialized when I launch a new terminal in Ubuntu
posting this answer for future reference and help.
1st run the following command in the bash.
nvm alias default <the default version you want to set>
then put this in your .bashrc
export NVM_DIR="path/to/.nvm/folder" #<-edit with real value
if [ -s "$NVM_DIR/nvm.sh" ]; then
source "$NVM_DIR/nvm.sh"
fi
NODE_DEFAULT_VERSION=$(<"$NVM_DIR/alias/default")
export PATH="$NVM_DIR/versions/node/$NODE_DEFAULT_VERSION/bin":$PATH
and, source .bashrc
the advantage of this setup is that, whatever node you will install and set as default, it will be available for all bash sessions.
You could put this in your ~/.bashrc:
nvm use 0.10.0

Resources