{"id":2213,"date":"2016-07-13T16:32:27","date_gmt":"2016-07-13T20:32:27","guid":{"rendered":"https:\/\/www.globo.tech\/learning-center\/?p=2213"},"modified":"2017-12-12T15:45:18","modified_gmt":"2017-12-12T20:45:18","slug":"install-nodejs-ubuntu-16","status":"publish","type":"post","link":"https:\/\/www.globo.tech\/learning-center\/install-nodejs-ubuntu-16\/","title":{"rendered":"How to install NodeJS and run node applications on Ubuntu 16"},"content":{"rendered":"<h1>How to install NodeJS and run node applications on Ubuntu 16<\/h1>\n<p>NodeJS is a open source runtime environment for Javascript server-side and networking applications that is designed to be flexible and easily scalable. NodeJS comes with a package manager known as NPM and can be installed via its own version manager, NVM, which can even manage multiple node versions.<\/p>\n<p>In this tutorial, we will cover the setup of NodeJS on an Ubuntu instance and the writing of a basic NodeJS application.<\/p>\n<h2>Getting Started<\/h2>\n<p>To complete this guide, you will need the following:<br \/>\n\u2022 1 Node (<a href=\"https:\/\/www.globo.tech\/cloud-server-pricing\" target=\"_blank\">Cloud Server<\/a> or <a href=\"http:\/\/www.globo.tech\/dedicated-server-hosting\" target=\"_blank\">Dedicated Server<\/a>) running Ubuntu 16. <\/p>\n<p>Install should be made as root, however we do not recommend running NodeJS applications in production as root. You may create an user to run the applications from to increase security on your infrastructure.<\/p>\n<h2>Updating Ubuntu<\/h2>\n<p>Make sure your Ubuntu instance is up to date by running the following commands as root or with superuser privileges. To begin, fetch the latest information available about new versions of packages and their dependencies:<\/p>\n<p><code>sudo apt-get update<\/code><\/p>\n<p>With the new information, install the latest versions of packages currently installed on the system. Enter y for yes when prompted and wait for completion:<\/p>\n<p><code>sudo apt-get upgrade<\/code><\/p>\n<h2>Install NodeJS<\/h2>\n<p>There are several possibilities for how to install NodeJS on your system. Make sure you have updated your packages before proceeding (see Updating Ubuntu section).<\/p>\n<h2>Option 1: Beginner Method with apt-get<\/h2>\n<p>Install stable (but not necessarily the latest) releases of NodeJS and NPM from Ubuntu&#8217;s own repositories:<\/p>\n<p><code>sudo apt-get install nodejs<br \/>\nsudo apt-get install npm<\/code><\/p>\n<h2>Option 2: Advanced Method with NVM<\/h2>\n<p>NVM is the Node Version Manager and can be used to install NodeJS and NPM.<\/p>\n<p>NVM requires a c++ compiler as well as the version control system Git. Install these NVM dependencies with source packages from the Ubuntu repositories if they are not already present:<\/p>\n<p><code>apt-get install build-essential libssl-dev<br \/>\napt-get install git<\/code><\/p>\n<p>Check what the latest NVM version released is from the NVM GitHub project page. Fetch and execute the NVM install script below, replacing the version number with your desired or latest version:<\/p>\n<p><code>curl -o- https:\/\/raw.githubusercontent.com\/creationix\/nvm\/v0.31.2\/install.sh | bash<\/code><\/p>\n<p>For the NVM setup to be complete, either restart the terminal, switch users, or refresh the profile. Change user using this command, replacing USER2 with your second user:<\/p>\n<p><code>su USER2<\/code><\/p>\n<p>Input the second user&#8217;s password, then exit to return to the first user:<\/p>\n<p><code>exit<\/code><\/p>\n<p>You can also simply refresh the profile instead of switching users:<\/p>\n<p><code>source ~\/.profile<\/code><\/p>\n<p>Verify NVM is installed and list all versions of NodeJS that are available:<\/p>\n<p><code>nvm --version<br \/>\nnvm ls-remote<\/code><\/p>\n<p>The output of the second command will contain quite a few versions of NodeJS (basically each and every release). As of the time of this guide, the current latest version is 6.2.2. We recommend you opt for this version unless your application requires an older deprecated version.<\/p>\n<p>Expected output:<\/p>\n<p><code class=\"gris\">  v0.1.14<br \/>\n  v0.1.15<br \/>\n  v0.1.16<br \/>\n  ...<\/code><\/p>\n<p>Install your desired version of NodeJS from the NVM list output:<\/p>\n<p><code>nvm install 6.2.2<\/code><\/p>\n<p>Confirm the installation succeeded by checking all installed versions of NodeJS on your system:<\/p>\n<p><code>nvm ls<\/code><\/p>\n<p>The output should be similar to this:<\/p>\n<p><code class=\"gris\">   v6.2.1<br \/>\n->  v6.2.2<br \/>\nnode -> stable (-> v6.2.2) (default)<br \/>\nstable -> 6.2 (-> v6.2.2) (default)<br \/>\niojs -> iojs- (-> N\/A) (default)<\/code><\/p>\n<p>Activate an installed NodeJS version with:<\/p>\n<p><code>nvm use 6.2.2<\/code><\/p>\n<p>Verify that your desired version of NodeJS is activated:<\/p>\n<p><code>nvm current<\/code><\/p>\n<h2>Deploying a Basic NodeJS Application<\/h2>\n<p>For the purposes of this guide, we will write a small NodeJS script using the express library to spawn a quick web server with some text.<\/p>\n<p>Create a folder for your NodeJS application and navigate inside it:<\/p>\n<p><code>mkdir my_first_node_app<br \/>\ncd my_first_node_app<\/code><\/p>\n<p>Create your NodeJS script using the terminal text editor vi:<\/p>\n<p><code>vi hello.js<\/code><\/p>\n<p>Within this new file, insert the following code:<\/p>\n<p><code class=\"gris\">var express = require('express');<br \/>\nvar app = express();<br \/>\napp.get('\/', function (req, res) {<br \/>\n  res.writeHead(200, {'Content-Type': 'text\/plain'});<br \/>\n  res.send('Globo.Tech says Hello!\\n');<br \/>\n})<br \/>\nvar server = app.listen(8080, function () {<br \/>\n  console.log(\u2018Server is up and running\u2019)<br \/>\n})<\/code><\/p>\n<p>Save the file and exit with Esc > :<em>wq!<\/em><\/p>\n<p>Install the Express library inside your application folder to satisfy dependencies:<\/p>\n<p><code>npm install express --save<\/code><\/p>\n<p>Confirm your application works by checking for the following console message after running this command:<\/p>\n<p><code>curl http:\/\/localhost:8080<\/code><\/p>\n<p>You should see &#8220;Globo.Tech says Hello!&#8221; in the console.<\/p>\n<p>This default app is basically a web server so you can visit the URL and port configured and see the text specified in the script as a webpage.<\/p>\n<p>Run your newly created NodeJS application (this command won&#8217;t exit until you quit it):<\/p>\n<p><code>node hello.js<\/code><\/p>\n<p>Then, navigate to the following url in the browser to see your app:<\/p>\n<p><code class=\"gris\"><a href=\"http:\/\/localhost:8080\/\">http:\/\/localhost:8080\/<\/a><\/code><\/p>\n<p>If you see &#8220;<em>Globo.Tech says Hello!<\/em>&#8221; your NodeJS setup works properly.<\/p>\n<h2>Conclusion<\/h2>\n<p>Congratulations! Now you know how to use NodeJS and run basic node applications. If this guide was helpful to you, kindly share it with others who may also be interested. <\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>How to install NodeJS and run node applications on Ubuntu 16 NodeJS is a open source runtime environment for Javascript server-side and networking applications that is designed to be flexible and easily scalable. NodeJS comes with a package manager known as NPM and can be installed via its own version manager, NVM, which can even<!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[71],"tags":[],"class_list":["post-2213","post","type-post","status-publish","format-standard","hentry","category-applications"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.1 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to install NodeJS and run node applications on Ubuntu 16 - Globo.Tech<\/title>\n<meta name=\"description\" content=\"This tutorial will show you how to install NodeJS to run node applications on your Ubuntu 16 server. Read now &amp; Start building network applications!\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.globo.tech\/learning-center\/install-nodejs-ubuntu-16\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to install NodeJS and run node applications on Ubuntu 16 - Globo.Tech\" \/>\n<meta property=\"og:description\" content=\"This tutorial will show you how to install NodeJS to run node applications on your Ubuntu 16 server. Read now &amp; Start building network applications!\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.globo.tech\/learning-center\/install-nodejs-ubuntu-16\/\" \/>\n<meta property=\"og:site_name\" content=\"Globo.Tech\" \/>\n<meta property=\"article:published_time\" content=\"2016-07-13T20:32:27+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2017-12-12T20:45:18+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.globo.tech\/learning-center\/wp-content\/uploads\/2016\/09\/GloboTech-Logo.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1920\" \/>\n\t<meta property=\"og:image:height\" content=\"963\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"GloboTech Communications\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"GloboTech Communications\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.globo.tech\\\/learning-center\\\/install-nodejs-ubuntu-16\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.globo.tech\\\/learning-center\\\/install-nodejs-ubuntu-16\\\/\"},\"author\":{\"name\":\"GloboTech Communications\",\"@id\":\"https:\\\/\\\/www.globo.tech\\\/learning-center\\\/#\\\/schema\\\/person\\\/e17784b37f4a4f49b7bc611847912e87\"},\"headline\":\"How to install NodeJS and run node applications on Ubuntu 16\",\"datePublished\":\"2016-07-13T20:32:27+00:00\",\"dateModified\":\"2017-12-12T20:45:18+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.globo.tech\\\/learning-center\\\/install-nodejs-ubuntu-16\\\/\"},\"wordCount\":700,\"commentCount\":0,\"articleSection\":[\"Applications\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.globo.tech\\\/learning-center\\\/install-nodejs-ubuntu-16\\\/\",\"url\":\"https:\\\/\\\/www.globo.tech\\\/learning-center\\\/install-nodejs-ubuntu-16\\\/\",\"name\":\"How to install NodeJS and run node applications on Ubuntu 16 - Globo.Tech\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.globo.tech\\\/learning-center\\\/#website\"},\"datePublished\":\"2016-07-13T20:32:27+00:00\",\"dateModified\":\"2017-12-12T20:45:18+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.globo.tech\\\/learning-center\\\/#\\\/schema\\\/person\\\/e17784b37f4a4f49b7bc611847912e87\"},\"description\":\"This tutorial will show you how to install NodeJS to run node applications on your Ubuntu 16 server. Read now & Start building network applications!\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.globo.tech\\\/learning-center\\\/install-nodejs-ubuntu-16\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.globo.tech\\\/learning-center\\\/install-nodejs-ubuntu-16\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.globo.tech\\\/learning-center\\\/install-nodejs-ubuntu-16\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.globo.tech\\\/learning-center\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to install NodeJS and run node applications on Ubuntu 16\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.globo.tech\\\/learning-center\\\/#website\",\"url\":\"https:\\\/\\\/www.globo.tech\\\/learning-center\\\/\",\"name\":\"Globo.Tech\",\"description\":\"Welcome to the Official Globo.Tech Learning Center\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.globo.tech\\\/learning-center\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.globo.tech\\\/learning-center\\\/#\\\/schema\\\/person\\\/e17784b37f4a4f49b7bc611847912e87\",\"name\":\"GloboTech Communications\",\"sameAs\":[\"http:\\\/\\\/www.gtcomm.net\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to install NodeJS and run node applications on Ubuntu 16 - Globo.Tech","description":"This tutorial will show you how to install NodeJS to run node applications on your Ubuntu 16 server. Read now & Start building network applications!","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.globo.tech\/learning-center\/install-nodejs-ubuntu-16\/","og_locale":"en_US","og_type":"article","og_title":"How to install NodeJS and run node applications on Ubuntu 16 - Globo.Tech","og_description":"This tutorial will show you how to install NodeJS to run node applications on your Ubuntu 16 server. Read now & Start building network applications!","og_url":"https:\/\/www.globo.tech\/learning-center\/install-nodejs-ubuntu-16\/","og_site_name":"Globo.Tech","article_published_time":"2016-07-13T20:32:27+00:00","article_modified_time":"2017-12-12T20:45:18+00:00","og_image":[{"width":1920,"height":963,"url":"https:\/\/www.globo.tech\/learning-center\/wp-content\/uploads\/2016\/09\/GloboTech-Logo.png","type":"image\/png"}],"author":"GloboTech Communications","twitter_misc":{"Written by":"GloboTech Communications","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.globo.tech\/learning-center\/install-nodejs-ubuntu-16\/#article","isPartOf":{"@id":"https:\/\/www.globo.tech\/learning-center\/install-nodejs-ubuntu-16\/"},"author":{"name":"GloboTech Communications","@id":"https:\/\/www.globo.tech\/learning-center\/#\/schema\/person\/e17784b37f4a4f49b7bc611847912e87"},"headline":"How to install NodeJS and run node applications on Ubuntu 16","datePublished":"2016-07-13T20:32:27+00:00","dateModified":"2017-12-12T20:45:18+00:00","mainEntityOfPage":{"@id":"https:\/\/www.globo.tech\/learning-center\/install-nodejs-ubuntu-16\/"},"wordCount":700,"commentCount":0,"articleSection":["Applications"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.globo.tech\/learning-center\/install-nodejs-ubuntu-16\/","url":"https:\/\/www.globo.tech\/learning-center\/install-nodejs-ubuntu-16\/","name":"How to install NodeJS and run node applications on Ubuntu 16 - Globo.Tech","isPartOf":{"@id":"https:\/\/www.globo.tech\/learning-center\/#website"},"datePublished":"2016-07-13T20:32:27+00:00","dateModified":"2017-12-12T20:45:18+00:00","author":{"@id":"https:\/\/www.globo.tech\/learning-center\/#\/schema\/person\/e17784b37f4a4f49b7bc611847912e87"},"description":"This tutorial will show you how to install NodeJS to run node applications on your Ubuntu 16 server. Read now & Start building network applications!","breadcrumb":{"@id":"https:\/\/www.globo.tech\/learning-center\/install-nodejs-ubuntu-16\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.globo.tech\/learning-center\/install-nodejs-ubuntu-16\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.globo.tech\/learning-center\/install-nodejs-ubuntu-16\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.globo.tech\/learning-center\/"},{"@type":"ListItem","position":2,"name":"How to install NodeJS and run node applications on Ubuntu 16"}]},{"@type":"WebSite","@id":"https:\/\/www.globo.tech\/learning-center\/#website","url":"https:\/\/www.globo.tech\/learning-center\/","name":"Globo.Tech","description":"Welcome to the Official Globo.Tech Learning Center","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.globo.tech\/learning-center\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.globo.tech\/learning-center\/#\/schema\/person\/e17784b37f4a4f49b7bc611847912e87","name":"GloboTech Communications","sameAs":["http:\/\/www.gtcomm.net"]}]}},"_links":{"self":[{"href":"https:\/\/www.globo.tech\/learning-center\/wp-json\/wp\/v2\/posts\/2213","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.globo.tech\/learning-center\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.globo.tech\/learning-center\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.globo.tech\/learning-center\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.globo.tech\/learning-center\/wp-json\/wp\/v2\/comments?post=2213"}],"version-history":[{"count":6,"href":"https:\/\/www.globo.tech\/learning-center\/wp-json\/wp\/v2\/posts\/2213\/revisions"}],"predecessor-version":[{"id":3986,"href":"https:\/\/www.globo.tech\/learning-center\/wp-json\/wp\/v2\/posts\/2213\/revisions\/3986"}],"wp:attachment":[{"href":"https:\/\/www.globo.tech\/learning-center\/wp-json\/wp\/v2\/media?parent=2213"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.globo.tech\/learning-center\/wp-json\/wp\/v2\/categories?post=2213"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.globo.tech\/learning-center\/wp-json\/wp\/v2\/tags?post=2213"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}