How to Install nim on Ubuntu 16.04

Overview

In this tutorial we’ll learn how to install Nim on Ubuntu 16.04. We will install Nim from the Ubuntu repository and also install nim from source.

Nim is :

systems and applications programming language. Statically typed and compiled, it provides unparalleled performance in an elegant package.

Nim provides:

  • High-performance garbage-collected languag
  • Compiles to C, C++ or JavaScript
  • Produces dependency-free binaries
  • Runs on Windows, MacOS, Linux and more

Install nim From Repository

Nim is available on [Ubuntu 16.04 repository][1].

To install Nim we can run the command below.


$ sudo apt-get update
$ sudo apt-get install -y nim

Now we can check Nim version installed with contents below:


$ nim -v
Nim Compiler Version 0.12.0 (2015-11-02) [Linux: amd64]
Copyright (c) 2006-2015 by Andreas Rumpf

active boot switches: -d:release

Hello Nim

Let’s create hello world application for Nim. Create new file called hello.nim with contents below:


echo "Hello Nim! - howtodojo.com"

```

Compile the source code using the command below.

```

$ nim c hello.nim 
Hint: system [Processing]
Hint: hello [Processing]
CC: hello
CC: system
Error: execution of an external compiler program 'gcc -c  -w  -I/usr/lib/nim -o /home/ubuntu/nimcache/hello.o /home/ubuntu/nimcache/hello.c' failed with exit code: 32512

/bin/sh: 1: gcc: not found

```

We still get an error since we don't have GCC installed. Yes, nim relies on the c / c++ compiler to compile.

Install GCC using the command below

```

$ sudo apt-get install gcc

```

Now let's recompile our `hello.nim` source code.

```

$ nim c hello.nim
Hint: system [Processing]
Hint: hello [Processing]
CC: hello
CC: system
Hint:  [Link]
Hint: operation successful (9852 lines compiled; 0.910 sec total; 14.148MB; Debug Build) [SuccessX]

```

Source code compiled sucessfully. The binary file name is `hello`. We can check the file type using `file` command.

```

$ file hello
hello: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=fe9a2d66209c2adb082ae63e15fc35e8f991a322, not stripped

```

We can see above that hello is a ELF / Linux binary file.

Now let's execute the hello application.

```

$ ./hello 
Hello Nim! - howtodojo.com

```

The application runs successfully.

## Install nim From Source

As you can see above, Nim version shipped with Ubuntu 16.04 is 0.12.0.

At the time of this writing, the latest stable version of Nim is 0.17.2. In this section, we'll learn how to install nim from the source.

We can download the latest version of Nim from [Nim install Unix][2] page.

Download nim source code using `wget`.

```

$ wget -c https://nim-lang.org/download/nim-0.17.2.tar.xz

```

Download the sha256 file that contains a hash for nim source code file. We will use this file to ensure the integrity of the nim source code.

```

$ wget -c https://nim-lang.org/download/nim-0.17.2.tar.xz.sha256  

```

Now let's check nim source code integrity using `sha256sum` command

```

$ sha256sum -c nim-0.17.2.tar.xz.sha256
nim-0.17.2.tar.xz: OK

```

If should output OK as shown below. If not then you will need to redownload nim source code.

Extract nim source code using command below.

```

$ tar xJf nim-0.17.2.tar.xz

```

Go to extracted directory

```

$ cd nim-0.17.2    

```

Run the command below to build nim and tools. We assume that you already install `gcc` before compiling Nim.

```

$ ./build.sh
$ bin/nim c koch
$ ./koch tools        

```

Install nim to /opt directory.

```

$ sudo ./install.sh /opt

```

Check the new Nim version installed on `opt`.

```

$ /opt/nim/bin/nim -v
Nim Compiler Version 0.17.2 (2017-09-07) [Linux: amd64]
Copyright (c) 2006-2017 by Andreas Rumpf

git hash: 811fbdafd958443ddac98ad58c77245860b38620
active boot switches: -d:release

```

## Summary

In this tutorial, we learn how to install Nim from the Ubuntu repository. We learn how to compile Hello Nim! source code to test our installation.

We also learn how to install Nim from source code. Have fun with Nim!

 [1]: https://launchpad.net/ubuntu/xenial/+package/nim
 [2]: https://nim-lang.org/install_unix.html