BLOG POSTS
    MangoHost Blog / How to Install the Deno JavaScript Runtime on Ubuntu 24
How to Install the Deno JavaScript Runtime on Ubuntu 24

How to Install the Deno JavaScript Runtime on Ubuntu 24

Deno, the brainchild of Node.js creator Ryan Dahl, has emerged as a compelling alternative to traditional JavaScript runtimes, offering built-in TypeScript support, a secure-by-default architecture, and modern ES modules out of the box. Unlike Node.js, Deno eliminates the need for package.json files and npm dependencies, instead using URL-based imports and a comprehensive standard library. This guide walks you through installing Deno on Ubuntu 24, covering multiple installation methods, configuration options, and practical examples to get you running JavaScript and TypeScript applications efficiently on your server environment.

Understanding Deno’s Architecture and Benefits

Deno fundamentally differs from Node.js in several key areas that make it particularly attractive for modern development workflows. Built on the same V8 JavaScript engine as Node.js, Deno implements a permission-based security model where scripts cannot access the file system, network, or environment variables without explicit authorization.

The runtime ships with a comprehensive toolchain including a bundler, formatter, test runner, and dependency inspector. Unlike Node.js’s CommonJS module system, Deno uses ES modules natively and supports TypeScript compilation without additional configuration. Dependencies are cached locally but imported directly from URLs, eliminating the node_modules directory entirely.

Feature Deno Node.js
TypeScript Support Built-in, zero configuration Requires additional setup
Module System ES Modules only CommonJS and ES Modules
Security Model Secure by default Full system access
Package Management URL imports, deps.ts npm, package.json
Standard Library Comprehensive built-in Minimal, relies on npm

Installation Methods for Ubuntu 24

Ubuntu 24 provides several pathways for installing Deno, each with distinct advantages depending on your deployment requirements and update preferences.

Method 1: Using the Official Install Script

The quickest installation method uses Deno’s official shell script, which automatically detects your system architecture and downloads the appropriate binary:

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

This script installs Deno to $HOME/.deno/bin. Add it to your PATH by modifying your shell configuration:

echo 'export PATH="$HOME/.deno/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc

For zsh users:

echo 'export PATH="$HOME/.deno/bin:$PATH"' >> ~/.zshrc
source ~/.zshrc

Method 2: Manual Binary Installation

For production environments or cases requiring specific version control, manually downloading the binary provides more predictable results:

# Create installation directory
sudo mkdir -p /usr/local/bin

# Download latest Deno binary (replace with specific version if needed)
curl -Lo deno.zip "https://github.com/denoland/deno/releases/latest/download/deno-x86_64-unknown-linux-gnu.zip"

# Extract and install
unzip deno.zip
sudo mv deno /usr/local/bin/
sudo chmod +x /usr/local/bin/deno

# Clean up
rm deno.zip

Method 3: Using Snap Package Manager

Ubuntu 24’s snap ecosystem includes Deno, though this version may lag behind the latest releases:

sudo snap install deno

Method 4: Building from Source

Developers requiring custom builds or the absolute latest features can compile Deno from source. This requires Rust and significant compilation time:

# Install Rust if not present
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
source ~/.cargo/env

# Install build dependencies
sudo apt update
sudo apt install -y build-essential

# Clone and build Deno
git clone https://github.com/denoland/deno.git
cd deno
cargo build --release

# Install binary
sudo cp target/release/deno /usr/local/bin/

Verification and Basic Configuration

After installation, verify Deno functionality and explore its built-in capabilities:

# Check version and installation
deno --version

# Display help and available commands
deno help

Deno’s REPL (Read-Eval-Print Loop) provides an interactive environment for testing JavaScript and TypeScript code:

deno repl

Create a simple test script to verify TypeScript compilation:

# Create test file
cat << 'EOF' > hello.ts
interface Person {
  name: string;
  age: number;
}

const person: Person = {
  name: "Ubuntu User",
  age: 25
};

console.log(`Hello, ${person.name}! You are ${person.age} years old.`);
EOF

# Run TypeScript directly
deno run hello.ts

Understanding Deno’s Permission System

Deno’s security model requires explicit permission grants for accessing system resources. This differs significantly from Node.js’s unrestricted access pattern:

# Network access permission
deno run --allow-net fetch_example.ts

