Setting up a modern JavaScript development environment on Ubuntu begins with understanding how to install npm, the Node Package Manager. This command-line tool is essential for installing, sharing, and managing software packages written in JavaScript, and it is bundled directly with Node.js. If you are running Ubuntu, whether on a desktop or server, the process is straightforward but requires attention to version control and repository configuration.
Understanding Node.js and npm
Before diving into the installation steps, it is important to grasp the relationship between Node.js and npm. Node.js is a runtime environment that allows you to execute JavaScript code outside of a web browser. npm is the default package manager for Node.js, providing access to a vast ecosystem of open-source libraries and tools. When you install Node.js on Ubuntu using the official repository or a version manager, npm is installed alongside it automatically.
Installing via the Official Ubuntu Repository
The simplest method to get npm running on Ubuntu is by using the APT package manager. This approach pulls software from the default Ubuntu repositories, ensuring stability and system integration. While the version may not be the absolute latest, it is thoroughly tested and suitable for production environments.
Step-by-Step APT Installation
Update your local package index with sudo apt update .
Install Node.js by running sudo apt install nodejs .
Verify the installation by checking the Node.js and npm versions with nodejs --version and npm --version .
This method installs both the node and npm binaries. You will typically use the node command to execute the JavaScript runtime.
Using Node Version Manager (nvm)
For developers who need to switch between different versions of Node.js, or who require the latest features, using Node Version Manager (nvm) is the superior approach. Nvm allows you to install and manage multiple active Node.js versions without interfering with the system-wide configuration. This is particularly useful for testing applications against different runtime environments.
Setting up NVM
To install nvm, you download a script from the official repository and execute it via your shell. This script modifies your shell configuration to add the nvm command. Once installed, you can list available versions and install the specific one your project requires, ensuring maximum flexibility.
Verifying the Installation
Regardless of the installation method you choose, verification is a critical step to ensure your environment is correctly configured. A successful installation means you can manage packages globally and locally without permission errors or path issues.
Running these commands confirms that the executables are in your system's PATH and that the runtime is ready for development.
Managing Global Packages
With npm installed, you can now leverage the vast repository of JavaScript tools. Global packages are installed system-wide and are typically command-line utilities. Common examples include tools for scaffolding projects, running build scripts, or linting code. Understanding how to install and update these packages is fundamental to maintaining a clean and efficient Ubuntu development setup.