One place for hosting & domains

      How to Install Node.js and Create a Local Development Environment on macOS


      Introduction

      Node.js is an open source JavaScript runtime environment for easily building server-side applications. It’s also the runtime that powers many client-side development tools for modern JavaScript frameworks.

      In this tutorial, you’ll set up a Node.js programming environment on your local macOS machine using Homebrew, and you’ll test your environment out by writing a simple Node.js program.

      Prerequisites

      You will need a macOS computer running High Sierra or higher with administrative access and an internet connection.

      Step 1 — Using the macOS Terminal

      You’ll use the command line to install Node.js and run various commands related to developing Node.js applications. The command line is a non-graphical way to interact with your computer. Instead of clicking buttons with your mouse, you’ll type commands as text and receive text-based feedback. The command line, also known as a shell, lets you automate many tasks you do on your computer daily, and is an essential tool for software developers.

      To access the command line interface, you’ll use the Terminal application provided by macOS. Like any other application, you can find it by going into Finder, navigating to the Applications folder, and then into the Utilities folder. From here, double-click the Terminal application to open it up. Alternatively, you can use Spotlight by holding down the COMMAND key and pressing SPACE to find Terminal by typing it out in the box that appears.

      macOS Terminal

      If you’d like to get comfortable using the command line, take a look at An Introduction to the Linux Terminal. The command line interface on macOS is very similar, and the concepts in that tutorial are directly applicable.

      Now that you have the Terminal running, let’s install some prerequisites we’ll need for Node.js.

      Xcode is an integrated development environment (IDE) that is comprised of software development tools for macOS. You won’t need Xcode to write Node.js programs, but Node.js and some of its components will rely on Xcode’s Command Line Tools package.

      Execute this command in the Terminal to download and install these components:

      You'll be prompted to start the installation, and then prompted again to accept a software license. Then the tools will download and install automatically.

      We're now ready to install the package manager Homebrew, which will let us install the latest version of Node.js.

      Step 3 — Installing and Setting Up Homebrew

      While the command line interface on macOS has a lot of the functionality you'd find in Linux and other Unix systems, it does not ship with a good package manager. A package manager is a collection of software tools that work to automate software installations, configurations, and upgrades. They keep the software they install in a central location and can maintain all software packages on the system in formats that are commonly used. Homebrew is a free and open-source software package managing system that simplifies the installation of software on macOS. We'll use Homebrew to install the most recent version of Node.js.

      To install Homebrew, type this command into your Terminal window:

      • /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

      The command uses curl to download the Homebrew installation script from Homebrew's Git repository on GitHub.

      Let’s walk through the flags that are associated with the curl command:

      • The -f or --fail flag tells the Terminal window to give no HTML document output on server errors.
      • The -s or --silent flag mutes curl so that it does not show the progress meter, and combined with the -S or --show-error flag it will ensure that curl shows an error message if it fails.
      • The -L or --location flag will tell curl to handle redirects. If the server reports that the requested page has moved to a different location, it'll automatically execute the request again using the new location.

      Once curl downloads the script, it's then executed by the Ruby interpreter that ships with macOS, starting the Homebrew installation process.

      The installation script will explain what it will do and will prompt you to confirm that you want to do it. This lets you know exactly what Homebrew is going to do to your system before you let it proceed. It also ensures you have the prerequisites in place before it continues.

      You'll be prompted to enter your password during the process. However, when you type your password, your keystrokes will not display in the Terminal window. This is a security measure and is something you'll see often when prompted for passwords on the command line. Even though you don't see them, your keystrokes are being recorded by the system, so press the RETURN key once you’ve entered your password.

      Press the letter y for “yes” whenever you are prompted to confirm the installation.

      Now let's verify that Homebrew is set up correctly. Execute this command:

      If no updates are required at this time, you'll see this in your Terminal:

      Output

      Your system is ready to brew.

      Otherwise, you may get a warning to run another command such as brew update to ensure that your installation of Homebrew is up to date.

      Now that Homebrew is installed, you can install Node.js.

      Step 4 — Installing Node.js

      With Homebrew installed, you can install a wide range of software and developer tools. We'll use it to install Node.js and its dependencies.

      You can use Homebrew to search for everything you can install with the brew search command, but to provide us with a shorter list, let’s instead search for packages related to Node.js:

      You'll see a list of packages you can install, like this:

      Output

      ==> Formulae node.js nodejs

      Both of these packages install Node.js on your system. They both exist just in case you can't remember if you need to use nodejs or node.js.

      Execute this command to install the nodejs package:

      You'll see output similar to the following in your Terminal. Homebrew will install many dependencies, but will eventually download and install Node.js itself:

      Output

      ==> Installing dependencies for node: icu4c ==> Installing node dependency: icu4c ==> Installing node ==> Downloading https://homebrew.bintray.com/bottles/node-11.0.0.sierra.bottle.tar.gz ######################################################################## 100.0% ==> Pouring node-11.0.0.sierra.bottle.tar.gz ... ==> Summary 🍺 /usr/local/Cellar/node/11.0.0: 3,936 files, 50.1MB

      In addition to Node.js itself, Homebrew installs a few related tools, including npm, which makes it easy to install and update Node.js libraries and packages you might use in your own projects.

      To check the version of Node.js that you installed, type

      This will output the specific version of Node.js that is currently installed, which will by default be the most up-to-date stable version of Node.js that is available.

      Output

      v11.0.0

      Check the version of npm with

      You'll see the version displayed:

      Output

      6.4.1

      You'll use npm to install additional components, libraries, and frameworks.

      To update your version of Node.js, you can first update Homebrew to get the latest list of packages, and then upgrade Node.js itself:

      • brew update
      • brew upgrade nodejs

      Now that Node.js is installed, let's write a program to ensure everything works.

      Step 5 — Creating a Simple Program

      Let's create a simple "Hello, World" program. This will make sure that our environment is working and gets you comfortable creating and running a Node.js program.

      To do this, create a new file called hello.js using nano:

      Type the following code into the file:

      hello.js

      let message = "Hello, World!";
      console.log(message);
      

      Exit the editor by pressing CTRL+X. Then press y when prompted to save the file. You'll be returned to your prompt.

      Now run the program with the following command:

      The program executes and displays its output to the screen:

      Output

      Hello, World!

      This simple program proves that you have a working development environment. You can use this environment to continue exploring Node.js and build larger, more interesting projects.

      Conclusion

      You've successfully installed Node.js, npm, and tested out your setup by creating and running a simple program. You can now use this to develop client-side apps or server-side apps. Take a look at the following tutorials to learn more:



      Source link

      Download a Local Copy of your Linode Backup


      Updated by Linode

      Written by Edward Angert


      Use promo code DOCS10 for $10 credit on a new account.

      The Linode Backups service can create automatic and manual snapshots of your Linode. A completed backup can be directly restored to the origin Linode or to a new Linode in the same data center. These workflows make it easy to revert to a working configuration if you run into any unexpected issues with your software.

      Linode’s backups are stored in a way that is only directly readable by the Linode Backups service. A common question for the service is how you can download the content from your Linode Backups to another storage location, like your home computer. This can be accomplished in two phases:

      1. Restore a backup to a new Linode in the same data center.
      2. Download either specific files or the entire disk image from that Linode, as needed.

      Before You Begin

      Account Permissions and Billing

      Several of the steps in this guide involve adding services to or removing services from a Linode account. Visit our guide on Users and Permissions for more information about restricted Linode users.

      Note that the cost of adding Backups service and adding a Linode to your account is billed, prorated per hour. If the backups service is only enabled for a few hours, you will only be charged for a few hours of the service. See the Backups pricing details for more information. Likewise, when you create a Linode, you will be billed per hour that the Linode exists, whether it is powered on or not.

      The steps in this guide have been designed to minimize the potential costs associated with this process. Additionally, keep the following in mind:

      • Removing a Linode from your account also cancels the associated Backup service for that Linode.
      • A Linode’s backups are deleted when a Linode is deleted.
      • If you choose to leave the Backups service enabled, or if you do not remove the additional Linode from your account, you will be automatically billed. If you only power the Linode off, you will still be billed for it.

      Enable Backups and Take a Snapshot

      These steps are the minimum required for the scope of this guide. Visit our Backups guide for information about how to implement regular backups of your Linode.

      1. Go to your Linode’s dashboard, click Backups, click Enable backups for this Linode » and confirm the additional cost per month.

      2. This guide focuses on saving a snapshot or specific backup. Click Take a New Snapshot Now.

        • The snapshot appears in the Backup History at the bottom of the page.

      Restore from a Backup

      This section shows how to restore a backup to a new Linode, or to an existing Linode.

      Restoring a backup will create a new configuration profile and a new set of disks on your Linode. The restore process does not restore single files or directories automatically. Restoring particular files can be done by completing a normal restore, copying the files off of the new disks, and then removing the disks afterward.

      Note

      The size of the disk(s) created by the restore process will only be slightly larger than the total size of the files restored. This means that the disk(s) created will be ‘full’.

      Some applications, like databases, need some amount of free unused space inside the disk in order to run. As a result, you may want to increase your disk(s) size after the restore process is completed.

      To restore a backup to a different data center, first restore to a Linode in the same data center, creating a new one if necessary. Once the restore is complete, use the Clone tab to copy the disk(s) to a Linode in a different data center.

      Restore to a New Linode

      You can restore a backup to any Linode located in the same data center, even if the target does not have the Backup Service enabled. This section covers how to restore a backup to a new Linode that does not have any disks deployed to it. If you wish to restore your backup to an existing Linode, see the next section.

      1. From the Dashboard of the Linode whose backups you intend to restore, click on the Backups tab. Select the Restore to… link beneath the backup version that you want to restore.

      2. Under the Select column, click the Restore to this Linode link next to your new Linode:

        The backup disks and configuration profiles will be restored to the Linode you selected. Watch the Host Job Queue to monitor the progress. Restoring from a backup can take several minutes depending on the size of your Linode and the amount of data you have stored on it.

      Restore to an Existing Linode

      To restore a backup to an existing Linode, you will need to make sure that you have enough storage space that is not currently assigned to disk images.

      Note

      If you are attempting to restore a disk to the same Linode the backup was created from, the restoration process will not delete the original disk for you. Manually delete the original disk to make room for the backup.

      1. Start by confirming the size of the backup that you wish to restore. From the Backups tab in your Linode’s Dashboard, click the Restore to… link beneath your desired backup version.

      2. Check the Total size required field to confirm the size of your backup.

        As an example, if the total size of the backup comes to 3107MB, this means you would need at least that much free space to restore the backup to your Linode.

      3. Next, you’ll confirm the total space assigned to disk images on your Linode, via the Storage indicator on your Linode’s Dashboard.

      4. If the amount of space available is greater than the size of the backup, you can proceed with restoring. If the amount of unallocated space is less than the size of the backup, you can resize your existing disks to make room for it.

        Note

        In some cases, you will not be able to shrink your disks enough to fit the restored backup. As an alternative, you can change your Linode’s plan to a higher tier that offers more disk space.
      5. Once the disk resize has completed, check the storage indicator on your Linode’s Dashboard to confirm that you’ve freed up enough space for your backup.

      6. From the Backups tab, click the Restore to this Linode link next to your Linode.

        Your backup will begin restoring to your Linode, and you can monitor its progress from the Host Job Queue in your Linode’s Dashboard tab. Note that the time it takes to restore your backup will vary depending upon the restore size, and the number of files being restored.

      Download Specific Files or Directories over SSH

      If you just need specific files from your Linode, you can download those over SSH. In order to do so, you’ll first need to reboot your Linode under the new configuration profile that was created by the restore process. This new profile is assigned to the restored disks, and your backed up data will be accessible when you boot from them.

      Downloading files over SSH can be done at a command-line interface, or with a graphical SFTP file browser.

      Using SCP

      To retrieve a specific directory or file via the command-line, you can use the secure copy (SCP) command from your computer. SCP is installed by default on most Mac and Linux systems, and you can install a tool like Cygwin to use it on Windows.

      • The syntax for using SCP to copy a file from your Linode into a directory on your computer is:

        scp your_linode_username@your_linode_ip:/path/to/your/file.txt /path/to/your/local/directory/
        

        The file will be saved inside /path/to/your/local/directory/ on your computer.

      • To copy a file from your Linode to your computer and give it a specific name (in this case, file.txt.backup):

        scp your_linode_username@your_linode_ip:/path/to/your/file.txt /path/to/your/local/directory/file.txt.backup
        
      • To copy a directory from your Linode to your computer:

        scp -r your_linode_username@your_linode_ip:/path/to/your/directory /path/to/your/local/directory
        

        If /path/to/your/local/directory already exists on your computer, then the copied directory will be placed inside /path/to/your/local/directory (i.e. /path/to/your/local/directory/directory).

        If /path/to/your/local/directory does not already exist, then the copied directory will be created with that name.

      For example:

      • Download an NGINX configuration file to your home folder:

        scp your_linode_username@your_linode_ip:/etc/nginx/conf.d/example.com.conf ~/example.com.conf.backup
        
      • Download an Apache configuration file to your home folder:

        scp your_linode_username@your_linode_ip:/etc/apache2/sites-available/example.com.conf ~/example.com.conf.backup
        
      • Copy the entire document root from a web server:

        scp -r your_linode_username@your_linode_ip:/var/www/html/ ~/html_backup
        

      If you intend to repeat this process regularly, consider using rsync to create additional local copies of your data. rsync is capable of performing incremental file copies, which means you do not have to fully transfer each file every time you download your data.

      Using FileZilla

      As an alternative to the command-line, you can download and install an SFTP client. These applications provide a graphical user interface for your Linode’s filesystem.

      FileZilla is a popular free example. Windows and OS X users can download FileZilla here. To install FileZilla on Linux:

      After you’ve installed FileZilla on your computer:

      1. Open FileZilla from your Windows start menu, OS X Launchpad, or the launcher provided by your Linux distribution of choice.

      2. Enter your Linode’s IP address or domain name in the Host field. Enter the account username you wish to connect as in the Username field. Please note that this must be a user account on your Linode; if in doubt, enter root to log in as the root user. Enter the account’s password in the Password field, and enter “22” in the Port field. Click Quickconnect to initiate the file transfer session.

        Quickconnect

      3. If this is the first time you’ve connected to your Linode with an SSH or SFTP program, you’ll receive a warning that the host key is unknown. Place a check mark in the box next to Always trust this host, add this key to the cache. Checking this box prevents further warnings unless the key presented to FileZilla changes; this should only happen if you reinstall the remote server’s operating system.

        Unknown Key

      4. Click the OK button to proceed. You’ll be presented with a split view, with your local filesystem on the left and your Linode’s filesystem on the right. You may transfer files by dragging and dropping them between each side.

      For more information on FileZilla, review our full guide on using the application.

      Using mysqldump to Back Up a Database

      Special care is needed when downloading data from a database. Before it can be downloaded, the data in a database needs to first be dumped to a file. This file can then be transferred just as any other normal file type.

      The mysqldump command’s general syntax is:

      mysqldump -u [username] -p [databaseName] > [filename]-$(date +%F).sql
      
      • mysqldump prompts for a password before it starts the backup process.
      • Depending on the size of the database, it could take a while to complete.
      • The database backup will be created in the directory the command is run.
      • -$(date +%F) adds a timestamp to the filename.

      Example use cases include:

      • Create a backup of an entire Database Management System (DBMS):

        mysqldump --all-databases --single-transaction --quick --lock-tables=false > full-backup-$(date +%F).sql -u root -p
        
      • Back up a specific database. Replace db1 with the name of the database you want to back up:

        mysqldump -u username -p db1 --single-transaction --quick --lock-tables=false > db1-backup-$(date +%F).sql
        
      • Back up a single table from any database. In the example below, table1 is exported from the database db1:

        mysqldump -u username -p --single-transaction --quick --lock-tables=false db1 table1 > db1-table1-$(date +%F).sql
        

      Here’s a breakdown of the mysqldump command options used above:

      • --single-transaction: Issue a BEGIN SQL statement before dumping data from the server.
      • --quick: Enforce dumping tables row by row. This provides added safety for systems with little RAM and/or large databases where storing tables in memory could become problematic.
      • --lock-tables=false: Do not lock tables for the backup session.

      For more information on MySQL database backups, including how to restore the data in a dump file to a MySQL installation, review our guide on the subject. An alternative to using mysqldump is to create physical backups. It’s also possible to backup PostgreSQL databases.

      Download a Disk over SSH

      Downloading your disk will copy a .img file to your computer that encapsulates all of the data that is on your Linode’s disk.

      Prepare the Receiving Computer

      Verify that the receiving computer has SSH installed. (Most Linux/Unix-like systems have it installed by default.) If you’re running Windows locally, you may wish to set up the Cygwin compatibility layer to provide a reasonably complete Unix-like environment. Instructions on setting up Cygwin are located here.

      Start Your Linode in Rescue Mode

      Before you initiate the transfer, start the source Linode in Rescue Mode and start SSH by following these guides:

      1. Start your Linode in Rescue Mode.
      2. Connecting to a Linode Running in Rescue Mode via LISH.
      3. Start the SSH server on your Linode.

      Copy the Disk

      Now that the Linode is running in Rescue Mode, you can transfer the disk from the Linode to the receiving machine over SSH:

      1. Enter the following command on the receiving machine. Replace 123.45.67.89 with the Linode’s IP address and /home/archive/linode.img with the path where you want to store the disk:

        ssh root@123.45.67.89 "dd if=/dev/sda " | dd of=/home/archive/linode.img
        

        Note

        The device /dev/sda is used for Linodes running on top of KVM. If your Linode is still using XEN, then throughout this guide you must use /dev/xvda instead.

      2. The receiving machine will connect to the Linode. Type yes and press Enter to continue connecting:

        The authenticity of host '123.45.67.89 (123.45.67.89)' can't be established.
        RSA key fingerprint is 39:6b:eb:05:f1:28:95:f0:da:63:17:9e:6b:6b:11:4a.
        Are you sure you want to continue connecting (yes/no)? yes
        
      3. Enter the root password for the Linode:

        Warning: Permanently added '123.45.67.89' (RSA) to the list of known hosts.
        root@123.45.67.89's password:
        

        The transfer starts, and you’ll see output similar to the following:

          
                4096000+0 records in
                4096000+0 records out
                2097152000 bytes (2.1 GB) copied, 371.632 seconds, 5.6 MB/s
                4096000+0 records in
                4096000+0 records out
                2097152000 bytes (2.1 GB) copied, 364.002 s, 5.8 MB/s
            
        

        Note

        Copying your disk can take a while. Please be patient. If you have a slow internet connection, add the -C option to the SSH command; this enables gzip compression for data transfer. If you receive a Write failed: Broken pipe error, repeat this process.

      Verify the Disk

      Once the copy has completed, you can verify it by mounting the image on the receiving machine.

      1. Log in to the receiving machine as root by entering the following command and entering the root user’s password:

        su
        
      2. Make a directory on the receiving machine by entering the following command:

        mkdir linode
        
      3. Mount the disk by entering the following command, replacing linode.img with the name of the disk:

        mount -o loop linode.img linode
        
      4. View the directories stored on the disk by entering the following command:

        ls linode/
        

        You should see the directories on the disks, similar to the ones shown below, indicating that everything has transferred:

        bin   dev  home  lost+found  mnt  proc  sbin     srv  tmp  var
        boot  etc  lib   media       opt  root  selinux  sys  usr
        

      Clean Up after Your Download

      After you’ve finished downloading your files or disks, you can optionally delete the restored disks. If you created a new Linode to perform the restore, consider deleting the Linode. As a reminder, billing for that Linode will continue automatically if you do not remove it. If you only power the Linode off, you will still be billed for it.

      Join our Community

      Find answers, ask questions, and help others.

      This guide is published under a CC BY-ND 4.0 license.



      Source link