How to Install Deno on Ubuntu Linux, Windows, Mac

This is a tutorial for installing Deno on Linux, Windows, or Mac.

Deno is a runtime for JavaScript and TypeScript. It's based on Rust and V8 JavaScript engine. Being one of the alternatives to Node.js, both Deno and Node.js are created by Ryan Dahl. However, Deno has some differences compared to Node.js.

  • Deno supports TypeScript out fo the box.
  • Deno doesn't support NPM packages, dependencies are imported via the URLs.
  • Deno uses Promises, supporting top-level await.
  • Deno supports browser APIs.
  • Deno is secured by default, the code is executed in a secure sandbox.

Installation

To install Deno, you need to download the executable. The installation is very simple, you only need to download Deno from a URL or using a package manager. Below are the installation commands which depend on the used operating system and package manager.

Shell (Mac, Linux)

  curl -fsSL https://deno.land/x/install/install.sh | sh

PowerShell (Windows)

  iwr https://deno.land/x/install/install.ps1 -useb | iex

Homebrew (Mac)

  brew install deno

Chocolatey (Windows)

  choco install deno

Scoop (Windows)

  scoop install deno

Cargo (Windows)

  cargo install deno

Running the above commands will download the latest version of Deno. After installation is successful, you can check the version of Deno by using deno -V command.

  $ deno -V
  deno 1.5.4

Alternatively, deno --version command also shows the typescript version.

  $ deno --version
  deno 1.5.4 (bc79d55, release, x86_64-unknown-linux-gnu)
  v8 8.8.278.2
  typescript 4.0.5

If installation is successful but you're unable to run any deno command, it can be because you haven't added the environment variable for Deno. For example, if you use Linux, you need to update the environment variables in ~/.bashrc (for non-login shells) or ~/.bash_profile (for interactive login shells).

  export DENO_INSTALL="/home/ivan/.deno"
  export PATH="$DENO_INSTALL/bin:$PATH"

After that, reopen your sheel or run source ~/.bashrc or source ~/.bash_profile depending on where you set the environment variables.

On Windows, the environment variables should be added automatically.

 

Installation Using DVM (Deno Version Manager)

Many Node.js users use NVM (Node Version Manager) to easily install and switch between multiple versions. For Deno, there is a similar thing called DVM (Deno Version Manager) developed by justjavac. To use DVM, first we have to install it.

The installation command depends whether you use Shell (on Linux or Mac) or PowerShell (on Windows).

Shell

  curl -fsSL https://deno.land/x/dvm/install.sh | sh

 

PowerShell

  iwr https://deno.land/x/dvm/install.ps1 -useb | iex

 

Depending on the platform, you may need to perform additional actions. For example, if you use Linux, you need to make sure that the following environment variables have been set in ~/.bashrc (for non-login shells) or ~/.bash_profile (for interactive login shells).

  export DVM_DIR="/home/ivan/.dvm"
  export PATH="$DVM_DIR/bin:$PATH"

After that, reopen your shell or run source ~/.bashrc or source ~/.bash_profile depending on where you set the environment variables.

You can use deno --help to the list of available commands. To install a specific version, use the following command.

  dvm install {version}

Below is the example to install version 1.5.4.

  dvm install 1.5.4

If there are multiple installed versions you can switch to another version using the below command.

  dvm use {version}

Below is the example to switch to version 1.5.4.

  dvm use 1.5.4

It's also possible to get the list of installed versions using the below command.

  dvm list

Usually the installed versions are stored in a .dvm directory which is located in the home directory of the user.

 

Running Deno

To run a Deno script, use the following command

  deno run {source}

The source can be a local file or a remote file accessed using the URL. Deno provides a sample code to run on https://deno.land/std/examples/welcome.ts.

  $ deno run https://deno.land/std/examples/welcome.ts

Output:

  Download https://deno.land/std/examples/welcome.ts
  Warning Implicitly using latest version (0.79.0) for https://deno.land/std/examples/welcome.ts
  Download https://deno.land/std@0.79.0/examples/welcome.ts
  Check https://deno.land/std/examples/welcome.ts
  Welcome to Deno 🦕

As a replacement to Node.js, it's quite common to use Deno as a server. Creating a server is very simple. You need to import a dependency from deno.land, then you can start the server on a particular port. Below is a very simple app that returns a text response every time a request is received.

app.ts

  import { serve } from 'https://deno.land/std@0.79.0/http/server.ts';

  const s = serve({ port: 8080 });

  console.log('Server started on port 8080');

  for await (const req of s) {
    req.respond({ body: 'Hello World! Welcome to Deno.\n' });
  }

To run the above code, use the similar command, but with the file path as the source.

  deno run app.ts

However, running the above command may result in the following error.

  Check file:///home/ivan/Projects/app.ts
  error: Uncaught PermissionDenied: network access to "0.0.0.0:8080", run again with the --allow-net flag
    at processResponse (core.js:223:11)
    at Object.jsonOpSync (core.js:246:12)
    at opListen (deno:cli/rt/30_net.js:32:17)
    at Object.listen (deno:cli/rt/30_net.js:207:17)
    at serve (server.ts:301:25)
    at app.ts:3:11

As suggested in the output, adding --allow-net flag is required. For the above example, because we want to run the server on port 8080, the command should be changed to:.

  deno run --allow-net=0.0.0.0:8080 app.ts

After adding the flag, you should be able to start the server and send HTTP requests.

 

That's how to install Deno which should be very simple. You can choose to install it directly or using version manager.