'Restart Shell in Vagrant
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?
Solution 1:[1]
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.
Solution 2:[2]
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
As mentioned, have not yet found a way to 'restart' the terminal - but would also like to know how that's done.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 |

