🛠 Set up Rust & WebAssembly in Node.js

• 4 minutes to read

There are great use cases for WebAssembly on the server-side, especially for AI, blockchain, and big data applications. You can write functions in Rust, compile into WebAssembly, and call the managed code in Node.js applications on the server. This approach combines Rust's performance, WebAssembly's security and portability, and JavaScript's ease-of-use.

Docker setup

The easiest way to set up your dev environment is to use Docker. We provide a Dockerfile for just this purpose. First, clone the starter template project.

$ git clone https://github.com/second-state/wasmedge-nodejs-starter
$ cd wasmedge-nodejs-starter

Pull a Docker image with the prerequisite software stack.

$ docker pull secondstate/wasmedge-nodejs-starter

Next run the Docker image, and mount your local workspace. This way, you can run software inside the Docker container against your local files. You can also map ports from Docker to the local machine so that you can test web applications.

$ docker run -p 3000:3000 --rm -it -v $(pwd):/app secondstate/wasmedge-nodejs-starter
(docker) $ cd /app

That's it. You can now build and deploy software from the (docker) # command line. All changes to your code will be saved in your local files regardless whether you stop or delete the Docker container.

Manual setup

Of course, not everyone wants to use Docker. In the rest of the article, we will walk through setps for a manual setup in a fresh Ubuntu Server 20.04 distribution. The whole system takes 10 minutes to set up. We will install Rust, Node.js, the WasmEdge Runtime, and the rustwasmc compiler tool.

Setup the Operating System

We recommend you use the Ubuntu 20.04 LTS or later. WasmEdge depends on the latest version of the libstdc++ library, which is already installed on Ubuntu 20.04. If you are using another Linux distribution, please make sure that GLIBCXX >= 3.4.28 or upgrade as needed.

Then, use the commands below to bring your operating system up to date with the latest developer tools.

$ sudo apt-get update
$ sudo apt-get -y upgrade
$ sudo apt install -y build-essential libboost-all-dev llvm-dev liblld-10-dev
$ sudo apt install -y curl wget git vim pkg-config libssl-dev // Optional dev tools

If you are using the Tensorflow WASI API for AI-as-a-Service applications, install the Tensorflow library and its dependencies too.

$ sudo apt install -y libjpeg-dev libpng-dev

$ wget https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-cpu-linux-x86_64-2.3.0.tar.gz
$ sudo tar -C /usr/local -xzf libtensorflow-cpu-linux-x86_64-2.3.0.tar.gz

$ wget https://github.com/second-state/ssvm-tensorflow-deps/releases/download/0.1.0/ssvm-tensorflow-deps-lite-0.1.0-linux-x64.tar.gz
$ tar -zxvf ssvm-tensorflow-deps-lite-0.1.0-linux-x64.tar.gz
$ sudo cp ./libtensorflowlite_c.so /usr/local/lib

$ sudo ldconfig

Rust

Installing Rust on your Linux machine is very easy. Just two commands. At this time, we only supports up to Rust 1.50.0.

$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
$ source $HOME/.cargo/env
$ rustup override set 1.50.0

For more options on installing Rust on different systems, please refer to the official Rust documentation.

Node.js

The best way to install Node.js on Linux is through the NVM tool. NVM allows you to use the latest Node.js, and change easily when you need to test your code on a different version of Node.js.

$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash
$ export NVM_DIR="$HOME/.nvm"
$ [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
$ [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"

$ nvm install v12.18.3
$ nvm use v12.18.3

For more options on installing Node.js on different systems, please refer to the official Node.js documentation.

WasmEdge Runtime

The WasmEdge Runtime is an open source WebAssembly runtime optimized for edge cloud and device applications. Use the following commands to install it as a Node.js module.

$ npm install wasmedge

If you are using the Tensorflow WASI API for AI-as-a-Service applications, install WasmEdge with extensions.

$ npm install wasmedge-extensions

The rustwasmc compiler toolchain

The rustwasmc tool provides an easy command to compile Rust functions into WebAssembly and it automatically generates Node.js and WasmEdge integration code. Since you already have Node.js installed, this just requires one command.

$ npm install -g rustwasmc # Append --unsafe-perm if permission denied

What’s next?

Now you have all the tools, get started with a hello world example!

RustWebAssemblyNode.jsgetting-startedrust-function-in-nodejs
A high-performance, extensible, and hardware optimized WebAssembly Virtual Machine for automotive, cloud, AI, and blockchain applications