Skip to content

Latest commit

 

History

History
88 lines (52 loc) · 4.33 KB

01-installation.adoc

File metadata and controls

88 lines (52 loc) · 4.33 KB

Installation

Installing Nim

Nim has ready made distributions for all three major operating systems and there are several options when it comes to installing Nim.

You can follow the official installation procedure to install the latest stable version, or you can use a tool called choosenim which enables you to easily switch between the stable and the latest development version if you’re interested in the latest features and bugfixes.

Whichever way you choose, just follow the installation procedure explained at each link and Nim should be installed. We will check that the installation went well in a coming chapter.

If you’re using Linux, there is a high probability that your distribution has Nim in the package manager. If you are installing it that way, make sure it’s the most recent version (see the website for what is the latest version), otherwise install via one of two methods mentioned above.

In this tutorial we will use the stable version. Originally, this tutorial was written for Nim 0.19 (released in September 2018), and it should work for any newer version, including Nim 1.x and Nim 2.x.

Installing additional tools

You can write Nim code in any text editor, and then compile and run it from the terminal. If you want syntax highlighting and code completion there are plugins for popular code editors which provide these features.

Most of Nim users prefer the VS Code editor, with the Nim extension which provides syntax highlighting and code completion, and the Code Runner extension for quick compiling and running.

The author personally uses NeoVim editor, with this plugin which provides additional features like syntax highlighting and code completion.

If you’re using other code editors, see the wiki for available editor support.

Testing the installation

To check if the installation was successful, we will write a program which is traditionally used as an introductory example: Hello World.

Printing (as in: displaying on the screen; not on a paper with a printer) the phrase Hello World! in Nim is straightforward and it doesn’t require any boilerplate code.

In a new text file called e.g. helloworld.nim we need to write just one line of code:

helloworld.nim
link:code/helloworld.nim[role=include]
Note
The phrase you want to print must follow the echo command and must be enclosed in double-quotes (").

First we need to compile our program, and then run it to see if it works as expected.

Open your terminal in the same directory where your file is (on Linux you can get "Open Terminal here" if you right-click the directory in your file manager, on Windows you should use Shift + right-click to get the menu option for opening the command line).

We compile our program by typing in the terminal:

nim c helloworld.nim

After a successful compilation, we can run our program. On Linux we can run our program by typing ./helloworld in the terminal, and on Windows we do it by typing helloworld.exe.

There is also a possibility to both compile and run the program with just one command. We need to type:

nim c -r helloworld.nim
Note
c is telling Nim to compile the file, and -r is telling it to run it immediately.
To see all compiler options, type nim --help in your terminal.

If you’re using VSCode with the Code Runner extension mentioned before, you’ll just have to press Ctrl+Alt+N and your file will be compiled and run.

Whichever way you chose to run your program, after a brief moment in the output window (or in your terminal) you should see:

Hello World!

Congratulations, you have successfully run your first Nim program!

Now you know how to print some stuff on the screen (using the echo command), compile your program (typing nim c programName.nim in your terminal), and run it (various possibilities).

We can now start to explore the basic elements which will help us to write simple Nim programs.