Programmatic Ponderings
Posts Tagged 14.10
Install Latest Node.js and npm in a Docker Container
Posted by Gary A. Stafford in Bash Scripting, Build Automation, Client-Side Development, DevOps, Enterprise Software Development on November 17, 2014
Install the latest versions of Node.js and npm, into a Docker container, with or without the need for root access. Easily update both applications to the latest versions.
Ubuntu and Node
Recently, I was setting up a new development laptop with Ubuntu 14.10 (Utopic Unicorn). As part of the setup, I needed to install all the several development tools, including Node.js and npm. Researching the current recommendations for installing Node.js and npm on Ubuntu, I found using the traditional ‘apt-get‘ command does not always install the latest versions of either application. Additionally, ‘apt-get’ makes updating those versions difficult.
After a lot of investigation, I created three different snippets of code to install the latest copies of Node.js and npm. Some of my code came from Isaac Z. Schlueter‘s series of installations Gists, and a post on StackOverflow by Pascal Hartig. Joyant and others recommended Isaac’s Gists for installing earlier versions of Node.js and npm. Other code was found in posts by DigitalOcean. Versions are as follows:
- Version 1: using ‘apt-get install’
- Version 2: using curl, make, and npmjs.org’s install script
- Version 3: version 2 without requiring ‘sudo’ to use npm*
*There is some debate on the use of ‘sudo’ with some earlier versions of npm. It appears not to be recommended with the latest versions of npm.
Docker
Docker containers and virtual machines (VM) are ideal platforms for developing and testing applications, locally. I often create a Docker container or VirtualBox VM, to install and test new scripts, before running them within our software environments. To test this code, I created three separate Docker containers, based on the official 14.04 Ubuntu base image, located on Docker Hub. I then executed each version of code within a container. After installation testing, I chose version 2 for my laptop.
GitHub Gists
The three versions of install scripts on gist.github.com, perform the following tasks:
- Creates Docker container
- Updates Ubuntu system packages within container
- Creates new ‘testuser’ account within container (‘testuser’)
- Installs required software to install Node.js, if necessary (curl, make, etc.)
- Installs Node.js and npm
- Installs some common full-stack JavaScript npm packages
- Verifies installation locations and contents correct
############################################################################### | |
# Version 1: using ‘apt-get install’ | |
# Installs using apt-get | |
# Requires update to npm afterwards | |
# Harder to get latest copy of node | |
# Requires sudo to use npm | |
############################################################################### | |
# create new docker ubuntu container | |
sudo docker run -i -t ubuntu /bin/bash # drops you into container as root | |
# update and install all required packages (no sudo required as root) | |
# https://gist.github.com/isaacs/579814#file-only-git-all-the-way-sh | |
apt-get update -yq && apt-get upgrade -yq && \ | |
apt-get install -yq curl git nano | |
# install from nodesource using apt-get | |
# https://www.digitalocean.com/community/tutorials/how-to-install-node-js-on-an-ubuntu-14-04-server | |
curl -sL https://deb.nodesource.com/setup | sudo bash - && \ | |
apt-get install -yq nodejs build-essential | |
# fix npm - not the latest version installed by apt-get | |
npm install -g npm | |
# add user with sudo privileges within Docker container | |
# without adduser input questions | |
# http://askubuntu.com/questions/94060/run-adduser-non-interactively/94067#94067 | |
USER="testuser" && \ | |
adduser --disabled-password --gecos "" $USER && \ | |
sudo usermod -a -G sudo $USER && \ | |
echo "$USER:abc123" | chpasswd && \ | |
su - $USER # switch to testuser | |
# install common full-stack JavaScript packages globally | |
# http://blog.nodejs.org/2011/03/23/npm-1-0-global-vs-local-installation | |
sudo npm install -g yo grunt-cli bower express | |
# optional, check locations and packages are correct | |
which node; node -v; which npm; npm -v; \ | |
npm ls -g --depth=0 |
############################################################################### | |
# Version 2: using curl, make, and npmjs.org’s install script | |
# Installs using make and shell script | |
# Easier to update node and npm versions in future | |
# Requires sudo to use npm | |
############################################################################### | |
# create new docker ubuntu container | |
sudo docker run -i -t ubuntu /bin/bash # drops you into container as root | |
# update and install all required packages (no sudo required as root) | |
# https://gist.github.com/isaacs/579814#file-only-git-all-the-way-sh | |
apt-get update -yq && apt-get upgrade -yq && \ | |
apt-get install -yq g++ libssl-dev apache2-utils curl git python make nano | |
# install latest Node.js and npm | |
# https://gist.github.com/isaacs/579814#file-node-and-npm-in-30-seconds-sh | |
mkdir ~/node-latest-install && cd $_ && \ | |
curl http://nodejs.org/dist/node-latest.tar.gz | tar xz --strip-components=1 && \ | |
make install && \ # takes a few minutes to build... | |
curl https://www.npmjs.org/install.sh | sh | |
# add user with sudo privileges within Docker container | |
# without adduser input questions | |
# http://askubuntu.com/questions/94060/run-adduser-non-interactively/94067#94067 | |
USER="testuser" && \ | |
adduser --disabled-password --gecos "" $USER && \ | |
sudo usermod -a -G sudo $USER && \ | |
echo "$USER:abc123" | chpasswd && \ | |
su - $USER # switch to testuser | |
# install common full-stack JavaScript packages globally | |
# http://blog.nodejs.org/2011/03/23/npm-1-0-global-vs-local-installation | |
sudo npm install -g yo grunt-cli bower express | |
# optional, check locations and packages are correct | |
which node; node -v; which npm; npm -v; \ | |
npm ls -g --depth=0 |
############################################################################### | |
# Version 3: version 2 without requiring ‘sudo’ to use npm | |
# Does NOT require sudo to use npm | |
############################################################################### | |
# create new docker ubuntu container | |
sudo docker run -i -t ubuntu /bin/bash # drops you into container as root | |
# update and install all required packages (no sudo required as root) | |
# https://gist.github.com/isaacs/579814#file-only-git-all-the-way-sh | |
apt-get update -yq && apt-get upgrade -yq && \ | |
apt-get install -yq g++ libssl-dev apache2-utils curl git python make nano | |
# add user with sudo privileges within Docker container | |
# without adduser input questions | |
# http://askubuntu.com/questions/94060/run-adduser-non-interactively/94067#94067 | |
USER="testuser" && \ | |
adduser --disabled-password --gecos "" $USER && \ | |
sudo usermod -a -G sudo $USER && \ | |
echo "$USER:abc123" | chpasswd && \ | |
su - $USER # switch to testuser | |
# setting up npm for global installation without sudo | |
# http://stackoverflow.com/a/19379795/580268 | |
MODULES="local" && \ | |
echo prefix = ~/$MODULES >> ~/.npmrc && \ | |
echo "export PATH=\$HOME/$MODULES/bin:\$PATH" >> ~/.bashrc && \ | |
. ~/.bashrc && \ | |
mkdir ~/$MODULES | |
# install Node.js and npm | |
# https://gist.github.com/isaacs/579814#file-node-and-npm-in-30-seconds-sh | |
mkdir ~/node-latest-install && cd $_ && \ | |
curl http://nodejs.org/dist/node-latest.tar.gz | tar xz --strip-components=1 && \ | |
./configure --prefix=~/$MODULES && \ | |
make install && \ # takes a few minutes to build... | |
curl https://www.npmjs.org/install.sh | sh | |
# install common fullstack JavaScript packages globally | |
npm install -g yo grunt-cli bower express | |
# optional, check locations and packages are correct | |
which node; node -v; which npm; npm -v; \ | |
npm ls -g --depth=0 |
Running Code
14.10, container, docker, Git, JavaScript, Node, Node.js, npm, package, sudo, Ubuntu, utopic unicorn
Recent Posts
- Amazon Managed Workflows for Apache Airflow — Configuration: Understanding Amazon MWAA’s Configuration Options
- Running Spark Jobs on Amazon EMR with Apache Airflow: Using the new Amazon Managed Workflows for Apache Airflow (Amazon MWAA) Service on AWS
- Installing Apache Superset on Amazon EMR: Add data exploration and visualization to your analytics cluster
- Running PySpark Applications on Amazon EMR: Methods for Interacting with PySpark on Amazon Elastic MapReduce
- GTM Stack: Exploring IoT Data Analytics at the Edge with Grafana, Mosquitto, and TimescaleDB on ARM-based Architectures
Top Posts & Pages
- Calling Microsoft SQL Server Stored Procedures from a Java Application Using JDBC
- Event-driven, Serverless Architectures with AWS Lambda, SQS, DynamoDB, and API Gateway
- Running PySpark Applications on Amazon EMR: Methods for Interacting with PySpark on Amazon Elastic MapReduce
- Getting Started with Data Analysis on AWS using AWS Glue, Amazon Athena, and QuickSight: Part 1
- Managing AWS Infrastructure as Code using Ansible, CloudFormation, and CodeBuild
Tweets
- Hanging out with my friend, @BernieSanders on @Zoom today. https://t.co/9WzqM5CLib 13 hours ago
- Using CodeCommit with AWS SSO: 'Federated multi-account access for AWS CodeCommit' aws.amazon.com/blogs/devops/f… via @awscloud 3 days ago
- Myth 1: Cloud vendors know every possible way their customers use their services. Myth 2: Cloud vendors can instant… twitter.com/i/web/status/1… 1 week ago
- Very proud to be an @awscloud employee today. Thank you. techcrunch.com/2021/01/09/ama… 2 weeks ago
- AWS to Azure [ or vice versa] services comparison - Azure Architecture Center | Microsoft Docs. Great tool for comp… twitter.com/i/web/status/1… 3 weeks ago