Skip to main content

Hello World (first programs) · Beginner · 15 min

Hello World in Nim: compile through C and to JavaScript

By Padding · Updated 18 Nov 2026
Nim 2.4.2 · Ubuntu 26.04 · GCC · Node.js

You will install Nim 2.4, compile Hello World to a native program through C and read the C it generates, compile the same file to JavaScript, and build a small nimble project that greets the reader by name.

On this page

Nim looks like Python: blocks are marked by indentation, there are no braces and no semicolons, and a whole program can be one line. Underneath, it is a statically typed, compiled language. The Nim compiler does not produce machine code itself. It translates your program into C (or C++, Objective-C or JavaScript) and hands that to an existing compiler such as GCC or Clang. You get Python-like source, type checking before anything runs, and programs that run at the speed of C.

That design has a practical upside you will see in this tutorial: the C that Nim generates sits in a folder you can open and read. And because JavaScript is one of the back ends, the same source file can run in a terminal as a native program or in Node.js and the browser.

You will install Nim, run Hello World, read the generated C, compile to JavaScript, then write a program that reads a name and greets it, and turn that into a project with nimble, Nim's package manager. The code was written for Nim 2.4.2 and also runs on 2.4.0 and 2.2.

Step 1 · Install Nim and a C compiler

Nim needs a C compiler on your path, so install that first, along with curl for the installer and git, which nimble uses to fetch packages. On Ubuntu 26.04 or Debian:

Bash
sudo apt update
sudo apt install -y build-essential curl git

The recommended way to install Nim on Linux and macOS is choosenim, a small installer from the Nim project that downloads the current stable release into your home folder and lets you switch versions later:

Bash
curl https://nim-lang.org/choosenim/init.sh -sSf | sh
export PATH="$HOME/.nimble/bin:$PATH"

The script asks before it changes anything. When it finishes, add the export line to ~/.bashrc (or ~/.zshrc on macOS) so new terminals find Nim. Everything lives under ~/.choosenim and ~/.nimble, so no root access is needed for Nim itself. A hand-installed toolchain like this gets no apt security updates: run choosenim update stable to move to a newer release.

Other routes:

  • Distribution package: sudo apt install nim works on Ubuntu and Debian, but the packaged version is often a release or two behind. Check it with nim -v before following this tutorial.
  • macOS: run xcode-select --install for the Clang compiler, then use choosenim as above, or brew install nim.
  • Windows: download the 64-bit zip from nim-lang.org/install_windows.html, unpack it, and follow that page's steps to add Nim to your path and install the MinGW C compiler it links to.

On a prepaid data plan, choosenim may try to download a prebuilt release and fall back to building from source, which takes longer but downloads less than you might expect. The Windows zip is roughly 50 MB.

Check it worked

Open a new terminal and run:

Bash
nim -v
gcc --version

The first line of nim -v says something like Nim Compiler Version 2.4.2 [Linux: amd64]. Any 2.4 release is fine. gcc --version prints a version number; if it says command not found, Nim installed but cannot compile anything yet, so go back and install the C compiler.

Step 2 · Write Hello World and run it

Make a throwaway folder and write the program:

Bash
mkdir -p ~/nim-demo
cd ~/nim-demo
cat > hello.nim <<'NIM'
echo "Hello, World!"
NIM
nim c -r hello.nim

