Liberica Native Image Kit (Liberica NIK) is a utility that converts your JVM-based application into a fully AOT compiled native executable under the closed-world assumption with an almost instant startup time. It is based on the open source GraalVM Community Edition. Being compatible with various platforms, including lightweight musl-based Alpine Linux, this technology optimizes resource consumption and minimizes the static footprint.
In this article, we’ll demonstrate how Liberica NIK aids in software development and what problems it solves.
A versatile optimization tool
Native Image is a helpful technology to accelerate your applications. In one of the recent articles, we ran a test to see which Java microservice configuration would use the least amount of RAM and launch the fastest. Compared to other methods, running a project in a native image on Alpine Linux consumed half as much memory and started up in 1/10 of a second, more than ten times as quickly as with JVM optimizations. At that time, it was only possible to execute GraalVM on Alpine with glibc installed separately. However, the whole purpose of using Alpine is to take advantage of the musl library.
Bringing full musl support to GraalVM Open Source was a challenge that BellSoft took. We have contributed four patches, two of which are integrated and two are under review as of writing this article.
- Linux-musl support #230 (integrated)
- Added linux-musl support #175 (fastr) (integrated)
- [WIP] linux-musl support #3141 (under review)
- Added linux-musl support #2223 (truffleruby) (under review)
All the patches deal with adding support for Linux distributions that use musl as their standard C library such as Alpine. They are meant to be added to both Graal’s build system and code.
BellSoft team came forth to introduce these changes in order to enable its planned musl-based GraalVM builds of Liberica JDK, a free and 100% open source OpenJDK distribution.
Just like our JEP 386 integrated into JDK 16, musl support provides three image-related benefits:
All these bring considerable economy in terms of time, traffic, and financial expenses. “Less is more” perfectly applies to IT: By minimizing resources, your project maximizes business value. See how much the Alpine + native image combination saves in footprint, RAM, and speed in the next section.
Liberica Native Image Kit
We release pre-assembled packages dubbed Liberica Native Image Kit that contain Liberica VM, native image (derived from GraalVM CE) and language installables compatible with a specific operating system.
Click the button below to open the Download Center and choose a package compatible with your system.
The current builds have the following features:
- OS: Linux x86_64 (glibc), Linux Alpine x86_64 (musl), Linux AArch64 (glibc), Linux Alpine AArch64 (musl), Mac OS 64_64 are supported
- Languages: Java, JavaScript, LLVM, Python, Ruby, R, and WebAssembly
- Update level: JDK 11.0.10, Graal VM CE 21.0
Learn more about which languages are available on different platforms at the Supported Configuration page.
Let’s show the gains you get with this tool through an experiment. A Spring-based echo microservice was first compiled into a JAR file and run inside a Debian 9 container. Then we compiled it to both a glibc-based native image (run inside a Debian 9 container) and a musl-based one (run inside an Alpine 3.11 container). The comparative results are shown in the table below.
Another prominent advantage is that Liberica NIK allows seamless polyglot projects, such as microservices in different programming languages. To understand the impact of using native images with Alpine Linux we created a simple client in Node.js and a simple server with Spring, mimicking a microservice application.
public class EchoApplication {
public static void main(String[] args) {
SpringApplication.run(EchoApplication.class);
}
@PostMapping("/")
public String echo(@RequestBody String body) {
return body;
}
}
Installation guide
We will give instructions separately for all Linux distributions (including Alpine) and macOS.
Use one of the following links, depending on your platform.
Alpine Linux
Download a Liberica NIK package from BellSoft:
wget https://download.bell-sw.com/vm/21.0.0.2/bellsoft-liberica-vm-openjdk11-21.0.0.2-linux-x64-musl.tar.gz
wget https://download.bell-sw.com/vm/21.0.0.2/native-image-installable-openjdk11-21.0.0.2-linux-x64-musl.jar
Specify the installation directory for this package:
export INSTALL_DIR=<your directory>
Unpack the package:
tar -C $INSTALL_DIR -xzf <filename>
Configure your environment:
export NIK_HOME=$INSTALL_DIR/liberica-vm-openjdk11-21.0.0.2
export PATH=$NIK_HOME/bin:$PATH
Finally, install Native Image with
gu -L install native-image-installable-openjdk11-21.0.0.2-linux-x64-musl.jar
Linux
Download a Liberica NIK package from BellSoft:
wget https://download.bell-sw.com/vm/21.0.0.2/bellsoft-liberica-vm-openjdk11-21.0.0.2-linux-amd64.tar.gz
wget https://download.bell-sw.com/vm/21.0.0.2/native-image-installable-openjdk11-21.0.0.2-linux-amd64.jar
Specify the installation directory for this package:
export INSTALL_DIR=<your directory>
Unpack the package:
tar -C $INSTALL_DIR -xzf <filename>
Configure your environment:
export NIK_HOME=$INSTALL_DIR/liberica-vm-openjdk11-21.0.0.2
export PATH=$NIK_HOME/bin:$PATH
Finally, install Native Image with
gu -L install native-image-installable-openjdk11-21.0.0.2-linux-amd64.jar
macOS
Download a Liberica NIK package from BellSoft:
curl -O https://download.bell-sw.com/vm/21.0.0.2/bellsoft-liberica-vm-openjdk11-21.0.0.2-macos-amd64.zip
curl -O https://download.bell-sw.com/vm/21.0.0.2/native-image-installable-openjdk11-21.0.0.2-macos-amd64.jar
Unpack the package:
unzip bellsoft-liberica-vm-openjdk11-21.0.0.2-macos-amd64.zip
Move the package to libraries:
sudo mv bellsoft-liberica-vm-openjdk11-21.0.0.2 /Library/Java/JavaVirtualMachines
Configure your environment:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/bellsoft-liberica-vm-openjdk11-21.0.0.2/Contents/Home
export PATH=$JAVA_HOME/bin:$PATH
Finally, install Native Image with
gu -L install native-image-installable-openjdk11-21.0.0.2-macos-amd64.jar
Language support
With this product release, BellSoft introduces support for multilingual projects as an experimental feature. Here’s how you use Liberica language binaries:
Open the terminal and type gu available
. You’ll see a list of available languages:
Downloading: Component catalog from download.bell-sw.com
ComponentId Version Component name Stability Origin
--------------------------------------------------------------------------------------------------------
llvm-toolchain 21.0.0.2 LLVM.org toolchain Supported download.bell-sw.com
native-image 21.0.0.2 Native Image Early adopter download.bell-sw.com
python 21.0.0.2 Graal.Python Experimental download.bell-sw.com
R 21.0.0.2 FastR Experimental download.bell-sw.com
ruby 21.0.0.2 TruffleRuby Experimental download.bell-sw.com
wasm 21.0.0.2 GraalWasm Experimental download.bell-sw.com
Execute gu install [language]
to install specific language. For example, gu install python
.
This is also possible to execute manually:
wget <component-url>
gu -L install <component>.jar
For example,
wget https://download.bell-sw.com/vm/21.0.0/python-installable-openjdk11-21.0.0-linux-amd64.jar
gu -L install python-installable-openjdk11-21.0.0-linux-amd64.jar
Are you keen on using native image in your own business but feel unprepared? Don’t worry, switching to a new tool is never hard or stressful with a reliable partner to support you. We have put together a couple of case studies and will be happy to show how our clients apply Liberica NIK to their enterprise projects. Besides saving you time, they might contain some elegant solutions for your use case. Click the button below, fill out the form, and one of BellSoft’s senior engineers will get back to you soon.
Conclusion
Next time we’ll go further and talk about different ways of using containerized applications executed with Liberica NIK. You see that with this tool at your disposal, software components consume less RAM, communicate with total ease, and run at high speed. We are happy to bring the technology to the OpenJDK community and look forward to exploring more applications for musl-based solutions.