How to Install NVM and LTS Node.js with a Bash Script?

April 22, 2025 3 answers
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

racket string 24
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
53 semaphore flags
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?

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.

Reply to this answer
_due diligence
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.

Related Discussions