如何在Linux上安装NIM编程语言

时间:2020-03-21 11:44:40  来源:igfitidea点击:

本教程介绍了如何在Linux上安装NIM编程语言。
对于那些想知道的人来说,NIM是一种可兑换,命令,多范式和编译的编程语言,可用于写入易于阅读的高性能代码。
它是一种相对较新的编程语言,并将成功的概念与Python,ADA和Modula等成熟语言相结合。
NIM支持所有主要操作系统,包括Linux,BSD,MacOS和Microsoft Windows。

在Linux上安装NIM编程语言

在安装NIM之前,请确保完成以下准备工作。

NIM编译器需要C编译器才能编译代码。
因此,我们应该在Linux系统中安装Clang或者GCC编译器。

NIM可以以多种方式安装。
以下是在Linux上安装NIM的三种不同方式。

1.使用Chooryim安装NIM(正式推荐)

官方推荐的申请Nim方法是使用Choentim。
类似于Rustup和Pyenv,Choentim是工具链多路复用器。

它可以帮助用户安装多个版本的NIM编译器以及所有必需的工具。
然后,我们可以轻松地在不同的NIM版本之间切换,并选择任何NIM版本点击。

安装GCC编译器后,运行以下命令以安装ChooryIM Toolchain Installer的最新稳定的NIM版本。

$curl https://nim-lang.org/choosenim/init.sh -sSf | sh

按照屏幕上的说明进行操作。
首先,我们将被问到我们是否想允许Chooryim录制和发送匿名遥测数据。
如果我们同意发送数据,请按"Y"并按Enter键。
如果我们不想要,请按"N"。

choosenim-init: Downloading choosenim-0.6.0_linux_amd64
Prompt: Can choosenim record and send anonymised telemetry data? [y/n]
... Anonymous aggregate user analytics allow us to prioritise
... fixes and features based on how, where and when people use Nim.
... For more details see: https://goo.gl/NzUEPf.

安装成功后,我们将看到以下消息。

