Install Node.js - Debian

Node.js is an open-source, cross-platform JavaScript runtime environment for developing a wide variety of tools and applications. Node.js runs on most operating systems, including Linux, OSX and Windows.

There are several different ways you can install Node.js on your server. This guide will cover these different options including:

  • APT package manager
  • PPA (Personal Package Archive)
  • NVM (Node Version Manager)

You can use whatever install method of Node.js that you prefer. If your Node.js application needs a specific version, or multiple versions, we suggest using NVM. Otherwise, if the current Node.js version works for you application, using APT is usually fine.

Install the Distro-Stable Version of Node.js with APT

  1. Update your local package index:
    sudo apt-get update
  2. Install Node.js:
    sudo apt-get install nodejs
  3. You will also want to install NPM, which lets you easily manage your different Node.js packages:
    sudo apt-get install npm

Note: To avoid conflicts, the executable from the Debian repositories is nodejs instead of node. Keep this in mind as you are executing applications.

Install Using a PPA

Another way to get the most recent version of Node.js is to add a PPA (Personal Package Archive), which is maintained by NodeSource. If you don't want to use the Node.js version in the APT repositories, this is a good alternative.

  1. Install the PPA. This will give you access to its contents.
    • For the most recent LTS (the 6.x branch), use:
      curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
    • For the older LTS (the 4.x branch), use:
      curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash -
    • For the currently active release (the 7.x branch), use:
      curl -sL https://deb.nodesource.com/setup_7.x | sudo -E bash -
  2. The PPA will be added into your APT configuration, and your local package cache will be automatically updated. After you run the setup script from NodeSource, you can then use the previous steps to install the Node.js package with APT.
    sudo apt-get install nodejs
  3. Note: The Node.js package contains the Node.js binary as well as npm, so you don't need to install npm separately. However, in order for some npm packages to work (such as those that require building from source), you will need to install the build-essentials package.

  4. Install build-essentials:
    sudo apt-get install build-essential

Install Node.js using NVM

Instead of using apt, you can install Node.js with a tool called nvm, which stands for "Node.js Version Manager".

NVM lets you install multiple different versions of Node.js. This way, you have more control over your environment for whatever your project needs. NVM will also give you on-demand access to the latest versions of Node.js, and the ability to use previous releases that your application may be dependent on.

  1. Run apt-get update:
    sudo apt-get update
  2. Install build-essentials:
    sudo apt-get install build-essential libssl-dev
  3. Download and install the installation script.

    Download the nvm installation script from the project's GitHub page.

    curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | sh

    This will download the script and execute it. It will install into a subdirectory of your home directory at ~/.nvm. It also adds the essential lines to your ~/.profile file to use the file with bash.

    Note: This command will pull from the Github Master page so that it is always up to date, but you can use previous versions by finding the URL on that GitHub repository.

  4. Update your current session:
    source ~/.profile
  5. List available Node.js versions.

    Now that nvm is installed, you can install isolated Node.js versions:

    nvm ls-remote
    . . .

    v6.7.0

    v6.8.0

    v6.8.1

    v6.9.0

    v6.9.1

    v7.0.0

    v7.1.0

  6. Install the version of Node.js that you want to use.

    In this example, the newest version is v7.1.0. If you wanted to install V7.1.0 you can do so by using the nvm install command. If there is a newer version available, use that version.

    nvm install 7.1.1
  7. After the command runs, your desired version of Node.js will be installed.

Options

  • Typically, nvm will default to the newest version, but you can explicitly tell nvm to use a previous version by using the following syntax where the highlighted portion is the version you wish to run.
    nvm use 7.0.0
  • When using nvm to install Node.js, the executable is simply called node. You can find out what Node.js version is being used by the shell by typing:
    node -v
  • This will display the version being used:
    v.7.1.0
  • If you have multiple versions of Node.jsc you can determine the installed version by typing:
    nvm ls
  • To set a specific Node.js as default, you can use the following syntax where the highlighted portion of the text is the desired version:
    nvm alias default 7.1.0
  • That version will be automatically selected when a new session is generated. You can also reference it using an alias such as:
    nvm use default

    Each version of Node.js will keep track of its own packages and uses npm to manage them.

  • You can instruct npm to install different JavaScript packages to your Node.js project's ./node_modules. In this example, we will install Express.js:
    npm install express
  • If you want to make that package available globally to all your Node.js projects, you can simply add the -g flag:
    npm install -g express

    After that command has been run, the package will be installed in:

    ~/.nvm/node_version/lib/node_modules/package_name
  • Using the global installation will allow you to run the commands from the command line. However, you will have to link the package into your local sphere to require it from within an application. In this example, we are linking Express.js to our current project:
    npm link express
  • For any other information about the NVM command, you can use help:
    nvm help

Bài này có hữu ích không?
Thanks for your feedback. To speak with a customer service representative, please use the support phone number or chat option above.
Rất vui vì chúng tôi đã giúp được bạn! Chúng tôi có thể làm gì thêm cho bạn?
Rất tiếc về điều đó. Tell us what was confusing or why the solution didn’t solve your problem.