How to install Node.js on Ubuntu 16.04

Introduction

Node.js is a JavaScript platform for general programming that allows users to quickly build web applications. By leveraging JavaScript on the front and back ends, development can be more consistent and designed in the same system.

In this tutorial, we will show you how to start using Node.js on Ubuntu 16.04 server.

Preparation

How to install the Distro-Stable version for Ubuntu

Ubuntu 16.04 includes a version of Node.js in its default repository, which can be used to easily provide a consistent experience across multiple systems. At the time of writing, the version in the repository is v4.2.6. This is not the latest version, but it should be very stable enough for quick language experiments.

To get this version, we just need to use the apt package manager. We should first refresh the local package index and then install from the repository:

sudo apt-get update
sudo apt-get install nodejs

If the packages in the repository meet your needs, all you need to do is set it up with Node.js. In most cases, you also need to install npm, the Node.js package manager. You can execute by entering the following:

sudo apt-get install npm

This will allow you to easily install modules and packages for use with Node.js.

Due to a conflict with another package, the executable file nodejs in the Ubuntu repository is called instead of node. Please keep this in mind when running the software.

To check which version of Node.js is installed after these initial steps, enter:

nodejs -v

Once you have determined which version of Node.js is installed from the Ubuntu repository, you can decide whether to use a different version, package archive or version manager. Next, we will discuss these elements and more flexible and robust installation methods.

How to install using PPA

An alternative that can provide you with an updated version of Node.js is to add a PPA (Personal Package Archive) maintained by NodeSource. This will have more Node.js versions than the official Ubuntu repositories and will allow you to choose between Node.js v4.x (the old long-term support version, which will be supported until April 2018). Node.js v6.x (support until April 2019) and Node.js v8.x (current LTS version, support until December 2019).

First, you need to install PPA to access its content. Make sure you are in your home directory and use curl to retrieve the installation script for the preferred version, making sure to replace 8.x with the preferred version string (if different):

cd ~
curl -sL https://deb.nodesource.com/setup_8.x -o nodesource_setup.sh

You can use nano (or your favorite text editor) to check the contents of this script:

nano nodesource_setup.sh

Use sudo and run the script:

sudo bash nodesource_setup.sh

The PPA will be added to your configuration, and your local package cache will be automatically updated. After running the installation script from nodesource, you can install the Node.js package as above:

sudo apt-get install nodejs

To check which version of Node.js is installed after these initial steps, enter:

nodejs -v

The output is as follows:

v8.10.0

The nodejs package contains the nodejs binary file npm, so you don't need to install npm separately.

npm uses the configuration file in the home directory to track updates. It will create npm the first time you run it. Execute this command to verify whether npm is installed and create a configuration file:

npm -v

The output is as follows:

5.6.0

In order for certain npm packages to work (for example, to compile code from source), you need to install the build-essential package:

sudo apt-get install build-essential

You now have the necessary tools to process npm packages that need to be compiled from source code.

How to install with NVM

Another way to install Node.js is to use a specially designed tool nvm through apt, which stands for "Node.js Version Manager". Instead of working at the operating system level nvm, work at the independent directory level in the home directory. This means you can install multiple self-contained versions of Node.js without affecting the entire system.

By controlling your environment, nvm you can access the latest version of Node.js and retain and manage previous versions. However, it is a different apt-get utility, and the version of Node.js you manage through it is different from the release version of Node.js available in the Ubuntu repository.

First, we need to get the package from our Ubuntu repository so that we can build the source package. The nvm script will use these tools to build the necessary components:

sudo apt-get update
sudo apt-get install build-essential libssl-dev