Downloading Nim 1.0.6 from nim-lang.org
[################] 100.0% 0kb/s
 Extracting nim-1.0.6-linux_x64.tar.xz
   Building Nim 1.0.6
  Compiler: Already built
     Tools: Already built
  Installed component 'nim'
  Installed component 'nimble'
  Installed component 'nimgrep'
  Installed component 'nimpretty'
  Installed component 'nimsuggest'
  Installed component 'testament'
   Switched to Nim 1.0.6
choosenim-init: ChooseNim installed in /home/sk/.nimble/bin
choosenim-init: You must now ensure that the Nimble bin dir is in your PATH.
choosenim-init: Place the following line in the ~/.profile or ~/.bashrc file.
choosenim-init:     export PATH=/home/sk/.nimble/bin:$PATH

如我们所见,yim和其他所需的组件已安装在~/.nimble/bin目录中。
我们应该确保$Path中添加Nimble Bin目录。
为此,请编辑~/.bashrc或者~/.profile文件:

$nano ~/.bashrc

或者,

$nano ~/.profile

添加以下行:

export PATH=/home/sk/.nimble/bin:$PATH

用自己的方式替换"/home/sk/..."路径。

最后使用命令更新更改:

$source ~/.bashrc

如果我们在~/.profile中添加了该行,请执行以下操作:

$source ~/.profile

要检查已安装的NIM版本,只需运行:

$nim -v

作为撰写本教程,最新的稳定NIM版本为1.0.6.

Nim Compiler Version 1.0.6 [Linux: amd64]
Compiled at 2017-01-23
Copyright (c) 2006-2019 by Andreas Rumpf
active boot switches: -d:release

切换到NIM开发版本

默认情况下,Chooryim将安装下载并安装最新的稳定NIM版本。
如果要安装开发版本,请运行:

$choosenim devel

示例输出:

Downloading Nim devel from GitHub
[################] 100.0% 0kb/s
Extracting nim-1.1.1-linux_x64.tar.xz
Setting up git repository
Building Nim #devel
Compiler: Already built
Tools: Already built
Switched to Nim #devel

验证NIM版本:

$nim -v
Nim Compiler Version 1.1.1 [Linux: amd64]
Compiled at 2017-04-02
Copyright (c) 2006-2017 by Andreas Rumpf
active boot switches: -d:release

要切换回稳定版本,请运行:

$choosenim stable
Switched to Nim 1.0.6

列表可用NIM版本

要查看所有可用的NIM版本的列表,请运行:

$choosenim versions

示例输出:

Channel: stable
 Installed:  
          * 1.0.6 (latest)
            #devel
 Available:  
            1.0.4
            1.0.2
            1.0.0
            0.20.2
            0.20.0
            0.19.6
            0.19.4
            0.19.2
            0.19.0
            0.18.0
            0.17.2
            0.17.0
            0.16.0

上面的命令显示已安装的NIM版本和可用的NIM版本列表。

安装特定的NIM版本

如果我们想要为项目进行特定的NIM版本,则可以如下安装。

$choosenim 1.0.4

此命令安装并选择NIM的1.0.4版本。

更新nim

NIM可以如下更新。

nim稳定:

$choosenim update stable

NIM开发版本:

$choosenim update devel

更新特定的NIM版本:

$choosenim update 1.0.6

要更新Chooryim本身,请运行:

$choosenim update self

求助,

$choosenim -h

示例输出:

choosenim: The Nim toolchain installer.
Choose a job. Choose a mortgage. Choose life. Choose Nim.
Usage:
  choosenim <version/path/channel>
Example:
  choosenim 0.16.0
    Installs (if necessary) and selects version 0.16.0 of Nim.
  choosenim stable
    Installs (if necessary) Nim from the stable channel (latest stable release)
    and then selects it.
  choosenim devel [--latest]
    Installs (if necessary) and selects the most recent nightly build of Nim.
    The '--latest' flag selects and builds the latest commit in the devel branch
  choosenim ~/projects/nim
    Selects the specified Nim installation.
  choosenim update stable
    Updates the version installed on the stable release channel.
  choosenim update devel [--latest]
    Updates to the most recent nightly build of Nim.
    The '--latest' flag updates and builds the latest commit in the devel branch
  choosenim versions [--installed]
    Lists the available versions of Nim that choosenim has access to.
Channels:
  stable
    Describes the latest stable release of Nim.
  devel
    Describes the latest development (or nightly) release of Nim taken from
    the devel branch.
Commands:
  update    <version/channel>    Installs the latest release of the specified
                                 version or channel.
  show                           Displays the selected version and channel.
  update    self                 Updates choosenim itself.
  versions  [--installed]        Lists available versions of Nim, passing
                                 `--installed` only displays versions that
                                 are installed locally (no network requests).
Options:
  -h --help             Show this output.
  -y --yes              Agree to every question.
  --version             Show version.
  --verbose             Show low (and higher) priority output.
  --debug               Show debug (and higher) priority output.
  --noColor             Don't colorise output.
  --choosenimDir:<dir>  Specify the directory where toolchains should be
                        installed. Default: ~/.choosenim.
  --nimbleDir:<dir>     Specify the Nimble directory where binaries will be
                        placed. Default: ~/.nimble.
  --firstInstall        Used by install script.

2.使用发行版包管理器安装NIM

NIM包装许多流行的Linux发行版,可在官方存储库中提供。

在Arch Linux及其变体如Manjaro Linux,运行以下命令以安装NIM:

$sudo pacman -S nim

在Debian,Ubuntu和衍生品:

$sudo apt install nim

关于Fedora:

$sudo dnf install nim

OpenSUSE:

$sudo zypper install nim

在void Linux上:

$xbps-install -S nim

NIM也可用作捕捉包装。
如果Linux发行版具有Snapd,请将NIM安装如下。

最新稳定版本:

$snap install nim-lang --classic

最新LTS 1.0.x版本:

$snap install nim-lang-lts-1 --classic

最新的夜间建立:

$snap install nim-lang-nightly --classic

请注意,官方存储库中的NIM版本可能会缩短。
如果我们想要最近的版本,请使用Phooryim安装程序如前一个方法中所述。

3.使用Docker安装

DockerHub上有社区管理的NIM Docker图像。

安装Docker后,使用命令获取最新的稳定NIM Docker镜像:

$docker pull nimlang/nim

获取最新开发版本:

$docker pull nimlang/nim:devel

测试NIM编程语言

要验证是否已成功安装了NIM安装,请让我们使用NIM进行简单的程序。

创建文本文件,例如theitroad.nim:

$nano theitroad.nim

添加以下行:

echo "Welcome To theitroad!"

保存并关闭文件。

使用命令编译文件:

$nim c theitroad.nim

示例输出:

Hint: used config file '/home/sk/.choosenim/toolchains/nim-1.0.6/config/nim.cfg' [Conf]
Hint: system [Processing]
Hint: widestrs [Processing]
Hint: io [Processing]
Hint: theitroad [Processing]
Hint: gcc -c  -w  -I/home/sk/.choosenim/toolchains/nim-1.0.6/lib -I/home/sk -o /home/sk/.cache/nim/theitroad_d/stdlib_io.nim.c.o /home/sk/.cache/nim/theitroad_d/stdlib_io.nim.c [Exec]
Hint: gcc -c  -w  -I/home/sk/.choosenim/toolchains/nim-1.0.6/lib -I/home/sk -o /home/sk/.cache/nim/theitroad_d/stdlib_system.nim.c.o /home/sk/.cache/nim/theitroad_d/stdlib_system.nim.c [Exec]
Hint: gcc -c  -w  -I/home/sk/.choosenim/toolchains/nim-1.0.6/lib -I/home/sk -o /home/sk/.cache/nim/theitroad_d/@mtheitroad.nim.c.o /home/sk/.cache/nim/theitroad_d/@mtheitroad.nim.c [Exec]
Hint:  [Link]
Hint: operation successful (14483 lines compiled; 1.894 sec total; 16.008MiB peakmem; Debug Build) [SuccessX]

最后,使用命令运行程序:

$./theitroad

示例输出:

Welcome To theitroad!

或者,在一个下面编译和运行,如下所示:

$nim c -r theitroad.nim

示例输出:

Hint: used config file '/home/sk/.choosenim/toolchains/nim-1.0.6/config/nim.cfg' [Conf]
Hint: system [Processing]
Hint: widestrs [Processing]
Hint: io [Processing]
Hint: theitroad [Processing]
Hint: [Link]
Hint: operation successful (14483 lines compiled; 0.405 sec total; 20.898MiB peakmem; Debug Build) [SuccessX]
Hint: /home/sk/theitroad [Exec]
Welcome To theitroad!

要了解更多关于NIM使用情况,请参阅最后的官方NIM基础文档。

卸载NIM

如果我们已使用Chooryim安装了Nim,只需删除NIM安装目录:

$rm -r ~/.nimble/~/.choosenim/

如果已使用分发的包管理器安装了它,只需运行uninstall命令。
例如,在Debian,Ubuntu,Nim可以使用以下命令删除:

$sudo apt-get purge --auto-remove nim