Back to blog

Hướng Dẫn Hoàn Chỉnh Version Managers: nvm, pyenv, rbenv, jenv & More

devtoolsnodejspythonrubyjavaversion-management
Hướng Dẫn Hoàn Chỉnh Version Managers: nvm, pyenv, rbenv, jenv & More

Quản lý multiple versions của programming languages là thiết yếu cho modern development. Version managers giúp bạn chuyển đổi giữa các versions dễ dàng cho các projects khác nhau.


Tại Sao Dùng Version Managers?

Vấn Đề Khi Không Có Version Manager

Bạn có 3 projects:

  • Project A: Yêu cầu Node.js 14 (legacy)
  • Project B: Dùng Node.js 18 (stable)
  • Project C: Cần Node.js 20 (bleeding edge)

Không có version manager:

# Cài Node 14
sudo apt install nodejs=14.x
 
# Gỡ Node 14, cài Node 18
sudo apt remove nodejs
sudo apt install nodejs=18.x
 
# Lặp lại cho Node 20...
# 😱 Rất đau đầu!

Với version manager (nvm):

nvm install 14 18 20
 
cd project-a && nvm use 14
cd project-b && nvm use 18
cd project-c && nvm use 20
 
# 🎉 Dễ dàng!

Lợi Ích

✅ Multiple versions đồng thời - Chuyển đổi ngay lập tức
✅ Per-project versions - .nvmrc, .python-version files tự động switch
✅ Không cần sudo - User-level installations
✅ Dọn dẹp dễ dàng - Xóa versions với một lệnh
✅ Reproducible environments - Same versions across team members


Node.js Version Managers

Option 1: nvm (Node Version Manager)

Phổ biến nhất cho Node.js version management.

Cài Đặt

macOS/Linux:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
 
# Thêm vào shell profile (~/.bashrc, ~/.zshrc)
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
 
source ~/.zshrc

Windows: Dùng nvm-windows

Sử Dụng

# List available versions
nvm ls-remote
 
# Cài đặt version
nvm install 20.10.0
nvm install 18.19.0
 
# Dùng version
nvm use 20.10.0
 
# Set default
nvm alias default 20.10.0
 
# Gỡ cài đặt
nvm uninstall 14.21.3

Per-Project Version với .nvmrc

Tạo .nvmrc trong project root:

20.10.0

Sau đó:

cd my-project
nvm use  # Tự động dùng version từ .nvmrc

Option 2: fnm (Fast Node Manager)

Rust-based, nhanh hơn 20x so với nvm.

Cài Đặt

# macOS/Linux
curl -fsSL https://fnm.vercel.app/install | bash
 
# macOS với Homebrew
brew install fnm
 
# Thêm vào shell profile
eval "$(fnm env --use-on-cd)"

Sử Dụng

# Cài đặt version
fnm install 20.10.0
 
# Dùng version
fnm use 20.10.0
 
# Set default
fnm default 20.10.0

Recommendation: Dùng fnm cho speed, nvm cho stability.


Python Version Managers

pyenv

De facto standard cho Python version management.

Cài Đặt

macOS:

brew install pyenv
 
# Thêm vào shell profile
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
echo 'eval "$(pyenv init --path)"' >> ~/.zshrc
echo 'eval "$(pyenv init -)"' >> ~/.zshrc
 
source ~/.zshrc

Linux:

# Cài dependencies
sudo apt install -y make build-essential libssl-dev zlib1g-dev \
  libbz2-dev libreadline-dev libsqlite3-dev wget curl
 
# Cài pyenv
curl https://pyenv.run | bash

Sử Dụng

# List available versions
pyenv install --list
 
# Cài đặt version
pyenv install 3.12.1
pyenv install 3.11.7
 
# Set global version
pyenv global 3.12.1
 
# Set local (per-directory) version
cd my-project
pyenv local 3.11.7  # Tạo .python-version file
 
# Gỡ cài đặt
pyenv uninstall 3.10.13

