DEV Community

kingyou
kingyou

Posted on

Managing Node.js and Python Versions: A Step-by-Step Guide with nvm and uv

In modern software development, managing multiple versions of runtime environments is essential. Different projects often require different versions of Node.js or Python, and switching between them manually can be tedious and error-prone. This guide will walk you through using two powerful tools: nvm for Node.js and uv for Python.

Why Version Management Matters

Before diving into the tools, let's understand why version management is crucial:

  • Project compatibility: Different projects may require specific runtime versions
  • Testing: You need to test your code across multiple versions
  • Isolation: Avoid conflicts between system-wide installations
  • Easy upgrades: Switch versions without breaking existing projects

Part 1: Managing Node.js with nvm

nvm (Node Version Manager) is the most popular tool for managing Node.js versions. It's lightweight, easy to use, and works seamlessly across different projects.

Step 1: Install nvm

For macOS/Linux:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
Enter fullscreen mode Exit fullscreen mode

For Windows:

Download and install nvm-windows

After installation, restart your terminal and verify:

nvm --version
Enter fullscreen mode Exit fullscreen mode

Step 2: Install Node.js Versions

List available Node.js versions:

nvm ls-remote
Enter fullscreen mode Exit fullscreen mode

Install a specific version:

nvm install 18.18.0
Enter fullscreen mode Exit fullscreen mode

Install the latest LTS version:

nvm install --lts
Enter fullscreen mode Exit fullscreen mode

Step 3: Switch Between Versions

Use a specific version:

nvm use 18.18.0
Enter fullscreen mode Exit fullscreen mode

Check current version:

node --version
Enter fullscreen mode Exit fullscreen mode

Step 4: Set Default Version

Set a default Node.js version for new terminal sessions:

nvm alias default 18.18.0
Enter fullscreen mode Exit fullscreen mode

Step 5: Project-Specific Versions

Create a .nvmrc file in your project root:

18.18.0
Enter fullscreen mode Exit fullscreen mode

Now, when you enter the project directory, simply run:

nvm use
Enter fullscreen mode Exit fullscreen mode

nvm will automatically switch to the version specified in .nvmrc.


Part 2: Managing Python with uv

uv is a modern, blazingly fast Python package and project manager. It's significantly faster than traditional tools like pyenv and pip, and it handles both Python versions and virtual environments.

Step 1: Install uv

For macOS/Linux:

curl -LsSf https://astral.sh/uv/install.sh | sh
Enter fullscreen mode Exit fullscreen mode

For Windows:

irm https://astral.sh/uv/install.ps1 | iex
Enter fullscreen mode Exit fullscreen mode

Verify installation:

uv --version
Enter fullscreen mode Exit fullscreen mode

Step 2: Install Python Versions

List available Python versions:

uv python list
Enter fullscreen mode Exit fullscreen mode

Install a specific Python version:

uv python install 3.12.1
Enter fullscreen mode Exit fullscreen mode

Install multiple versions:

uv python install 3.11.8 3.12.1
Enter fullscreen mode Exit fullscreen mode

Step 3: Set Python Version for a Project

Navigate to your project directory and pin a Python version:

cd my-project
uv python pin 3.12.1
Enter fullscreen mode Exit fullscreen mode

This creates a .python-version file that uv will automatically detect.

Step 4: Create Virtual Environments

Create a virtual environment with a specific Python version:

uv venv --python 3.12.1
Enter fullscreen mode Exit fullscreen mode

Activate the virtual environment:

macOS/Linux:

source .venv/bin/activate
Enter fullscreen mode Exit fullscreen mode

Windows:

.venv\Scripts\activate
Enter fullscreen mode Exit fullscreen mode

Step 5: Install Packages

With uv, package installation is incredibly fast:

uv pip install requests numpy pandas
Enter fullscreen mode Exit fullscreen mode

Install from requirements.txt:

uv pip install -r requirements.txt
Enter fullscreen mode Exit fullscreen mode

Best Practices

For Node.js Projects:

  1. Always use .nvmrc: Document the required Node.js version in your repository
  2. Install project dependencies locally: Use npm install or yarn install after switching versions
  3. Avoid global packages: Use npx to run packages without global installation

For Python Projects:

  1. Use .python-version: Let uv automatically detect the required Python version
  2. Always use virtual environments: Isolate project dependencies
  3. Generate requirements.txt: Keep track of dependencies with uv pip freeze > requirements.txt

Quick Reference Commands

nvm Commands

nvm install <version>    # Install a Node.js version
nvm use <version>        # Switch to a version
nvm ls                   # List installed versions
nvm ls-remote            # List available versions
nvm alias default <v>    # Set default version
nvm uninstall <version>  # Remove a version
Enter fullscreen mode Exit fullscreen mode

uv Commands

uv python install <v>    # Install Python version
uv python pin <version>  # Pin version for project
uv python list           # List available versions
uv venv                  # Create virtual environment
uv pip install <pkg>     # Install package
uv pip freeze            # List installed packages
Enter fullscreen mode Exit fullscreen mode

Conclusion

Managing multiple runtime versions doesn't have to be complicated. With nvm and uv, you can:

  • ✅ Switch between versions effortlessly
  • ✅ Maintain project-specific configurations
  • ✅ Avoid version conflicts
  • ✅ Improve development workflow

Both tools are actively maintained, widely adopted, and will make your development experience much smoother. Give them a try in your next project!


Have you used nvm or uv in your projects? Share your experience in the comments below! 💬

Top comments (0)