Categories
graalvm Java Quarkus

How to install GraalVM and native-image on Ubuntu

In this guide, you gonna see how easy it is to install GraalVM on Ubuntu Linux and start using awesome features like cross-language communication, faster JVM and more.

Before we start

We need to ensure that we have all the required technologies available on PC to use native-image, don’t forget to click Y when prompted.

sudo apt-get update
sudo apt-get install gcc zlib1g-dev build-essential unzip

GraalVM installation

Once it’s done, we’re ready to load and install GraalVM instance. First of all, we need to load it onto our machine. You can find latest GraalVM versions on official GitHub. First of all, you can choose to use either Java11 or Java8. For Java11, there’s also different archetypes: depending on your machine version, it can be either ...-linux-aarch64-version.tar.gz or ...-linux-amd64-version.tar.gz. This tutorial goes with java8-linux-amd64-20.0.0, let’s load it and unzip:

wget https://github.com/graalvm/graalvm-ce-builds/releases/download/vm-20.0.0/graalvm-ce-java8-linux-amd64-20.0.0.tar.gz
tar -xvzf graalvm-ce-java8-linux-amd64-20.0.0.tar.gz
ls

Once it’s done, you can find new folder called graalvm-ce-java8-20.0.0, or similar. This folder contains all the binaries required to use GraalVM, as well as JDK to run our Java apps. Now, we need to copy it over to place where we can use it for both purposes.

sudo mkdir /usr/lib/jvm
sudo mv graalvm-ce-java8-20.0.0/ /usr/lib/jvm

Specify PATH and JAVA_HOME

Okay, now that our GraalVM is in a handy place, the last thing that is left is to tell our system that you have it on the machine and you would like to access it via command line:

echo 'export PATH=/usr/lib/jvm/graalvm-ce-java8-20.0.0/bin:$PATH' >> ~/.bashrc
echo 'export JAVA_HOME=/usr/lib/jvm/graalvm-ce-java8-20.0.0' >> ~/.bashrc

Now, once you restart or re-login into your machine, you’ll have java and gu utils available for use. To start using them without restart you’ll need to execute source command:

source ~/.bashrc

Now, you should be able to use any of the commands that GraalVM has to offer:

java -version
# openjdk version "1.8.0_242"
# OpenJDK Runtime Environment (build 1.8.0_242-b06)
# OpenJDK 64-Bit Server VM GraalVM CE 20.0.0 (build # 25.242-b06-jvmci-20.0-b02, mixed mode)
gu
# GraalVM Component Updater v2.0.0...

Native-image installation

This part is a lot easier and faster than GraalVM installation, this is because Graal comes with Graal Updater, or gu for short. gu command allows you to install any additional dependencies. To install native-image you just need to run next command:

gu install native-image

(Optional) Install more features

While we’re at it, you may have the target to use additional features that are not available out of the box. This can be accomplished again with gu command. You can run gu available to look at all available options.

gu available
#llvm-toolchain           20.0.0              LLVM.org toolchain  github.com
#native-image             20.0.0              Native Image        github.com
#python                   20.0.0              Graal.Python        github.com
#R                        20.0.0              FastR               github.com
#ruby                     20.0.0              TruffleRuby         github.com
#wasm                     20.0.0              GraalWasm           github.com

We’re gonna install Python, but you can use any, procedure is the same for all of them

gu install python

Verify installation

Let’s create super simple java example: use any editor that you’re comfortable with(vim, nano, emacs, idea) to create a HelloWorld.java

//Insert next into HelloWorld.java
public class HelloWorld {
  public static void main(String[] args) {
    System.out.println("Hello, Quarkify!");
  }
}

Save the file, now let’s ensure that our normal GraalVM java is working, that’s easy as running next lines of code:

javac HelloWorld.java
java HelloWorld
#> Hello, Quarkify!

Building a native image

To build a native image you only need additional command to code above. Just run native-image on compiled java file.

javac HelloWorld.java
native-image HelloWorld

It will take some time, once it’s done, you’ll have lowercased helloworld file that you can execute without any other tool.

./helloworld
#> Hello, Quarkify!

That’s it, now you’re ready to start building native applications with Quarkus.

Let me know how much advantage GraalVM gave you, maybe your startup time became 10x faster, or maybe now you can call other languages from Java to solve some real-world problems. Please let me know in a comments 🙂

Weekly Quarkus Newsletter

Our newsletter saves your precious time with curated list of top news, guides and articles of the week

Marketing by

3 replies on “How to install GraalVM and native-image on Ubuntu”

Leave a Reply

Connect with




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