Virtual Environments với pyenv-virtualenv

# Cài plugin
brew install pyenv-virtualenv
 
# Tạo virtual environment
pyenv virtualenv 3.12.1 my-project-env
 
# Activate
pyenv activate my-project-env
 
# Auto-activate per directory
cd my-project
pyenv local my-project-env

Ruby Version Managers

Option 1: rbenv

Simple và Unix-like Ruby version manager.

Cài Đặt

# macOS
brew install rbenv ruby-build
 
# Thêm vào shell profile
echo 'eval "$(rbenv init - zsh)"' >> ~/.zshrc
source ~/.zshrc

Sử Dụng

# List available versions
rbenv install --list
 
# Cài Ruby
rbenv install 3.3.0
 
# Set global version
rbenv global 3.3.0
 
# Set local version
rbenv local 3.2.2  # Tạo .ruby-version

Option 2: rvm (Ruby Version Manager)

Feature-rich với gemsets.

# Cài đặt
\curl -sSL https://get.rvm.io | bash -s stable
 
# Cài Ruby
rvm install 3.3.0
 
# Dùng version
rvm use 3.3.0
 
# Tạo gemset
rvm gemset create my-project
rvm use 3.3.0@my-project

Recommendation: Dùng rbenv cho simplicity, rvm cho gemsets.


Java Version Managers

Option 1: jenv

Java version manager giống rbenv.

# macOS
brew install jenv
 
# Thêm Java installations
jenv add /Library/Java/JavaVirtualMachines/jdk-21.jdk/Contents/Home
jenv add /Library/Java/JavaVirtualMachines/jdk-17.jdk/Contents/Home
 
# Set version
jenv global 21
jenv local 17  # Tạo .java-version

Note: jenv không cài đặt Java; chỉ quản lý existing installations.


Option 2: SDKMAN!

Comprehensive Java ecosystem manager.

Cài Đặt

curl -s "https://get.sdkman.io" | bash
source "$HOME/.sdkman/bin/sdkman-init.sh"

Sử Dụng

# List Java versions
sdk list java
 
# Cài Java
sdk install java 21.0.1-tem      # Temurin
sdk install java 17.0.9-amzn     # Amazon Corretto
 
# Dùng version
sdk use java 21.0.1-tem
 
# Set default
sdk default java 21.0.1-tem
 
# Cài tools khác
sdk install gradle
sdk install maven
sdk install kotlin

Recommendation: Dùng SDKMAN! cho full ecosystem management.


Go Version Manager: gvm

