To replace a direct Node.js installation on a Raspberry Pi 3 running Raspbian with NVM, you must first remove the existing version to avoid path conflicts. Then, update your system packages and install NVM using the official curl script. Finally, use NVM to install your desired Node.js version, which provides better management capabilities than static system installations.
Removing the Previous Installation
Before installing NVM, it is critical to ensure that no previous versions of Node.js or npm are interfering with the environment. Direct installations often place binaries in system directories like
/usr/bin, which can conflict with NVM's user-level directory structure. Start by opening your terminal and checking if Node.js is already installed by running
node -v. If a version number appears, you need to remove it.
You can attempt to remove the package using the apt package manager with the command
sudo apt-get remove nodejs. This removes the main package but may leave configuration files or dependencies behind. To clean up completely, run
sudo apt-get autoremove and
sudo apt-get autoclean. It is also wise to check for any remaining npm installations by running
npm -v and removing them if necessary. Clearing these ensures that NVM becomes the primary source for Node.js commands.
Updating System Packages
After clearing out old software, update your Raspberry Pi's package lists to ensure you have the latest security patches and dependency information. Run
sudo apt-get update followed by
sudo apt-get upgrade -y. This step is essential because NVM and Node.js compilation tools rely on current system libraries. While this process might take several minutes depending on your internet connection, it prevents potential build errors later. Ensure your Raspberry Pi 3 has adequate storage space before proceeding, as updates can consume significant disk resources.
Installing NVM
NVM (Node Version Manager) allows you to install and switch between multiple Node.js versions easily. The recommended way to install NVM is via the official installation script provided by the project maintainers. Use curl to download and execute the script directly into your shell environment. Run the following command:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
This script clones the NVM repository into ~/.nvm and adds the necessary export lines to your
~/.bashrc file. After the script finishes, you must reload your shell configuration for the changes to take effect immediately. Execute
source ~/.bashrc or simply close and reopen your terminal window. Verify the installation by typing
nvm --version; it should return the version number without errors. For more details on managing local environments, see our guide on
Running a Standalone App on Raspberry Pi with Local WiFi AP.
Installing Node.js via NVM
With NVM active, you can now install specific versions of Node.js. Unlike the system-wide installation, NVM installs Node.js in your home directory, making it non-root and safer for development. To install the latest Long Term Support (LTS) version, which is generally recommended for production stability, run
nvm install --lts. You can also specify a precise version, such as
nvm install 18.17.0, if your project requires a particular release.
Once installed, you can set this version as the default for all new shells using
nvm alias default node. This ensures that every time you open a terminal, the correct Node.js version is automatically loaded. You can verify the active version by running
node -v again; it should now reflect the version managed by NVM. Understanding how package managers interact with your system is crucial, similar to how
--legacy-peer-deps affects npm dependency resolution.
Managing Multiple Versions
One of the primary advantages of using NVM is the ability to switch between different Node.js versions seamlessly. If you have a legacy project requiring an older version, you can install it with
nvm install 14.17.0 and then switch to it using
nvm use 14.17.0. This change is temporary for the current session. To make it permanent for a specific project, you can create a
.nvmrc file containing the version number and run
nvm use within that directory. This feature is particularly useful for developers working on multiple projects with conflicting requirements.
Frequently Asked Questions
Can I use NVM with sudo?
No, you should never use
sudo with NVM commands. NVM is designed to work entirely within your user space to avoid permission issues. Using
sudo will cause NVM to fail because it cannot modify system-wide paths correctly when elevated privileges are involved.
How do I uninstall NVM completely?
To remove NVM, delete the
~/.nvm directory and remove the NVM-related lines from your
~/.bashrc or
~/.zshrc files. You can do this manually or by running
nvm deactivate followed by deleting the directory. This returns your environment to its pre-NVM state.
Does NVM work on Raspberry Pi OS Lite?
Yes, NVM works perfectly on Raspberry Pi OS Lite (headless version). Since NVM relies only on the command line and does not require a graphical interface, it functions identically on both desktop and lite editions. Just ensure you have the basic build tools installed if compiling native modules.
Why is my Node.js version still showing the old one?
If
node -v shows an old version after installing via NVM, your shell profile may not have been sourced correctly. Restart your terminal or run
source ~/.bashrc manually. Also, check if there are any other PATH variables overriding the NVM path in your configuration files.
Is NVM slower than a direct installation?
NVM introduces negligible performance overhead once Node.js is running. The slight delay occurs only during shell startup when NVM initializes, which is typically milliseconds. For long-running applications, there is no noticeable difference in execution speed compared to a system-installed Node.js.