Hướng dẫn cài nodejs trên ubuntu năm 2024

Node.js is a JavaScript runtime for server-side programming. It allows developers to create scalable backend functionality using JavaScript, a language many are already familiar with from browser-based web development.

Show

    In this guide, we will show you three different ways of getting Node.js installed on an Ubuntu 20.04 server:

    • using

      Output v10.19.0

      1 to install the

      Output v10.19.0

      2 package from Ubuntu’s default software repository
    • using

      Output v10.19.0

      1 with an alternate PPA software repository to install specific versions of the

      Output v10.19.0

      2 package
    • installing

      Output v10.19.0

      5, the Node Version Manager, and using it to install and manage multiple versions of Node.js

    For many users, using

    
    Output
    
    v10.19.0
    

    1 with the default repo will be sufficient. If you need specific newer or legacy versions of Node, you should use the PPA repository. If you are actively developing Node applications and need to switch between

    
    Output
    
    v10.19.0
    

    7 versions frequently, choose the

    
    Output
    
    v10.19.0
    

    5 method.

    This article will walk you through installing Node.js on an Ubuntu server. If you wanted a 1-click way to deploy a Node application to a live server, take a look at DigitalOcean App Platform.

    To follow this guide, you will need an Ubuntu 20.04 server set up. Before you begin, you should have a non-root user account with

    
    Output
    
    v10.19.0
    

    9 privileges set up on your system. You can learn how to do this by following the Ubuntu 20.04 initial server setup tutorial.

    Ubuntu 20.04 contains a version of Node.js in its default repositories that can be used to provide a consistent experience across multiple systems. At the time of writing, the version in the repositories is 10.19. This will not be the latest version, but it should be stable and sufficient for quick experimentation with the language.

    Warning: the version of Node.js included with Ubuntu 20.04, version 10.19, is now unsupported and unmaintained. You should not use this version in production, and should refer to one of the other sections in this tutorial to install a more recent version of Node.

    To get this version, you can use the

    
    Output
    
    v10.19.0
    

    1 package manager. Refresh your local package index first:

    1. sudo apt update

    Then install Node.js:

    1. sudo apt install nodejs

    Check that the install was successful by querying

    
    Output
    
    v10.19.0
    

    7 for its version number:

    1. node -v
    
    Output
    
    v10.19.0
    

    If the package in the repositories suits your needs, this is all you need to do to get set up with Node.js. In most cases, you’ll also want to also install

    1. sudo apt install npm

    2, the Node.js package manager. You can do this by installing the

    1. sudo apt install npm

    2 package with

    
    Output
    
    v10.19.0
    

    1:

    1. sudo apt install npm

    This allows you to install modules and packages to use with Node.js.

    At this point, you have successfully installed Node.js and

    1. sudo apt install npm

    2 using

    
    Output
    
    v10.19.0
    

    1 and the default Ubuntu software repositories. The next section will show how to use an alternate repository to install different versions of Node.js.

    To install a different version of Node.js, you can use a PPA (personal package archive) maintained by NodeSource. These PPAs have more versions of Node.js available than the official Ubuntu repositories. Node.js v16 and v18 are available as of the time of writing.

    First, install the PPA to get access to its packages. From your home directory, use

    1. sudo apt install npm

    7 to retrieve the installation script for your preferred version, making sure to replace

    1. sudo apt install npm

    8 with your preferred version string (if different):

    1. cd ~
    2. curl -sL https://deb.nodesource.com/setup_16.x -o /tmp/nodesource_setup.sh

    Refer to the NodeSource documentation for more information on the available versions.

    Inspect the contents of the downloaded script with

    1. sudo apt install npm

    9 or your preferred text editor:

    1. nano /tmp/nodesource_setup.sh

    When you are satisfied that the script is safe to run, exit your editor. Then run the script with

    
    Output
    
    v10.19.0
    

    9:

    1. sudo bash /tmp/nodesource_setup.sh

    The PPA will be added to your configuration and your local package cache will be updated automatically. You can now install the Node.js package in the same way you did in the previous section:

    1. sudo apt install nodejs

    Verify that you’ve installed the new version by running

    
    Output
    
    v10.19.0
    

    7 with the

    1. cd ~
    2. curl -sL https://deb.nodesource.com/setup_16.x -o /tmp/nodesource_setup.sh

    2 version flag:

    1. node -v
    1. sudo apt install nodejs

    0

    The NodeSource

    
    Output
    
    v10.19.0
    

    2 package contains both the

    
    Output
    
    v10.19.0
    

    7 binary and

    1. sudo apt install npm

    2, so you don’t need to install

    1. sudo apt install npm

    2 separately.

    At this point, you have successfully installed Node.js and

    1. sudo apt install npm

    2 using

    
    Output
    
    v10.19.0
    

    1 and the NodeSource PPA. The next section will show how to use the Node Version Manager to install and manage multiple versions of Node.js.

    Another way of installing Node.js that is particularly flexible is to use nvm, the Node Version Manager. This piece of software allows you to install and maintain many different independent versions of Node.js, and their associated Node packages, at the same time.

    To install NVM on your Ubuntu 20.04 machine, visit the project’s GitHub page. Copy the

    1. sudo apt install npm

    7 command from the README file that displays on the main page. This will get you the most recent version of the installation script.

    Before piping the command through to

    1. nano /tmp/nodesource_setup.sh

    0, it is always a good idea to audit the script to make sure it isn’t doing anything you don’t agree with. You can do that by removing the

    1. nano /tmp/nodesource_setup.sh

    1 segment at the end of the

    1. sudo apt install npm

    7 command:

    1. sudo apt install nodejs

    1

    Review the script and make sure you are comfortable with the changes it is making. When you are satisfied, run the command again with

    1. nano /tmp/nodesource_setup.sh

    1 appended at the end. The URL you use will change depending on the latest version of nvm, but as of right now, the script can be downloaded and executed with the following:

    1. sudo apt install nodejs

    2

    This will install the

    
    Output
    
    v10.19.0
    

    5 script to your user account. To use it, you must first source your

    1. nano /tmp/nodesource_setup.sh

    5 file:

    1. sudo apt install nodejs

    3

    Now, you can ask NVM which versions of Node are available:

    1. sudo apt install nodejs

    4

    1. sudo apt install nodejs

    5

    It’s a very long list. You can install a version of Node by writing in any of the release versions listed. For instance, to get version v14.10.0, you can run:

    1. sudo apt install nodejs

    6

    You can view the different versions you have installed by listing them:

    1. sudo apt install nodejs

    7

    1. sudo apt install nodejs

    8

    This shows the currently active version on the first line (

    1. nano /tmp/nodesource_setup.sh

    6), followed by some named aliases and the versions that those aliases point to.

    Note: if you also have a version of Node.js installed through

    
    Output
    
    v10.19.0
    

    1, you may receive a

    1. nano /tmp/nodesource_setup.sh

    8 entry here. You can always activate the system-installed version of Node using

    1. nano /tmp/nodesource_setup.sh

    9.

    Additionally, there are aliases for the various long-term support (or LTS) releases of Node:

    1. sudo apt install nodejs

    9

    You can install a release based on these aliases as well. For instance, to install the latest long-term support version,

    1. sudo bash /tmp/nodesource_setup.sh

    0, run the following:

    1. node -v

    0

    1. node -v

    1

    You can switch between installed versions with

    1. sudo bash /tmp/nodesource_setup.sh

    1:

    1. node -v

    2

    1. node -v

    3

    1. node -v

    4

    The correct version of Node is installed on your machine as expected. A compatible version of

    1. sudo apt install npm

    2 is also available.

    You can uninstall Node.js using

    
    Output
    
    v10.19.0
    

    1 or

    
    Output
    
    v10.19.0
    

    5, depending on how it was installed. To remove the version from the system repositories, use

    1. sudo bash /tmp/nodesource_setup.sh

    5:

    1. node -v

    5

    By default,

    1. sudo bash /tmp/nodesource_setup.sh

    5 retains any local configuration files that were created since installation. If you don’t want to save the configuration files for later use, use

    1. sudo bash /tmp/nodesource_setup.sh

    7:

    1. node -v

    6

    To uninstall a version of Node.js that you installed using

    
    Output
    
    v10.19.0
    

    5, first determine whether it is the current active version:

    1. node -v

    7

    If the version you are targeting is not the current active version, you can run:

    1. node -v

    8

    1. node -v

    9

    This command will uninstall the selected version of Node.js.

    If the version you would like to remove is the current active version, you first need to deactivate

    
    Output
    
    v10.19.0
    

    5 to enable your changes:

    
    Output
    
    v10.19.0
    

    0

    Now you can uninstall the current version using the

    1. sudo apt install nodejs

    0 command used previously. This removes all files associated with the targeted version of Node.js.

    There are quite a few ways to get up and running with Node.js on your Ubuntu 20.04 server. Your circumstances will dictate which of the above methods is best for your needs. While using the packaged version in Ubuntu’s repository is one method, using

    
    Output
    
    v10.19.0
    

    5 or a NodeSource PPA offers additional flexibility.

    For more information on programming with Node.js, please refer to our tutorial series How To Code in Node.js.