# Cài đặt
bash < <(curl -s -S -L https://raw.githubusercontent.com/moovweb/gvm/master/binscripts/gvm-installer)
 
# Cài Go version
gvm install go1.21.5
 
# Dùng version
gvm use go1.21.5 --default

Alternative: Download binaries từ go.dev và update PATH.


Rust Version Manager: rustup

Official Rust toolchain installer.

# Cài rustup
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
 
# Cài toolchains
rustup toolchain install stable
rustup toolchain install nightly
 
# Set default
rustup default stable
 
# Update Rust
rustup update

asdf: Universal Version Manager

Một tool cho tất cả languages.

Cài Đặt

# macOS
brew install asdf
 
# Thêm vào shell profile
echo '. "$HOME/.asdf/asdf.sh"' >> ~/.zshrc
source ~/.zshrc

Sử Dụng

# Thêm plugins
asdf plugin add nodejs
asdf plugin add python
asdf plugin add ruby
 
# Cài versions
asdf install nodejs 20.10.0
asdf install python 3.12.1
 
# Set global versions
asdf global nodejs 20.10.0
 
# Set local versions
asdf local nodejs 18.19.0  # Tạo .tool-versions file

.tool-versions File

nodejs 20.10.0
python 3.12.1
ruby 3.3.0

Bảng So Sánh

LanguageRecommended ToolAlternativeNotes
Node.jsfnmnvmfnm nhanh hơn 20x
Pythonpyenvasdfpyenv có virtualenv support tốt hơn
Rubyrbenvrvmrbenv đơn giản hơn, rvm có gemsets
JavaSDKMAN!jenvSDKMAN! tự cài Java
GoOfficial binariesgvmGo's backward compatibility tốt
Rustrustup-Official tool
Multi-languageasdf-Tốt cho polyglot projects

Best Practices

1. Dùng Version Files

Luôn commit version files vào Git:

# .nvmrc (Node.js)
20.10.0
 
# .python-version (Python)
3.12.1
 
# .ruby-version (Ruby)
3.3.0
 
# .tool-versions (asdf)
nodejs 20.10.0
python 3.12.1

2. Document trong README

## Prerequisites
 
- Node.js 20.10.0 (dùng `nvm install`)
- Python 3.12.1 (dùng `pyenv install 3.12.1`)

3. CI/CD Integration

GitHub Actions với nvm:

- uses: actions/setup-node@v4
  with:
    node-version-file: '.nvmrc'

4. Team Consistency

# Setup script cho team members mới
#!/bin/bash
nvm install
pyenv install
bundle install
npm install

Common Workflows

Workflow 1: Testing Multiple Python Versions

# Cài versions
pyenv install 3.12.1 3.11.7 3.10.13
 
# Test script
for version in 3.12.1 3.11.7 3.10.13; do
  pyenv shell $version
  python --version
  python -m pytest
done

Workflow 2: Project Onboarding

# New developer setup
cd project
nvm use          # Đọc .nvmrc
pyenv install    # Đọc .python-version
npm install
pip install -r requirements.txt

Workflow 3: Upgrade Path

# Current: Node 18 → Target: Node 20
nvm install 20.10.0
nvm use 20.10.0
npm install
npm test  # Verify compatibility
echo "20.10.0" > .nvmrc

Troubleshooting

Issue 1: "command not found: nvm"

Fix:

# Thêm vào ~/.zshrc
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
 
source ~/.zshrc

Issue 2: pyenv build fails

Fix (Ubuntu):

sudo apt install -y make build-essential libssl-dev zlib1g-dev \
  libbz2-dev libreadline-dev libsqlite3-dev wget curl

Issue 3: Slow shell startup với nvm

Fix: Dùng lazy loading hoặc chuyển sang fnm.

Issue 4: jenv không change Java version

Fix:

jenv enable-plugin export

Tóm Tắt và Key Takeaways

✅ Version managers cho phép seamless multi-version development
✅ Node.js: Dùng fnm (nhanh) hoặc nvm (ổn định)
✅ Python: Dùng pyenv với pyenv-virtualenv
✅ Ruby: Dùng rbenv (đơn giản) hoặc rvm (nhiều tính năng)
✅ Java: Dùng SDKMAN! cho ecosystem management
✅ Rust: Dùng rustup (official tool)
✅ Multi-language: Dùng asdf cho consistency
✅ Commit version files (.nvmrc, .python-version, .tool-versions)
✅ Document versions trong README
✅ CI/CD integration với version file support


Tiếp Theo Là Gì?

Sau khi đã biết quản lý runtime versions, khám phá thêm:

Development Tools:

  • Docker cho containerized development environments
  • Dev Containers (VS Code) cho reproducible setups
  • Nix cho declarative environment management

Tài Nguyên Bổ Sung

Tài liệu chính thức:


Có câu hỏi về version managers? Để lại comment bên dưới!

Chúc quản lý versions vui vẻ! 🚀

📬 Đăng ký nhận bản tin

Nhận những bài viết mới nhất qua email mỗi tuần. Không spam, hủy đăng ký bất cứ lúc nào.

Chúng tôi tôn trọng quyền riêng tư của bạn. Hủy đăng ký bất cứ lúc nào.

💬 Comments

Đăng nhập để bình luận

Chúng tôi sẽ không đăng bất cứ điều gì mà không có sự cho phép của bạn.