# Wasmtime — Fast Secure WebAssembly Runtime > Wasmtime is a standalone WebAssembly runtime by the Bytecode Alliance. It runs Wasm modules outside the browser with near-native speed, sandboxed security, and WASI support — enabling server-side Wasm for plugins, serverless functions, and edge computing. ## Install Save in your project root: # Wasmtime — Fast Secure WebAssembly Runtime ## Quick Use ```bash # Install Wasmtime curl https://wasmtime.dev/install.sh -sSf | bash # Run a WebAssembly module wasmtime hello.wasm # Compile Rust to Wasm and run cargo new hello-wasm --lib # Add to Cargo.toml: [lib] crate-type = ["cdylib"] cargo build --target wasm32-wasip1 wasmtime target/wasm32-wasip1/debug/hello_wasm.wasm # Run with WASI filesystem access wasmtime --dir . my-app.wasm ``` ## Introduction Wasmtime runs WebAssembly outside the browser. While Wasm was originally designed for web browsers, Wasmtime enables server-side use cases: plugin systems (Envoy, Zed, Lapce use Wasm plugins), serverless functions (Cloudflare Workers, Fastly Compute), embedded scripting, and portable application distribution. With over 18,000 GitHub stars, Wasmtime is developed by the Bytecode Alliance (Mozilla, Fastly, Intel, Microsoft) and is the reference implementation for WASI (WebAssembly System Interface). It compiles Wasm to native code via Cranelift for near-native performance. ## What Wasmtime Does Wasmtime takes compiled WebAssembly modules (.wasm files) and executes them in a sandboxed environment. Programs can only access resources (files, network, environment) explicitly granted by the host. This capability-based security model makes Wasm ideal for running untrusted code safely. ## Architecture Overview ``` [WebAssembly Module (.wasm)] Compiled from Rust, C, Go, Python, JS, or any language | [Wasmtime Runtime] | +-------+-------+ | | [Cranelift] [WASI] Compiler System Wasm -> native Interface machine code File, Network, JIT or AOT Clock, Random | [Sandboxed Execution] Capability-based security No access unless granted Memory isolation | [Host Integration] Embed in Rust, C, Python, Go, .NET applications Import/export functions ``` ## Self-Hosting & Configuration ```rust // Embedding Wasmtime in a Rust application use wasmtime::*; fn main() -> Result<()> { let engine = Engine::default(); let module = Module::from_file(&engine, "plugin.wasm")?; let mut store = Store::new(&engine, ()); let instance = Instance::new(&mut store, &module, &[])?; // Call an exported function let greet = instance.get_typed_func::<(i32,), i32>(&mut store, "add")?; let result = greet.call(&mut store, (40,))?; println!("Result: {}", result); Ok(()) } ``` ```bash # WASI capabilities (grant filesystem access) wasmtime --dir /data:/data my-app.wasm wasmtime --env KEY=VALUE my-app.wasm wasmtime --tcplisten 0.0.0.0:8080 my-server.wasm # AOT compilation (faster startup) wasmtime compile my-app.wasm -o my-app.cwasm wasmtime run my-app.cwasm ``` ## Key Features - **Near-Native Speed** — Cranelift JIT/AOT compilation - **Sandboxed Security** — capability-based, no access by default - **WASI Support** — standard system interface for files, network, etc. - **Component Model** — compose Wasm modules with typed interfaces - **Multi-Language** — run code compiled from Rust, C, Go, Python, etc. - **Embeddable** — host API for Rust, C, Python, Go, .NET, Ruby - **AOT Compilation** — pre-compile for instant startup - **Standards Compliant** — W3C WebAssembly + WASI standards ## Comparison with Similar Tools | Feature | Wasmtime | Wasmer | WasmEdge | V8 (wasm) | Node.js (wasm) | |---|---|---|---|---|---| | Focus | Standards compliance | Ease of use | Edge/cloud | Browser + server | Browser compat | | Compiler | Cranelift | Cranelift/LLVM/Single | LLVM | TurboFan | V8 | | WASI | Yes (reference) | Yes | Yes | Partial | Partial | | Component Model | Yes | No | Partial | No | No | | Embeddable | Rust, C, Python, Go | Rust, C, Python, Go, JS | Rust, C, Go | C++ | JavaScript | | Backed By | Bytecode Alliance | Wasmer Inc | CNCF | Google | OpenJS | ## FAQ **Q: When should I use Wasm outside the browser?** A: For plugin systems (run untrusted code safely), serverless functions (portable, fast cold-start), edge computing (platform-independent binaries), and portable CLI tools. **Q: Wasmtime vs Wasmer — which should I choose?** A: Wasmtime for standards compliance, Component Model support, and production use (backed by Bytecode Alliance). Wasmer for ease of use, package registry (WAPM), and broader language support. **Q: Can I run Python or JavaScript in Wasmtime?** A: Yes. Python (via CPython compiled to Wasm) and JavaScript (via QuickJS or SpiderMonkey compiled to Wasm) can run inside Wasmtime, though with some limitations compared to native runtimes. **Q: What is the Component Model?** A: The Component Model is a standard for composing Wasm modules with typed interfaces (WIT). It enables language-agnostic module composition — a Rust component can call a Python component seamlessly. ## Sources - GitHub: https://github.com/bytecodealliance/wasmtime - Documentation: https://docs.wasmtime.dev - Bytecode Alliance: https://bytecodealliance.org - License: Apache-2.0 --- Source: https://tokrepo.com/en/workflows/823821b7-3745-11f1-9bc6-00163e2b0d79 Author: AI Open Source