The program is one line:

  • echo is a procedure (Nim's word for a function) that prints its arguments followed by a newline. It comes from the system module, which every Nim file imports automatically.
  • "Hello, World!" is a string literal, the argument.
  • There are no brackets. Nim lets you call a procedure with command syntax, the name, a space and the arguments. echo("Hello, World!") means exactly the same.
  • There is no main function. Statements at the top level of a file run from top to bottom when the program starts.

The command does three things:

  • nim c compiles with the C back end: Nim checks your code, writes C files, then runs GCC on them.
  • -r runs the resulting program straight after a successful build.
  • hello.nim is the file to compile. The program is written next to it as hello (or hello.exe on Windows).

Nim prints a few lines starting with Hint: while it works, ending with a line that contains [SuccessX] and the time taken. Then your program's output appears.

Check it worked

The last line of output is Hello, World!. ls now shows hello next to hello.nim, and ./hello prints the greeting again without compiling: it is an ordinary native program that needs no Nim installation to run.

Step 3 · Read the C that Nim generated

Nim kept its intermediate C files. By default they go to a cache folder in your home directory, named after the project with _d on the end for a debug build (the default) or _r for a build with -d:release:

Bash
ls ~/.cache/nim/hello_d

To look at them more easily, build again and ask Nim to put the cache in a folder inside the project:

Bash
nim c --nimcache:nimcache --hints:off hello.nim
ls nimcache
grep -n "Hello, World" nimcache/*hello.nim.c
  • --nimcache:nimcache sets the cache folder to ./nimcache.
  • --hints:off hides the Hint: lines, so you see only warnings and errors.
  • The file whose name ends in hello.nim.c is your module translated to C. The other .c files are the parts of Nim's standard library your program uses, such as the system module, translated the same way. The .o files are what GCC made from them, and the .json file records the exact compiler commands Nim ran.

Open the file ending in hello.nim.c in your editor. Most of it is setup that Nim's runtime needs, but you will find your string stored as a constant and a call that writes it out. You never need to edit this C, and it changes between Nim versions, but it is a useful window: when you want to know what a line of Nim costs, you can see what it became.

Check it worked

ls nimcache lists several .c and .o files, one of them ending in hello.nim.c, and grep prints at least one line of C containing Hello, World!.

Step 4 · Compile the same file to JavaScript

The JavaScript back end turns the same source into a .js file. To run it in a terminal you need Node.js. Install it from your distribution if you do not have it already:

Bash
sudo apt update
sudo apt install -y nodejs
node --version

Any Node.js version that your distribution ships works for this step. Now compile and run:

Bash
nim js --hints:off -o:hello.js hello.nim
node hello.js
  • nim js uses the JavaScript back end instead of C. No C compiler is involved.
  • -o:hello.js names the output file.
  • node hello.js runs it. On this back end, echo becomes a call to console.log.

The output file is much larger than one line, because it carries the parts of Nim's runtime your program needs. You could load the same file in a web page with a <script> tag and see the greeting in the browser's developer console.

Not everything works on both back ends. JavaScript in a browser has no keyboard input stream, so reading from stdin, which you do next, is only available when compiling to C.

Check it worked

node hello.js prints Hello, World!, the same output as ./hello, from the same hello.nim.

Step 5 · Read a name and greet it with a proc

Write a second program that asks for a name, greets it, and counts its letters:

Bash
cat > greet.nim <<'NIM'
import std/strutils
from std/unicode import runeLen

proc greet(name: string): string =
  if name.len == 0:
    result = "Hello, stranger!"
  else:
    result = "Hello, " & name & "!"

stdout.write "What is your name? "
stdout.flushFile()
let name = readLine(stdin).strip()
let letters = name.runeLen

echo greet(name)
echo "Your name has ", letters, " letters and ", name.len, " bytes."
echo "Nim inferred the type of letters: ", typeof(letters)
NIM
nim c --hints:off greet.nim
echo "Ngozi" | ./greet

Line by line:

  • import std/strutils loads the standard string utilities, for strip.
  • from std/unicode import runeLen loads only runeLen from the Unicode module. Importing just the name you need avoids a clash, because both modules have a procedure called strip.
  • proc greet(name: string): string = defines a procedure called greet. Its parameter name is a string, and it returns a string. In Nim, parameters and return types are always written out.
  • The indented lines under it are its body. Indentation is syntax, as in Python: two spaces per level is the Nim convention.
  • result is a variable every procedure with a return type gets for free. Whatever it holds at the end is returned, so you do not need a return statement.
  • & joins strings. + is only for numbers.
  • stdout.write prints without a newline, so the answer appears after the question. stdout.flushFile() makes sure the question is on screen before the program waits for input.
  • readLine(stdin) reads one line from the keyboard, without the newline, and .strip() removes spaces at both ends.
  • let name = ... declares a variable whose value cannot change. You did not write a type: Nim infers string from the value. Use var for a variable you want to change later.
  • name.runeLen and name.len use method call syntax: name.len is another way to write len(name). Any procedure can be called either way.
  • echo takes several arguments and prints them one after another, turning numbers into text for you.
  • typeof(letters) asks the compiler for the type it inferred, here int.

Why two counts? len counts bytes, and runeLen counts Unicode characters (code points). For names written only in basic Latin letters they match. For names such as Zoë, Adébáyọ̀ or Ọlá, or text in Amharic or Arabic script, each accented or non-Latin character takes two or more bytes in UTF-8, so len is larger. If you are limiting the length of a name field, count characters, not bytes.

Run ./greet on its own to type a name yourself. echo "Ngozi" | types it for you.

Check it worked

The program prints What is your name? Hello, Ngozi! on one line, then Your name has 5 letters and 5 bytes. and Nim inferred the type of letters: int. Typing Zoë gives 3 letters and 4 bytes. Pressing Enter without a name prints Hello, stranger!.

Step 6 · Make it a project with nimble

nimble is Nim's package manager and build tool, installed along with Nim. Ask it to create a project called greeter. It asks a few questions; press Tab until binary is highlighted for the package type, then Enter, and press Enter to accept the default for the rest:

Bash
nimble init greeter

If git has no user.name set, nimble also asks for your name first. A binary package produces a program; a library package provides code for other packages to import.

Look at what it made:

Bash
cd ~/nim-demo/greeter
ls
cat greeter.nimble
cat src/greeter.nim
  • greeter.nimble describes the package: its version, author, licence, that the source is in src, that it builds a binary called greeter, and a requires "nim >= ..." line for the lowest Nim version it supports. Dependencies you add later go on more requires lines.
  • src/greeter.nim is the program. It holds a Hello World inside when isMainModule:, a block that only runs when this file is the program being built, not when another file imports it.

Replace the program with your greeting code, keeping the when isMainModule: block, then build and run it:

Bash
cat > src/greeter.nim <<'NIM'
import std/strutils

proc greet*(name: string): string =
  if name.len == 0:
    result = "Hello, stranger!"
  else:
    result = "Hello, " & name & "!"

when isMainModule:
  stdout.write "What is your name? "
  stdout.flushFile()
  let name = readLine(stdin).strip()
  echo greet(name)
NIM
nimble build
echo "Amina" | ./greeter

The * after greet exports it, so another module could import greeter and call greet. Without the star, a name is private to its file. nimble build reads greeter.nimble, fetches any dependencies listed there (there are none yet), and compiles src/greeter.nim to ./greeter.

Check it worked

ls shows greeter.nimble and src, and after the build also greeter. The last command prints What is your name? Hello, Amina!.

Step 7 · Learn the common mistakes, and where Nim is used

Make one mistake on purpose, so the first real one looks familiar. Join a string and a number with &:

Bash
cd ~/nim-demo
cat > oops.nim <<'NIM'
let age = 30
echo "Age: " & age
NIM
nim c --hints:off oops.nim > errors.txt 2>&1 || echo "Build failed, as expected"
cat errors.txt

Nim refuses to compile it. The message starts with the file, line and column, oops.nim(2, ...), then Error: type mismatch, and lists the versions of & it knows about, none of which takes a string and an int. Fix it with $, which turns a value into a string: echo "Age: " & $age. Or pass two arguments, echo "Age: ", age, as in the greeting program.

Other mistakes beginners hit:

  • Tabs. Nim rejects tab characters for indentation with Error: tabs are not allowed, use spaces instead. Set your editor to insert spaces in .nim files.
  • No C compiler. Nim installs fine without one, but nim c then fails with an error about invoking the external compiler. Install build-essential (Linux), the Xcode command line tools (macOS) or MinGW (Windows).
  • Changing a let. Assigning to a let variable again fails with an error that it cannot be assigned to. Use var when the value must change.
  • Surprised by names. Nim compares identifiers ignoring case and underscores, except for the first letter. readLine, readline and read_line are the same name, so you cannot have two variables called userName and username.
  • stdin in JavaScript. readLine(stdin) does not compile with nim js. Keep keyboard input in programs built with nim c.

Where Nim is used:

  • System tools and command-line programs, where you want a single fast binary with no runtime to install, written in a language that reads like a script. Small Nim binaries also suit low-spec servers and machines with little memory.
  • Fast scripts: the same code you would write in Python, compiled to native speed when a script gets too slow.
  • Games, through bindings to C libraries such as raylib and SDL, which Nim calls with little glue because it compiles to C anyway.
  • Blockchain infrastructure: Status's Nimbus client for Ethereum is written in Nim.

When you have finished, delete the demo folder:

Bash
cd ~
rm -rf ~/nim-demo ~/.cache/nim/hello_d ~/.cache/nim/greet_d ~/.cache/nim/oops_d

The cache folders are safe to delete; Nim recreates them on the next build. Nim, nimble and Node.js stay installed.

Check it worked

errors.txt contained type mismatch and no oops program was made. After cleanup, ls ~/nim-demo reports No such file or directory, while nim -v still prints the 2.4 version.

Next steps

  • Compare with the language whose syntax Nim borrows, which runs your code without a separate compile step: Hello World in Python.
  • See another language that pairs a familiar scripting syntax with a native compiler: Hello World in Crystal.
  • Write C directly and see the compile and link stages Nim drives for you: Hello World in C.
  • Add a dependency to greeter.nimble with a requires line and run nimble build again to watch nimble fetch it.

Need this done for you?

Padding does this work for growing companies, from the first diagnosis to a system that keeps working.

Platform Engineering

Get new tutorials

New tutorials, about twice a month.