# File system read permission
deno run --allow-read read_file.ts

# File system write permission
deno run --allow-write write_file.ts

# Environment variable access
deno run --allow-env env_example.ts

# All permissions (use cautiously)
deno run --allow-all app.ts

# Specific URL network access
deno run --allow-net=api.github.com github_api.ts

Here’s a practical example demonstrating permission usage:

# Create a simple HTTP server example
cat << 'EOF' > server.ts
import { serve } from "https://deno.land/std@0.208.0/http/server.ts";

const handler = (request: Request): Response => {
  const body = `Your user-agent is:\n\n${
    request.headers.get("user-agent") ?? "Unknown"
  }`;

  return new Response(body, { status: 200 });
};

console.log("HTTP webserver running. Access it at: http://localhost:8000/");
serve(handler, { port: 8000 });
EOF

# Run with network permissions
deno run --allow-net server.ts

Real-World Use Cases and Examples

Deno excels in several practical scenarios where its built-in tooling and security model provide immediate value.

Web API Development

Building REST APIs with Deno eliminates many configuration steps required in Node.js environments:

cat << 'EOF' > api.ts
import { serve } from "https://deno.land/std@0.208.0/http/server.ts";

interface Task {
  id: number;
  title: string;
  completed: boolean;
}

const tasks: Task[] = [
  { id: 1, title: "Install Deno", completed: true },
  { id: 2, title: "Build API", completed: false },
];

const handler = async (request: Request): Promise => {
  const url = new URL(request.url);
  const method = request.method;

  // CORS headers
  const headers = new Headers({
    "Content-Type": "application/json",
    "Access-Control-Allow-Origin": "*",
    "Access-Control-Allow-Methods": "GET, POST, PUT, DELETE",
  });

  if (method === "GET" && url.pathname === "/tasks") {
    return new Response(JSON.stringify(tasks), { headers });
  }

  if (method === "POST" && url.pathname === "/tasks") {
    const body = await request.json();
    const newTask: Task = {
      id: tasks.length + 1,
      title: body.title,
      completed: false,
    };
    tasks.push(newTask);
    return new Response(JSON.stringify(newTask), { headers, status: 201 });
  }

  return new Response("Not Found", { status: 404 });
};

serve(handler, { port: 3000 });
EOF

# Run the API server
deno run --allow-net api.ts

Automated Scripts and System Administration

System administrators can leverage Deno for automation scripts that require strong typing and modern JavaScript features:

cat << 'EOF' > system_monitor.ts
interface SystemInfo {
  platform: string;
  hostname: string;
  uptime: number;
  memory: {
    total: number;
    free: number;
    used: number;
  };
}

async function getSystemInfo(): Promise {
  const platform = Deno.build.os;
  const hostname = Deno.hostname();
  
  // Read memory info on Linux
  const meminfo = await Deno.readTextFile("/proc/meminfo");
  const memLines = meminfo.split('\n');
  
  const getMemValue = (line: string): number => {
    const match = line.match(/(\d+)/);
    return match ? parseInt(match[1]) * 1024 : 0; // Convert KB to bytes
  };
  
  const totalMem = getMemValue(memLines.find(line => line.startsWith('MemTotal:')) || '');
  const freeMem = getMemValue(memLines.find(line => line.startsWith('MemFree:')) || '');
  
  return {
    platform,
    hostname,
    uptime: performance.now(),
    memory: {
      total: totalMem,
      free: freeMem,
      used: totalMem - freeMem,
    },
  };
}

const info = await getSystemInfo();
console.log(JSON.stringify(info, null, 2));
EOF

# Run system monitoring script
deno run --allow-read --allow-sys system_monitor.ts

Performance Optimization and Best Practices

Deno provides several mechanisms for optimizing application performance and development workflows.

Dependency Management

Centralize dependencies using a deps.ts file to improve maintainability and caching:

cat << 'EOF' > deps.ts
// External dependencies
export { serve } from "https://deno.land/std@0.208.0/http/server.ts";
export { assertEquals } from "https://deno.land/std@0.208.0/assert/mod.ts";
export { delay } from "https://deno.land/std@0.208.0/async/delay.ts";

// Third-party libraries
export { Application, Router } from "https://deno.land/x/oak@v12.6.2/mod.ts";
EOF

