One place for hosting & domains

      Local

      How To Install Go and Set Up a Local Programming Environment on Ubuntu 18.04


      Introduction

      Go is a programming language that was born out of frustration at Google. Developers continually had to pick a language that executed efficiently but took a long time to compile, or to pick a language that was easy to program but ran inefficiently in production. Go was designed to have all three available at the same time: fast compilation, ease of programming, and efficient execution in production.

      While Go is a versatile programming language that can be used for many different programming projects, it’s particularly well suited for networking/distributed systems programs, and has earned a reputation as “the language of the cloud”. It focuses on helping the modern programmer do more with a strong set of tooling, removing debates over formatting by making the format part of the language specification, as well as making deployment easy by compiling to a single binary. Go is easy to learn, with a very small set of keywords, which makes it a great choice for beginners and experienced developers alike.

      This tutorial will guide you through installing and configuring a programming workspace with Go via the command line. This tutorial will explicitly cover the installation procedure for Ubuntu 18.04, but the general principles can apply to other Debian Linux distributions.

      Prerequisites

      You will need a computer or virtual machine with Ubuntu 18.04 installed, as well as have administrative access to that machine and an internet connection. You can download this operating system via the Ubuntu 18.04 releases page.

      Step 1 — Setting Up Go

      In this step, you’ll install Go by downloading the current release from the official Go downloads page.

      To do this, you’ll want to find the URL for the current binary release tarball. You will also want to note the SHA256 hash listed next to it, as you’ll use this hash to verify the downloaded file.

      You’ll be completing the installation and setup on the command line, which is a non-graphical way to interact with your computer. That is, instead of clicking on buttons, you’ll be typing in text and receiving feedback from your computer through text as well.

      The command line, also known as a shell or terminal, can help you modify and automate many of the tasks you do on a computer every day, and is an essential tool for software developers. There are many terminal commands to learn that can enable you to do more powerful things. For more information about the command line, check out the Introduction to the Linux Terminal tutorial.

      On Ubuntu 18.04, you can find the Terminal application by clicking on the Ubuntu icon in the upper-left hand corner of your screen and typing terminal into the search bar. Click on the Terminal application icon to open it. Alternatively, you can hit the CTRL, ALT, and T keys on your keyboard at the same time to open the Terminal application automatically.

      Ubuntu Terminal

      Once the terminal is open, you will manually install the Go binaries. While you could use a package manager, such as apt-get, walking through the manual installation steps will help you understand any configuration changes to your system that are needed to have a valid Go workspace.

      Before downloading Go, make sure that you are in the home (~) directory:

      Use curl to retrieve the tarball URL that you copied from the official Go downloads page:

      • curl -O https://dl.google.com/go/go1.12.1.linux-amd64.tar.gz

      Next, use sha256sum to verify the tarball:

      • sha256sum go1.12.1.linux-amd64.tar.gz

      The hash that is displayed from running the above command should match the hash that was on the downloads page. If it does not, then this is not a valid file and you should download the file again.

      Output

      2a3fdabf665496a0db5f41ec6af7a9b15a49fbe71a85a50ca38b1f13a103aeec go1.12.1.linux-amd64.tar.gz

      Next, extract the downloaded archive and install it to the desired location on the system. It's considered best practice to keep it under /usr/local:

      • sudo tar -xvf go1.12.1.linux-amd64.tar.gz -C /usr/local

      You will now have a directory called go in the /usr/local directory. Next, recursively change this directory's owner and group to root:

      • sudo chown -R root:root /usr/local/go

      This will secure all the files and ensure that only the root user can run the Go binaries.

      Note: Although /usr/local/go is the officially-recommended location, some users may prefer or require different paths.

      In this step, you downloaded and installed Go on your Ubuntu 18.04 machine. In the next step you will configure your Go workspace.

      Step 2 — Creating Your Go Workspace

      You can create your programming workspace now that Go is installed. The Go workspace will contain two directories at its root:

      • src: The directory that contains Go source files. A source file is a file that you write using the Go programming language. Source files are used by the Go compiler to create an executable binary file.
      • bin: The directory that contains executables built and installed by the Go tools. Executables are binary files that run on your system and execute tasks. These are typically the programs compiled by your source code or other downloaded Go source code.

      The src subdirectory may contain multiple version control repositories (such as Git, Mercurial, and Bazaar). This allows for a canonical import of code in your project. Canonical imports are imports that reference a fully qualified package, such as github.com/digitalocean/godo.

      You will see directories like github.com, golang.org, or others when your program imports third party libraries. If you are using a code repository like github.com, you will also put your projects and source files under that directory. We will explore this concept later in this step.

      Here is what a typical workspace may look like:

      .
      ├── bin
      │   ├── buffalo                                      # command executable
      │   ├── dlv                                          # command executable
      │   └── packr                                        # command executable
      └── src
          └── github.com
              └── digitalocean
                  └── godo
                      ├── .git                            # Git repository metadata
                      ├── account.go                      # package source
                      ├── account_test.go                 # test source
                      ├── ...
                      ├── timestamp.go
                      ├── timestamp_test.go
                      └── util
                          ├── droplet.go
                          └── droplet_test.go
      

      The default directory for the Go workspace as of 1.8 is your user's home directory with a go subdirectory, or $HOME/go. If you are using an earlier version of Go than 1.8, it is still considered best practice to use the $HOME/go location for your workspace.

      Issue the following command to create the directory structure for your Go workspace:

      • mkdir -p $HOME/go/{bin,src}

      The -p option tells mkdir to create all parents in the directory, even if they don't currently exist. Using {bin,src} creates a set of arguments to mkdir and tells it to create both the bin directory and the src directory.

      This will ensure the following directory structure is now in place:

      └── $HOME
          └── go
              ├── bin
              └── src
      

      Prior to Go 1.8, it was required to set a local environment variable called $GOPATH. $GOPATH told the compiler where to find imported third party source code, as well as any local source code you had written. While it is no longer explicitly required, it is still considered a good practice as many third party tools still depend on this variable being set.

      You can set your $GOPATH by adding the global variables to your ~/.profile. You may want to add this into .zshrc or .bashrc file as per your shell configuration.

      First, open ~/.profile with nano or your preferred text editor:

      Set your $GOPATH by adding the following to the file:

      ~/.profile

      export GOPATH=$HOME/go
      

      When Go compiles and installs tools, it will put them in the $GOPATH/bin directory. For convenience, it's common to add the workspace's /bin subdirectory to your PATH in your ~/.profile:

      ~/.profile

      export PATH=$PATH:$GOPATH/bin
      

      This will allow you to run any programs you compile or download via the Go tools anywhere on your system.

      Finally, you need to add the go binary to your PATH. You can do this by adding /usr/local/go/bin to the end of the line:

      ~/.profile

      export PATH=$PATH:$GOPATH/bin:/usr/local/go/bin
      

      Adding /user/local/go/bin to your $PATH makes all of the Go tools available anywhere on your system.

      To update your shell, issue the following command to load the global variables:

      You can verify your $PATH is updated by using the echo command and inspecting the output:

      You will see your $GOPATH/bin which will show up in your home directory. If you are logged in as root, you would see root/go/bin in the path.

      Output

      /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/root/go/bin:/usr/local/go/bin

      You will also see the path to the Go tools for /usr/local/go/bin:

      Output

      /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/root/go/bin:/usr/local/go/bin

      Verify the installation by checking the current version of Go:

      And we should receive output like this:

      Output

      go version go1.12.1 linux/amd64

      Now that you have the root of the workspace created and your $GOPATH environment variable set, you can create your future projects with the following directory structure. This example assumes you are using github.com as your repository:

      $GOPATH/src/github.com/username/project
      

      So as an example, if you were working on the https://github.com/digitalocean/godo project, it would be stored in the following directory:

      $GOPATH/src/github.com/digitalocean/godo
      

      This project structure will make projects available with the go get tool. It will also help readability later. You can verify this by using the go get command and fetch the godo library:

      • go get github.com/digitalocean/godo

      This will download the contents of the godo library and create the $GOPATH/src/github.com/digitalocean/godo directory on your machine.

      You can check to see if it successfully downloaded the godo package by listing the directory:

      • ll $GOPATH/src/github.com/digitalocean/godo

      You should see output similar to this:

      Output

      drwxr-xr-x 4 root root 4096 Apr 5 00:43 ./ drwxr-xr-x 3 root root 4096 Apr 5 00:43 ../ drwxr-xr-x 8 root root 4096 Apr 5 00:43 .git/ -rwxr-xr-x 1 root root 8 Apr 5 00:43 .gitignore* -rw-r--r-- 1 root root 61 Apr 5 00:43 .travis.yml -rw-r--r-- 1 root root 2808 Apr 5 00:43 CHANGELOG.md -rw-r--r-- 1 root root 1851 Apr 5 00:43 CONTRIBUTING.md . . . -rw-r--r-- 1 root root 4893 Apr 5 00:43 vpcs.go -rw-r--r-- 1 root root 4091 Apr 5 00:43 vpcs_test.go

      In this step, you created a Go workspace and configured the necessary environment variables. In the next step you will test the workspace with some code.

      Step 3 — Creating a Simple Program

      Now that you have the Go workspace set up, create a “Hello, World!” program. This will make sure that the workspace is configured properly, and also gives you the opportunity to become more familiar with Go. Because we are creating a single Go source file, and not an actual project, we don't need to be in our workspace to do this.

      From your home directory, open up a command-line text editor, such as nano, and create a new file:

      Write your program in the new file:

      package main
      
      import "fmt"
      
      func main() {
          fmt.Println("Hello, World!")
      }
      

      This code will use the fmt package and call the Println function with Hello, World! as the argument. This will cause the phrase Hello, World! to print out to the terminal when the program is run.

      Exit nano by pressing the CTRL and X keys. When prompted to save the file, press Y and then ENTER.

      Once you exit out of nano and return to your shell, run the program:

      go run hello.go
      

      The hello.go program will cause the terminal to produce the following output:

      Output

      Hello, World!

      In this step, you used a basic program to verify that your Go workspace is properly configured.

      Conclusion

      Congratulations! At this point you have a Go programming workspace set up on your Ubuntu machine and can begin a coding project!



      Source link

      How To Install Go and Set Up a Local Programming Environment on macOS


      Introduction

      Go is a programming language that was born out of frustration at Google. Developers continually had to pick a language that executed efficiently but took a long time to compile, or to pick a language that was easy to program but ran inefficiently in production. Go was designed to have all three available at the same time: fast compilation, ease of programming, and efficient execution in production.

      While Go is a versatile programming language that can be used for many different programming projects, it’s particularly well suited for networking/distributed systems programs, and has earned a reputation as “the language of the cloud.” It focuses on helping the modern programmer do more with a strong set of tooling, removing debates over formatting by making the format part of the language specification, as well as making deployment easy by compiling to a single binary. Go is easy to learn, with a very small set of keywords, which makes it a great choice for beginners and experienced developers alike.

      This tutorial will guide you through installing Go on your local macOS machine and setting up a programming workspace via the command line.

      Prerequisites

      You will need a macOS computer with administrative access that is connected to the internet.

      Step 1 — Opening Terminal

      We’ll be completing most of our installation and setup on the command line, which is a non-graphical way to interact with your computer. That is, instead of clicking on buttons, you’ll be typing in text and receiving feedback from your computer through text as well. The command line, also known as a shell, can help you modify and automate many of the tasks you do on a computer every day, and is an essential tool for software developers.

      The macOS Terminal is an application you can use to access the command line interface. 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 like any other application to open it up. Alternatively, you can use Spotlight by holding down the CMD and SPACE keys to find Terminal by typing it out in the box that appears.

      macOS Terminal

      There are many more Terminal commands to learn that can enable you to do more powerful things. The article “An Introduction to the Linux Terminal” can get you better oriented with the Linux Terminal, which is similar to the macOS Terminal.

      Now that you have opened up Terminal, you can download and install Xcode, a package of developer tools that you will need in order to install Go.

      Step 2 — Installing Xcode

      Xcode is an integrated development environment (IDE) that is comprised of software development tools for macOS. You can check if Xcode is already installed by typing the following in the Terminal window:

      The following output means that Xcode is installed:

      Output

      /Library/Developer/CommandLineTools

      If you received an error, then in your web browser install Xcode from the App Store and accept the default options.

      Once Xcode is installed, return to your Terminal window. Next, you’ll need to install Xcode’s separate Command Line Tools app, which you can do by typing:

      At this point, Xcode and its Command Line Tools app are fully installed, and we are ready to install the package manager Homebrew.

      Step 3 — Installing and Setting Up Homebrew

      While the macOS Terminal has a lot of the functionality of Linux Terminals and other Unix systems, it does not ship with a package manager that accommodates best practices. A package manager is a collection of software tools that work to automate installation processes that include initial software installation, upgrading and configuring of software, and removing software as needed. They keep installations in a central location and can maintain all software packages on the system in formats that are commonly used. Homebrew provides macOS with a free and open source software package managing system that simplifies the installation of software on macOS.

      To install Homebrew, type this into your Terminal window:

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

      Homebrew is made with Ruby, so it will be modifying your computer’s Ruby path. The curl command pulls a script from the specified URL. This script will explain what it will do and then pauses the process to prompt you to confirm. This provides you with a lot of feedback on what the script is going to be doing to your system and gives you the opportunity to verify the process.

      If you need to enter your password, note that your keystrokes will not display in the Terminal window but they will be recorded. Simply press the return key once you’ve entered your password. Otherwise press the letter y for “yes” whenever you are prompted to confirm the installation.

      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 redo the request to a new place if the server reports that the requested page has moved to a different location.

      Once the installation process is complete, we’ll put the Homebrew directory at the top of the PATH environment variable. This will ensure that Homebrew installations will be called over the tools that macOS may select automatically that could run counter to the development environment we’re creating.

      You should create or open the ~/.bash_profile file with the command-line text editor nano using the nano command:

      Once the file opens up in the Terminal window, write the following:

      export PATH=/usr/local/bin:$PATH
      

      To save your changes, hold down the CTRL key and the letter o, and when prompted press the RETURN key. Now you can exit nano by holding the CTRL key and the letter x.

      Activate these changes by executing the following in Terminal:

      Once you have done this, the changes you have made to the PATH environment variable will go into effect.

      You can make sure that Homebrew was successfully installed by typing:

      If no updates are required at this time, the Terminal output will read:

      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.

      Once Homebrew is ready, you can install Go.

      Step 4 — Installing Go

      You can use Homebrew to search for all available packages with the brew search command. For the purpose of this tutorial, you will search for Go-related packages or modules:

      Note: This tutorial does not use brew search go as it returns too many results. Because go is such a small word and would match many packages, it has become common to use golang as the search term. This is common practice when searching the internet for Go-related articles as well. The term Golang was born from the domain for Go, which is golang.org.

      The Terminal will output a list of what you can install:

      Output

      golang golang-migrate

      Go will be among the items on the list. Go ahead and install it:

      The Terminal window will give you feedback regarding the installation process of Go. It may take a few minutes before installation is complete.

      To check the version of Go that you installed, type the following:

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

      In the future, to update Go, you can run the following commands to first update Homebrew and then update Go. You don't have to do this now, as you just installed the latest version:

      • brew update
      • brew upgrade golang

      brew update will update the formulae for Homebrew itself, ensuring you have the latest information for packages you want to install. brew upgrade golang will update the golang package to the latest release of the package.

      It is good practice to ensure that your version of Go is up-to-date.

      With Go installed on your computer, you are now ready to create a workspace for your Go projects.

      Step 5 — Creating Your Go Workspace

      Now that you have Xcode, Homebrew, and Go installed, you can go on to create your programming workspace.

      The Go workspace will contain two directories at its root:

      • src: The directory that contains Go source files. A source file is a file that you write using the Go programming language. Source files are used by the Go compiler to create an executable binary file.
      • bin: The directory that contains executables built and installed by the Go tools. Executables are binary files that run on your system and execute tasks. These are typically the programs compiled by your source code or another downloaded Go source code.

      The src subdirectory may contain multiple version control repositories (such as Git, Mercurial, and Bazaar). You will see directories like github.com or golang.org when your program imports third party libraries. If you are using a code repository like github.com, you will also put your projects and source files under that directory. This allows for a canonical import of code in your project. Canonical imports are imports that reference a fully qualified package, such as github.com/digitalocean/godo.

      Here is what a typical workspace may look like:

      .
      ├── bin
      │   ├── buffalo                                      # command executable
      │   ├── dlv                                          # command executable
      │   └── packr                                        # command executable
      └── src
          └── github.com
              └── digitalocean
                  └── godo
                      ├── .git                            # Git reposistory metadata
                      ├── account.go                      # package source
                      ├── account_test.go                 # test source
                      ├── ...
                      ├── timestamp.go
                      ├── timestamp_test.go
                      └── util
                          ├── droplet.go
                          └── droplet_test.go
      

      The default directory for the Go workspace as of 1.8 is your user's home directory with a go subdirectory, or $HOME/go. If you are using a version of Go earlier than 1.8, it is considered best practice to still use the $HOME/go location for your workspace.

      Issue the following command to create the directory structure for your Go workspace:

      • mkdir -p $HOME/go/{bin,src}

      The -p option tells mkdir to create all parents in the directory, even if they don't currently exist. Using {bin,src} creates a set of arguments to mkdir and tells it to create both the bin directory and the src directory.

      This will ensure the following directory structure is now in place:

      └── $HOME
          └── go
              ├── bin
              └── src
      

      Prior to Go 1.8, it was required to set a local environment variable called $GOPATH. While it is no longer explicitly required to do so, it is still considered a good practice as many third party tools still depend on this variable being set.

      You can set your $GOPATH by adding it to your ~/.bash_profile.

      First, open ~/.bash_profile with nano or your preferred text editor:

      Set your $GOPATH by adding the following to the file:

      ~/.bash_profile

      export GOPATH=$HOME/go
      

      When Go compiles and installs tools, it will put them in the $GOPATH/bin directory. For convenience, it's common to add the workspace's /bin subdirectory to your PATH in your ~/.bash_profile:

      ~/.bash_profile

      export PATH=$PATH:$GOPATH/bin
      

      You should now have the following entries in your ~/.bash_profile:

      ~/.bash_profile

      export GOPATH=$HOME/go
      export PATH=$PATH:$GOPATH/bin
      

      This will now allow you to run any programs you compile or download via the Go tools anywhere on your system.

      To update your shell, issue the following command to load the global variables you just created:

      You can verify your $PATH is updated by using the echo command and inspecting the output:

      You should see your $GOPATH/bin which will show up in your home directory. If you were logged in as sammy, you would see /Users/sammy/go/bin in the path.

      Output

      /Users/sammy/go/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

      Now that you have the root of the workspace created and your $GOPATH environment variable set, you will create your future projects with the following directory structure. This example assumes you are using github.com as your repository:

      $GOPATH/src/github.com/username/project
      

      If you were working on the https://github.com/digitalocean/godo project, you would put it in the following directory:

      $GOPATH/src/github.com/digitalocean/godo
      

      Structuring your projects in this manner will make projects available with the go get tool. It will also help readability later.

      You can verify this by using the go get command to fetch the godo library:

      • go get github.com/digitalocean/godo

      We can see it successfully download the godo package by listing the directory:

      • ls -l $GOPATH/src/github.com/digitalocean/godo

      You will receive output similar to this:

      Output

      -rw-r--r-- 1 sammy staff 2892 Apr 5 15:56 CHANGELOG.md -rw-r--r-- 1 sammy staff 1851 Apr 5 15:56 CONTRIBUTING.md . . . -rw-r--r-- 1 sammy staff 4893 Apr 5 15:56 vpcs.go -rw-r--r-- 1 sammy staff 4091 Apr 5 15:56 vpcs_test.go

      In this step, you created a Go workspace and configured the necessary environment variables. In the next step you will test the workspace with some code.

      Step 6 — Creating a Simple Program

      Now that you have your Go workspace set up, it's time to create a simple “Hello, World!” program. This will make sure that your workspace is working and gives you the opportunity to become more familiar with Go.

      Because you are creating a single Go source file, and not an actual project, you don't need to be in your workspace to do this.

      From your home directory, open up a command-line text editor, such as nano, and create a new file:

      Once the text file opens up in Terminal, type out your program:

      package main
      
      import "fmt"
      
      func main() {
          fmt.Println("Hello, World!")
      }
      

      Exit nano by typing the control and x keys, and when prompted to save the file press y.

      This code will use the fmt package and call the Println function with Hello, World! as the argument. This will cause the phrase Hello, World! to print out to the terminal when the program is run.

      Once you exit out of nano and return to your shell, run the program:

      The hello.go program that you just created will cause Terminal to produce the following output:

      Output

      Hello, World!

      In this step, you used a basic program to verify that your Go workspace is properly configured.

      Conclusion

      Congratulations! At this point you have a Go programming workspace set up on your local macOS machine and can begin a coding project!



      Source link

      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