Emscripten SDK is a toolchain designed to compile C and C++ code into WebAssembly (Wasm), enabling native applications to run efficiently within web browsers. Emscripten translates traditional native code into a web-compatible format without requiring major code rewrites, making it a popular choice for porting performance-critical applications such as video games, 3D rendering engines, scientific simulations, etc. This tutorial explains how to install Emscripten SDK on Ubuntu 24.04.
Install Emscripten SDK
Download Emscripten SDK from GitHub repository:
wget -qO emsdk.tar.gz https://github.com/emscripten-core/emsdk/archive/main.tar.gz
Create a new directory to store Emscripten SDK and extract the tar.gz
file to it:
sudo mkdir /opt/emsdk
sudo tar xf emsdk.tar.gz --strip-components=1 -C /opt/emsdk
Download and install the latest SDK related tools (Clang, Node.js, etc.):
sudo /opt/emsdk/emsdk install latest
Activate the installed SDK version:
sudo /opt/emsdk/emsdk activate latest
To ensure that the Emscripten environment variables are automatically loaded on system startup, add the environment setup script to the system-wide profile directory:
echo 'EMSDK_QUIET=1 source /opt/emsdk/emsdk_env.sh' | sudo tee -a /etc/profile.d/emsdk.sh
To apply the changes right away, you can either log out and log back in, or simply run the following command:
source /etc/profile
Verify installation by checking the Emscripten compiler version:
emcc --version
Clean up the installation archive:
rm -rf emsdk.tar.gz
Testing Emscripten SDK
Create a main.c
file:
nano main.c
Add the following code to the file:
#include <stdio.h>
int main() {
printf("Hello world\n");
return 0;
}
Compile the C code to JavaScript:
emcc main.c -o test.js
Run the compiled JavaScript with Node.js:
node test.js
Uninstall Emscripten SDK
If you wish to completely remove Emscripten SDK, delete the installation directory:
sudo rm -rf /opt/emsdk
Remove the environment setup script from the system profile:
sudo rm -rf /etc/profile.d/emsdk.sh
Leave a Comment
Cancel reply