Import from the deps.ts file in your application modules:

import { serve, Application } from "./deps.ts";

Caching and Performance

Deno caches remote dependencies automatically, but you can pre-cache dependencies for faster startup times:

# Cache all dependencies
deno cache deps.ts

# Cache with specific import map
deno cache --import-map=import_map.json main.ts

# Display cached dependencies
deno info deps.ts

Import Maps for Better Dependency Management

Import maps provide Node.js-style bare specifiers while maintaining URL-based imports:

cat << 'EOF' > import_map.json
{
  "imports": {
    "oak": "https://deno.land/x/oak@v12.6.2/mod.ts",
    "std/": "https://deno.land/std@0.208.0/"
  }
}
EOF

# Use import map
deno run --import-map=import_map.json --allow-net app.ts

Common Issues and Troubleshooting

Permission Errors

The most frequent Deno issues involve insufficient permissions. Always check error messages for required permission flags:

  • Network access denied: Add --allow-net or specify allowed domains
  • File system access denied: Use --allow-read and --allow-write as needed
  • Environment variable access: Include --allow-env for process environment access

Module Resolution Issues

URL-based imports can cause confusion for developers coming from Node.js:

# Incorrect - this will fail
import { something } from "package-name";

# Correct - use full URLs
import { something } from "https://deno.land/x/package-name@v1.0.0/mod.ts";

Version Pinning

Always pin dependency versions in production to avoid unexpected breaking changes:

# Good - version pinned
import { serve } from "https://deno.land/std@0.208.0/http/server.ts";

# Avoid - uses latest version
import { serve } from "https://deno.land/std/http/server.ts";

Integration with Development Tools

Modern development workflows require proper tooling integration. Deno provides built-in support for common development tasks:

# Format code
deno fmt

# Lint code
deno lint

# Run tests
deno test

# Bundle application
deno bundle main.ts bundle.js

# Generate documentation
deno doc main.ts

For VS Code users, install the official Deno extension for enhanced TypeScript support and IntelliSense.

Production Deployment Considerations

When deploying Deno applications on production servers, several considerations ensure reliable operation:

Systemd Service Configuration

Create a systemd service for automatic startup and process management:

cat << 'EOF' | sudo tee /etc/systemd/system/deno-app.service
[Unit]
Description=Deno Application
After=network.target

[Service]
Type=simple
User=www-data
WorkingDirectory=/opt/deno-app
ExecStart=/usr/local/bin/deno run --allow-net --allow-read --allow-env main.ts
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target
EOF

# Enable and start service
sudo systemctl enable deno-app
sudo systemctl start deno-app

Security Hardening

Minimize permission grants and use specific allow lists:

# Instead of --allow-net, specify allowed domains
deno run --allow-net=api.example.com,cdn.jsdelivr.net app.ts

# Use read-only permissions where possible
deno run --allow-read=/app/config,/app/static app.ts

Deno represents a significant evolution in JavaScript runtime design, offering compelling advantages for modern web development and system administration tasks. Its secure-by-default architecture, built-in TypeScript support, and comprehensive standard library make it particularly well-suited for VPS deployments and dedicated server environments where security and developer productivity are priorities. The installation process on Ubuntu 24 is straightforward across multiple methods, and the runtime's modern approach to dependency management eliminates many traditional Node.js pain points while maintaining excellent performance characteristics.

For additional information and advanced usage patterns, consult the official Deno documentation and explore the growing ecosystem of Deno-specific modules available through the Deno third-party modules registry.



This article incorporates information and material from various online sources. We acknowledge and appreciate the work of all original authors, publishers, and websites. While every effort has been made to appropriately credit the source material, any unintentional oversight or omission does not constitute a copyright infringement. All trademarks, logos, and images mentioned are the property of their respective owners. If you believe that any content used in this article infringes upon your copyright, please contact us immediately for review and prompt action.

This article is intended for informational and educational purposes only and does not infringe on the rights of the copyright owners. If any copyrighted material has been used without proper credit or in violation of copyright laws, it is unintentional and we will rectify it promptly upon notification. Please note that the republishing, redistribution, or reproduction of part or all of the contents in any form is prohibited without express written permission from the author and website owner. For permissions or further inquiries, please contact us.

Leave a reply

Your email address will not be published. Required fields are marked