Pexels Photo 374720

GraalVM installation and setup on macOS by Adam Warski

Pexels Photo 374720

Are you wondering about the use of GraalVM? 

This set of tools is incredibly useful but how should you go about the installation and setup? Check out this article by Founder of SoftwareMill, Adam Warski and you'll have all your questions answered. 


'GraalVM, of which the final 19.0 version was released a couple of days ago, is a set of tools providing a runtime for multiple (not only JVM-based) languages, in various deployment scenarios. What does that mean?

First of all, GraalVM contains a new implementation of a just-in-time (JIT) compiler for Java, replacing the original one. The JIT is responsible for translating the bytecode into machine code. GraalVM aims at doing more work upfront (when the bytecode is first interpreted), with the goal of producing more efficient machine code.

Apart from the JIT, the other parts of the JVM remain the same. GraalVM distribution is based on Java 8 (OpenJDK 1.8.0_212 to be precise). That means you won’t get the newest Java features; however, the main focus of GraalVM is on other languages, such as Scala and Kotlin, so this should not be a blocker.

The original JIT can still be used, if needed (which is useful for benchmarking), by providing the '-XX:+UseJVMCICompiler' option. See here for more compiler options.

Moreover, GraalVM offers the possibility of running Java code alongside R, Python, Javascript and Ruby, by providing a common, sandboxed runtime. Data can be shared, and code cross-executed. You can even write your own language and leverage GraalVM’s interpreter!

Finally, GraalVM offers the possibility to build native images of Java applications, which have nearly instant startup time and consume less memory. It does so by pre-analysing what a particular Java application needs (starting from a provided main class), pre-compiling and including only code that is really needed. The built executable is based on SubstrateVM which is a “partial” Java virtual machine — it includes threading and garbage collection, but won’t be able to interpret bytecode or dynamically load classes, for example.

For more details on what you can do with GraalVM, see Top 10 Things To Do With GraalVM and the Why GraalVM documentation section.

GraalVM is an open-source project, hosted by Oracle, with contributions from many others well-known names in the industry (such as Twitter).


Installing on macOS

Sounds great, doesn’t it? How to you use it on macOS? Just a couple of easy steps:


First of all, download the official binary. You can choose from the free community edition, and the commercial enterprise edition, which offers additional performance, scalability and security features.

Enterprise edition can be downloaded from the Oracle Technology Network, but we’ll use the community edition, which is more than enough to test GraalVM.

Head over to the GitHub page, and select the `graalvm-ce-darwin-amd64–19.0.0.tar.gz download:

Unpack the archive (opening the file in Finder should do it), and you will end up with a 'graalvm-ce-19.0.0' directory.



Now, let’s move the JVM that we have just download (which is, just to remind, 'OpenJDK 8u212' with GraalVM’s JIT and other tools) to its proper location on macOS:

sudo mv graalvm-ce-19.0.0 /Library/Java/JavaVirtualMachines

Since '/Library/Java/JavaVirtualMachines' is a system directory, you’ll need sudo to run the command. This is where all the other JVMs live on macOS.



Now it’s time to verify our installation. We can use the 'java_home' utility provided by the system to list the JVMs that are installed. Run '/usr/libexec/java_home -V' and you should get a list of all installed VMs:

And there it is, GraalVM waiting to be used!



Now we have to modify our 'PATH' to make sure it points to the GraalVM JVM. It will be easiest to use 'java_home' once again. We can get the path to the GraalVM home directory using:

/usr/libexec/java_home -v 1.8

I’m not entirely sure why GraalVM is chosen over the standard Java 8 installation. Is it because GraalVM’s Java is newer than the Java 8 I have? Is it random? Is it ordered alphabetically? Either way, the above works for me (and is also mentioned in the official docs).

If you add the following to your '~/.bash_profile', or other startup script, you’ll have everything setup properly:

export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)
export PATH=$JAVA_HOME/bin:$PATH



Enjoy! Now GraalVM should be the default JVM available on the path:

We can also use other tools coming with GraalVM. For example, to install 'native-image', using which we can build native executables from java and jar files, first we need to use 'gu' (GraalVM updater) to download the component:

gu install native-image

This will add the 'native-image' command to the path.



That’s it! We can now enjoy (hopefully) faster Java execution. There’s much more to GraalVM than we have mentioned here — head over to the official documentation to find out more (e.g. how you can integrate with non-JVM languages, such as JavaScript or Python).

Stay tuned for the next post on GraalVM, which will cover using 'native-image' with Scala and SBT!'



This article was written by Adam Warski and posted originally on SoftwareMill Blog.