After installing the prerequisite software package, you can download the (https://github.com/creationix/nvm) nvm installation script from the [project's GitHub page]. The version number may be different, but in general, you can use curl to download:

curl -sL https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh -o install_nvm.sh

Use nano to check the installation script:

nano install_nvm.sh

bash run the script:

bash install_nvm.sh

It will install the software into a subdirectory of the main directory ~/.nvm. It will also add the necessary lines to your ~/.profile file to use it.

To access the nvm function, you need to log out and log in again, or you can get the ~/.profile file so that the current session can understand the changes:

source ~/.profile

Now that you have installed nvm, you can install the isolated version of Node.js.

To find out the Node.js version available for installation, you can enter:

nvm ls-remote     

The output is as follows:

...
v8.5.0
v8.6.0
v8.7.0
v8.8.0
v8.8.1
v8.9.0   
v8.9.1   
v8.9.2   
v8.9.3->   v8.9.4(Latest LTS: Carbon)

As you can see, the latest LTS version at the time of writing is v8.9.4. You can install it by entering the following command:

nvm install 8.9.4

Normally, nvm will switch to use the most recently installed version. You can explicitly tell nvm to use the version we just downloaded by typing:

nvm use 8.9.4

When installing Node.js using nvm, the executable file node will be called. You can check the version currently used by the shell by typing:

node -v

The output is as follows:

v8.9.4

If you have multiple versions of Node.js, you can view the installed content by entering the following:

nvm ls

If you want to default one of the versions, you can enter:

nvm alias default8.9.4

When a new session is created, this version will be automatically selected. You can also refer to it through the following aliases:

nvm use default

Each version of Node.js keeps track of its own packages, and npm can manage them.

You can use the npm normal format to install the package into the ./node_modules directory of the Node.js project. For example, for the express module:

npm install express

If you want to install it globally (make it use the same Node.js version for other projects to use), you can add the -g flag:

npm install -g express

This will install the package:

~ /.nvm/node_version/lib/node_modules/package_name

Global installation will allow you to run commands from the command line, but you must link the package to the local scope to request it from the program:

npm link express

You can learn more about the options available for nvm by typing:

nvm help

Delete Node.js

You can uninstall Node.js with apt-get or nvm, depending on the version you want to target. To delete the release version, you need apt-get to use the utility at the system level.

To delete the release version, enter the following:

sudo apt-get remove nodejs

This command will delete the package and keep the configuration file. If you plan to install the package again later, these may be useful to you. However, if you do not want to save the configuration file for later use, run the following command:

sudo apt-get purge nodejs

This will uninstall the package and delete the configuration file associated with it. In the last step, you can delete all unused packages that are automatically installed with the removed packages:

sudo apt-get autoremove

To uninstall the Node.js version that has been enabled with nvm, first determine whether the version to be deleted is the currently active version:

nvm current

If the version you want to target** is not** the current active version, you can run:

nvm uninstall node_version

This command will uninstall the selected Node.js version.

If the version you want to delete is** the currently active version, you must first deactivate nvm to enable the changes:

nvm deactivate

You can now uninstall the current version using the uninstall command above, which will delete all files associated with the target version of Node.js, except for the cache files that can be used for reinstallation.

in conclusion

As you can see, there are many ways to get Node.js up and running on an Ubuntu 16.04 server. Your situation will determine which of the above methods is best for your situation. Although the packaged version in the Ubuntu repository is the simplest, the nvm method is definitely more flexible.

To learn more about Linux open source information tutorial, please go to [Tencent Cloud + Community] (https://cloud.tencent.com/developer?from=10680) to learn more.

Reference: "How To Install Node.js on Ubuntu 16.04"

Recommended Posts

How to install Node.js on Ubuntu 16.04
How to install Memcached on Ubuntu 20.04
How to install Java on Ubuntu 20.04
How to install MySQL on Ubuntu 20.04
How to install VirtualBox on Ubuntu 20.04
How to install Elasticsearch on Ubuntu 20.04
How to install Protobuf 3 on Ubuntu
How to install Nginx on Ubuntu 20.04
How to install Apache on Ubuntu 20.04
How to install Git on Ubuntu 20.04
How to install MySQL on Ubuntu 20.04
How to install Vagrant on Ubuntu 20.04
How to install Bacula-Web on Ubuntu 14.04
How to install PostgreSQL on Ubuntu 16.04
How to install Git on Ubuntu 20.04
How to install Anaconda3 on Ubuntu 18.04
How to install Memcached on Ubuntu 18.04
How to install Jenkins on Ubuntu 16.04
How to install MemSQL on Ubuntu 14.04
How to install Go on Ubuntu 20.04
How to install MongoDB on Ubuntu 16.04
How to install Mailpile on Ubuntu 14.04
How to install PrestaShop on Ubuntu 16.04
How to install Skype on Ubuntu 20.04
How to install Jenkins on Ubuntu 20.04
How to install Python 3.8 on Ubuntu 18.04
How to install KVM on Ubuntu 18.04
How to install KVM on Ubuntu 20.04
How to install opencv3.0.0 on ubuntu14.04
How to install Anaconda on Ubuntu 20.04
How to install Prometheus on Ubuntu 16.04
How to install Jenkins on Ubuntu 18.04
How to install Apache on Ubuntu 20.04
How to install R on Ubuntu 20.04
How to install Moodle on Ubuntu 16.04
How to install Solr 5.2.1 on Ubuntu 14.04
How to install Teamviewer on Ubuntu 16.04
How to install MariaDB on Ubuntu 20.04
How to install Nginx on Ubuntu 20.04
How to install Mono on Ubuntu 20.04
How to install Go on Ubuntu 20.04
How to install Zoom on Ubuntu 20.04
How to install Nginx on Ubuntu 16.04
How to install OpenCV on Ubuntu 20.04
How to install Spotify on Ubuntu 20.04
How to install Postman on Ubuntu 18.04
How to install Go 1.6 on Ubuntu 16.04
How to install Go on Ubuntu 18.04
How to install MySQL on Ubuntu 14.04
How to install PostgreSQL on Ubuntu 20.04
How to install VLC on Ubuntu 18.04
How to install TeamViewer on Ubuntu 20.04
How to install Webmin on Ubuntu 20.04
Install nodejs10 on Ubuntu16
How to install Docker Compose on Ubuntu 18.04
How to install Ubuntu on Raspberry Pi
How to install Bacula Server on Ubuntu 14.04
How to install MySQL on Ubuntu 18.04 (linux)
How to install Ubuntu 19.10 on Raspberry Pi 4
How to install Apache Kafka on Ubuntu 18.04
How to install Apache Maven on Ubuntu 20.04