Skip to navigation

Install Node.js and npm without sudo

The following instructions install Node.js and npm into your home directory (does not require sudo)

Prerequisites:

You are connected to your dev VM via SSH in VS Code

Download and install NVM

curl -fsSL https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.4/install.sh | bash

Load nvm into your shell

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"

Install Node.js

nvm install --lts

Verify installation

node -v
npm -v

Persist nvm across sessions

To ensure nvm loads automatically every time you open a terminal, add this to your ~/.bashrc or ~/.bash_profile:

Note: If you are unsure if you have a ~/.bashrc or ~/.bash_profile run the following:

ls -a ~ | grep -E '(\.bashrc|\.bash_profile)'

The example appends the commands to ~/.bashrc. if your environment uses ~/.bash_profile then swap that filename in the commands below:

echo 'export NVM_DIR="$HOME/.nvm"' >> ~/.bashrc
echo '[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"' >> ~/.bashrc
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"

Reload your .bashrc (to avoid restarting your VM)

source ~/.bashrc

In case of trouble, I wanted to clarify that the start files for bash are .bash_profile, .bash_login, .profile or .bashrc. Depending on how your shell is started and which files you have one will be selected in favor of the others. For example, if your shell is a non-login shell (look that up if you want to know - it is hard to explain it in a few words) it will use .bashrc. For login shells, it will pick which ever it finds first (in this order): .bash_profile, .bash_login or .profile. It is best to only put these NVM lines in “the right one” so choose whichever of those 3 for login shells or the .bashrc for those commands. You can check for exactly which one if you look for any of those 4 files and add an echo "something" command to the top of each. Then start a new terminal session and look for the file with what you see printed on the screen.

You can guess that if you are using the integrated terminal in vscode and you have not configured it specifically to use a “login” session that you are using a non-login shell session. For the Windows Terminal app (wt.exe) you are likely to be using a login session. The main reason there are two kinds of session is that the non-login one is meant to start up more quickly but to have less support for the user to improve startup time. Traditionally it was used when running a shell inside another shell.

Troubleshooting

Docker created a file/folder owned by root that you can’t delete

Sometimes Docker containers create files or folders on your host system that are owned by root, especially when volumes are mounted and the container runs as root. If your user doesn’t have sudo access, you won’t be able to delete or modify those files directly.

Fortunately, since Docker runs as root inside the container, you can use Docker itself to fix the ownership.

Fixing ownership with Docker

Run the following command from your host machine in the directory containing the root-owned files or folders. This will recursively change ownership of everything in the current directory to match your user:

folders. This will recursively change ownership of everything in the current directory to match your user:

docker run --rm -v "$(pwd):/cleanup" -e HOST_UID=$(id -u) -e HOST_GID=$(id -g) alpine sh -c "chown -R \$HOST_UID:\$HOST_GID /cleanup"

This sets ownership to your current host user, making the files editable and deletable without needing sudo