How to Install NVM and LTS Node.js with a Bash Script?
Asked by
~ticket 64
I'm looking for a simple bash script that can install NVM (Node Version Manager) along with the latest LTS version of Node.js on Ubuntu 22.04. I usually run the following commands just fine in an interactive shell, but I can't seem to get it to work within a bash script. Here's what I normally use: `curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash && source ~/.bashrc && nvm install --lts`. Can anyone help me with this?
Answers
Have you checked if your `.bashrc` file sources NVM after installation? Sometimes, you need to manually add the NVM source line if itโs missing, or ensure your script is being run in a context where `.bashrc` is loaded.
Reply to this answer
It sounds like youโre trying to simplify things with a script. Just remember, since `nvm` is a bash script itself, make sure you have the `#!/bin/bash` shebang at the top of your script. Otherwise, it wonโt run properly. Whatโs going wrong specifically when you run your dependencies script?
Reply to this answer
You might want to try using `asdf`. It's a great tool for managing installations like this. The newer version is a single binary and simplifies managing multiple developer tools, including Node.js.
Reply to this answer
Add your answer
Answers are reviewed before publication.
spoonbill: Iโm actually trying to automate the installation of a Python project, including its dependencies, and Iโve had no issue except for NVM. It seems that after installing NVM, I canโt access it when running Python scripts in my bash script. I'm getting a `command not found` error. I've tried sourcing `~/.bashrc` again, but no luck.