Managing multiple Node.js versions on your Ubuntu system can be streamlined with NVM (Node Version Manager). Installing NVM allows you to easily switch between different Node.js versions for development, testing, or deployment purposes. Whether you’re a developer working on various projects or just want to keep your environment flexible, setting up NVM is a straightforward process. Before starting, ensure your Ubuntu system is up to date and has an active internet connection, as you’ll need to download scripts from the web.
What You’ll Need
- An Ubuntu system (version 18.04, 20.04, 22.04, or later)
- Access to a terminal with sudo privileges
- An active internet connection
- Basic familiarity with command line operations
Step-by-Step
- Open the Terminal: Launch your terminal application from the application menu or by pressing Ctrl+Alt+T.
- Update Package Lists: Run the following command to ensure your package list is current:
sudo apt update - Install Required Dependencies: Install curl or wget if not already installed. These tools are needed to download the NVM installation script:
sudo apt install curl -yor
sudo apt install wget -y - Download and Run the NVM Install Script: Use curl or wget to fetch the installation script from the official NVM repository:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.4/install.sh | bash— or —
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.4/install.sh | bash - Reload Your Shell Configuration: To make NVM available immediately, reload your shell profile:
export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "$HOME/.nvm" || printf %s "$XDG_CONFIG_HOME/nvm")" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm> Alternatively, close and reopen your terminal or run:
source ~/.bashrc - Verify the Installation: Confirm NVM is installed correctly by typing:
nvm --versionIf you see a version number, NVM is ready to use.
- Install Node.js Versions: Use NVM to install your preferred Node.js versions, for example:
nvm install 14.17.0or
nvm install node(which installs the latest stable version).
- Set Default Node.js Version: To set a default version for new terminal sessions:
nvm alias default 14.17.0
Common Mistakes
- Not updating package lists: Skipping “sudo apt update” can lead to outdated dependencies.
- Using incorrect script URL: Always copy the latest install script URL from the official NVM repository to avoid security risks or outdated scripts.
- Forgetting to reload shell: After installation, not reloading your shell configuration can make the “nvm” command unavailable.
- Installing as root: Run the installation as your normal user, not as root, to prevent permission issues.
Tips
- Use nvm list: To see all installed Node.js versions:
nvm list - Remove unused versions: To free up space:
nvm uninstall - Check for updates: Regularly update NVM by rerunning the install script to get the latest features and security patches.
- Use .nvmrc files: For project-specific Node.js versions, place a .nvmrc file in your project directory with the version number, then run “nvm use”.
Troubleshooting
If you encounter issues during installation or NVM isn’t working as expected, try these steps:
- Verify your shell configuration: Ensure that your shell profile (e.g., ~/.bashrc, ~/.zshrc) contains the lines to load NVM, typically added automatically by the install script. If not, add:
export NVM_DIR="$HOME/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" - Check for conflicting versions: Remove any previous Node.js installations that might interfere:
sudo apt remove nodejs npm -y - Reinstall NVM: Delete the ~/.nvm directory and rerun the install script if issues persist.
rm -rf ~/.nvm curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.4/install.sh | bash - Consult logs and error messages: Read terminal output carefully; often, errors indicate missing dependencies or permission issues.
Frequently Asked Questions
1. Can I install NVM without sudo?
Yes, NVM is designed to be installed per user account without requiring root privileges, making it safe and easy to manage multiple Node.js versions.
2. Is NVM compatible with all Linux distributions?
NVM works well on most Linux distributions, including Ubuntu, Debian, and Fedora, as long as you have a compatible shell environment. Compatibility issues are rare.
3. How do I uninstall NVM if I no longer need it?
Remove the ~/.nvm directory and delete the lines added to your shell profile (like ~/.bashrc). Then, restart your terminal.
4. Can I run multiple Node.js versions simultaneously with NVM?
No, NVM switches between versions. You can install multiple versions but only activate one at a time using “nvm use”.
5. How do I update NVM to the latest version?
Re-run the install script from the official repository. NVM manages its updates this way, ensuring you have the latest features and security fixes.
6. Does NVM affect system-wide Node.js installations?
No, NVM manages Node.js versions within your user space, so it doesn’t interfere with system-wide installations unless explicitly configured.
By following these steps, tips, and troubleshooting advice, you can easily set up NVM on your Ubuntu system and manage multiple Node.js versions effectively. This flexibility enhances your development environment and helps keep your projects organized and up to date.
Related
- How to Hide WiFi Networks in Windows 11 for Better Privacy
- How To Hide Windows Updates in Windows 10 Effectively
- How to Hide Windows Terminal in Notification Area