How to install Node.js on Linux

How to Install Node.js on Linux Node.js has become an essential tool for modern web development, enabling developers to run JavaScript on the server side. If you're working on a Linux system, installing Node.js correctly is crucial for building scalable applications, running development tools, and managing JavaScript packages. This comprehensive guide will walk you through multiple methods to install Node.js on various Linux distributions, ensuring you can choose the approach that best fits your needs. What is Node.js and Why Install It? Node.js is a cross-platform JavaScript runtime environment built on Chrome's V8 JavaScript engine. It allows developers to execute JavaScript code outside of web browsers, making it possible to build server-side applications, command-line tools, and desktop applications using JavaScript. Key benefits of Node.js include: - High performance: Event-driven, non-blocking I/O model - Large ecosystem: Access to npm (Node Package Manager) with over 1 million packages - Scalability: Ideal for building scalable network applications - Unified development: Use JavaScript for both frontend and backend development Prerequisites Before installing Node.js on your Linux system, ensure you have: - A Linux distribution (Ubuntu, CentOS, Fedora, Debian, etc.) - Terminal access with sudo privileges - Basic command-line knowledge - Internet connection for downloading packages Method 1: Installing Node.js Using Package Managers Ubuntu and Debian-based Distributions The easiest way to install Node.js on Ubuntu and Debian-based systems is using the default package repository: ```bash Update package index sudo apt update Install Node.js sudo apt install nodejs Install npm (Node Package Manager) sudo apt install npm ``` Verify the installation: ```bash Check Node.js version node --version Check npm version npm --version ``` CentOS, RHEL, and Fedora For Red Hat-based distributions, use the following commands: CentOS/RHEL 7/8: ```bash Enable EPEL repository (if not already enabled) sudo yum install epel-release Install Node.js and npm sudo yum install nodejs npm ``` Fedora: ```bash Install Node.js and npm sudo dnf install nodejs npm ``` Verify installation: ```bash node --version npm --version ``` Arch Linux For Arch Linux users: ```bash Update package database sudo pacman -Sy Install Node.js and npm sudo pacman -S nodejs npm ``` Method 2: Installing Node.js from NodeSource Repository NodeSource provides official Node.js binary distributions for Linux. This method ensures you get the latest stable version directly from the Node.js team. Ubuntu/Debian Setup ```bash Download and execute the NodeSource setup script for Node.js 18.x curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash - Install Node.js (includes npm) sudo apt-get install -y nodejs ``` For different Node.js versions, replace `18.x` with your desired version (e.g., `16.x`, `20.x`). CentOS/RHEL/Fedora Setup ```bash Download and execute the NodeSource setup script curl -fsSL https://rpm.nodesource.com/setup_18.x | sudo bash - Install Node.js sudo yum install -y nodejs or for newer systems sudo dnf install -y nodejs ``` Verify NodeSource Installation ```bash node --version npm --version Check installation details which node which npm ``` Method 3: Using Node Version Manager (NVM) NVM (Node Version Manager) is an excellent tool for developers who need to work with multiple Node.js versions. It allows you to install, switch between, and manage different Node.js versions easily. Installing NVM ```bash Download and install NVM curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash Reload your terminal or run: source ~/.bashrc ``` Using NVM to Install Node.js ```bash List available Node.js versions nvm list-remote Install the latest LTS version nvm install --lts Install a specific version nvm install 18.17.0 Use a specific version nvm use 18.17.0 Set default version nvm alias default 18.17.0 List installed versions nvm list ``` NVM Usage Examples ```bash Switch to latest LTS nvm use --lts Install and use latest stable version nvm install node nvm use node Run a script with a specific Node.js version nvm exec 16.20.0 node app.js ``` Method 4: Installing from Source Code For advanced users who need custom configurations or the latest development features: Download and Compile Node.js ```bash Install build dependencies (Ubuntu/Debian) sudo apt-get install build-essential curl file git python3 Download Node.js source wget https://nodejs.org/dist/v18.17.0/node-v18.17.0.tar.gz Extract the archive tar -xzf node-v18.17.0.tar.gz cd node-v18.17.0 Configure, compile, and install ./configure make -j$(nproc) sudo make install ``` For CentOS/RHEL: ```bash Install build dependencies sudo yum groupinstall "Development Tools" sudo yum install curl file git python3 Follow the same download and compilation steps ``` Method 5: Using Snap Package Manager If your Linux distribution supports Snap packages: ```bash Install Node.js via Snap sudo snap install node --classic Verify installation node --version npm --version ``` Choosing the Right Installation Method | Method | Best For | Pros | Cons | |--------|----------|------|------| | Package Manager | Beginners, production servers | Simple, automatic updates | May have older versions | | NodeSource | Getting latest official releases | Latest versions, official support | Requires additional repository | | NVM | Developers, multiple projects | Version switching, flexibility | User-level installation only | | Source Compilation | Custom builds, latest features | Full control, latest code | Complex, time-consuming | | Snap | Universal package support | Easy installation, sandboxed | Limited customization | Post-Installation Configuration Setting up npm Global Packages Directory To avoid permission issues with global npm packages: ```bash Create a directory for global packages mkdir ~/.npm-global Configure npm to use the new directory npm config set prefix '~/.npm-global' Add to your ~/.bashrc or ~/.profile echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.bashrc Reload your shell configuration source ~/.bashrc ``` Installing Essential Global Packages ```bash Install commonly used global packages npm install -g nodemon npm install -g create-react-app npm install -g @angular/cli npm install -g vue-cli npm install -g typescript ``` Updating Node.js and npm Using package manager: ```bash Ubuntu/Debian sudo apt update && sudo apt upgrade nodejs npm CentOS/RHEL sudo yum update nodejs npm ``` Using NVM: ```bash Install latest LTS and migrate packages nvm install --lts --reinstall-packages-from=current ``` Updating npm separately: ```bash npm install -g npm@latest ``` Troubleshooting Common Issues Permission Errors with npm If you encounter permission errors when installing global packages: ```bash Option 1: Change npm default directory (recommended) mkdir ~/.npm-global npm config set prefix '~/.npm-global' echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.profile source ~/.profile Option 2: Use npx for one-time package execution npx create-react-app my-app ``` Node.js Command Not Found If the `node` command isn't recognized: ```bash Check if nodejs is installed instead nodejs --version Create a symlink if needed sudo ln -s /usr/bin/nodejs /usr/bin/node Or add alias to ~/.bashrc echo 'alias node=nodejs' >> ~/.bashrc source ~/.bashrc ``` NVM Command Not Found If NVM isn't working after installation: ```bash Manually source NVM source ~/.nvm/nvm.sh Add to shell profile permanently echo 'source ~/.nvm/nvm.sh' >> ~/.bashrc source ~/.bashrc ``` Version Conflicts When dealing with version conflicts: ```bash Check current versions node --version npm --version which node With NVM, switch versions nvm list nvm use Clear npm cache if needed npm cache clean --force ``` SSL/Certificate Issues If you encounter SSL certificate errors: ```bash Configure npm to use HTTP registry (temporary fix) npm config set registry http://registry.npmjs.org/ Or update certificates sudo apt-get update && sudo apt-get install ca-certificates Reset to HTTPS after fixing certificates npm config set registry https://registry.npmjs.org/ ``` Uninstalling Node.js Removing Package Manager Installation Ubuntu/Debian: ```bash sudo apt-get remove nodejs npm sudo apt-get purge nodejs npm sudo apt-get autoremove ``` CentOS/RHEL: ```bash sudo yum remove nodejs npm ``` Removing NVM Installation ```bash Uninstall all Node.js versions nvm deactivate nvm unload Remove NVM directory rm -rf ~/.nvm Remove NVM lines from shell profile nano ~/.bashrc # Remove NVM-related lines ``` Complete Cleanup ```bash Remove global npm packages directory rm -rf ~/.npm-global Remove npm cache npm cache clean --force Remove any remaining Node.js files sudo rm -rf /usr/local/lib/node_modules sudo rm -rf /usr/local/include/node ``` Best Practices and Recommendations Development Environment 1. Use NVM for development: Allows easy switching between Node.js versions for different projects 2. Use LTS versions for production: Long Term Support versions provide stability and security updates 3. Keep npm updated: Regularly update npm to get latest features and security fixes Production Environment 1. Use package managers: More stable and easier to manage on production servers 2. Pin specific versions: Use exact version numbers in package.json for consistency 3. Regular updates: Keep Node.js updated for security patches Security Considerations ```bash Audit npm packages for vulnerabilities npm audit Fix vulnerabilities automatically npm audit fix Check for outdated packages npm outdated Update packages npm update ``` Conclusion Installing Node.js on Linux can be accomplished through various methods, each with its own advantages. For beginners, using the default package manager provides the simplest approach. Developers working on multiple projects benefit from NVM's flexibility, while production environments may prefer the stability of package manager installations or NodeSource repositories. The key is choosing the method that best fits your use case: - Package managers for simplicity and automatic updates - NodeSource for the latest official versions - NVM for development flexibility - Source compilation for custom requirements Remember to regularly update your Node.js installation and npm packages to ensure security and access to the latest features. With Node.js properly installed, you're ready to start building powerful JavaScript applications on your Linux system. Whether you're developing web applications, building APIs, or creating command-line tools, Node.js provides a robust foundation for your JavaScript projects. Take time to familiarize yourself with npm, explore the vast ecosystem of available packages, and follow best practices for a smooth development experience.