Introduction / Why This Is Needed
Nim is a statically typed, compiled programming language that combines Python's expressiveness with C's performance. It is ideal for systems programming, web servers, games, and embedded systems.
After completing this guide, you will be able to:
- Compile and run Nim programs from the terminal.
- Use the
nimblepackage manager. - Develop in VS Code with syntax highlighting and autocompletion.
- Create and manage Nim projects.
This guide is intended for Nim beginners but requires basic familiarity with the Linux terminal.
Prerequisites / Preparation
Before you begin, ensure:
- You have access to a Linux terminal (Ubuntu, Fedora, Debian, Arch, or another distribution).
- Basic build tools are installed:
- Ubuntu/Debian:
sudo apt update && sudo apt install build-essential curl - Fedora:
sudo dnf groupinstall "Development Tools" && sudo dnf install curl - Arch:
sudo pacman -S base-devel curl
- Ubuntu/Debian:
- You have write permissions in your home directory (
~).
If you plan to use VS Code, download it from the official website or install it via your distribution's package manager.
Step 1: Install the choosenim Script
Choosenim is the official Nim installation and version management script. It downloads the compiler, configures paths, and allows easy switching between versions.
- Open your terminal.
- Run the command to download and execute the script:
curl https://nim-lang.org/choosenim/init.sh -sSf | sh
- The script will propose installing choosenim to
~/.choosenim. PressEnterto confirm or typey. - After completion, you will see a success message.
⚠️ Important: If curl is not installed, install it first:
sudo apt install curl(Ubuntu/Debian) orsudo dnf install curl(Fedora).
Step 2: Install the Nim Compiler
Now that choosenim is installed, use it to install the latest stable version of Nim:
~/.choosenim/bin/choosenim stable
The command:
- Downloads Nim binaries (compiler, standard library).
- Installs them to
~/.nimble. - Automatically configures environment variables.
The process may take a few minutes depending on your internet speed.
After completion, check the version:
nim --version
The output should look similar to:
Nim Compiler Version 2.0.0 [Linux: amd64]
Compiled with '--cc:gcc' for Linux/amd64
Step 3: Configure the PATH Environment Variable
To make the nim and nimble commands available from anywhere, you need to add the ~/.nimble/bin path to your PATH variable.
- Identify which shell you are using:
- Bash: file
~/.bashrc - Zsh: file
~/.zshrc - Fish: file
~/.config/fish/config.fish
- Bash: file
- Open the corresponding file in a text editor (e.g.,
nano ~/.bashrc). - Add this line to the end of the file:
export PATH="$HOME/.nimble/bin:$PATH"
- Save the file (
Ctrl+Oin nano, thenEnter) and close the editor (Ctrl+X). - Apply the changes:
- For Bash/Zsh:
source ~/.bashrcorsource ~/.zshrc - For Fish:
source ~/.config/fish/config.fish
- For Bash/Zsh:
- Verify that the commands are available:
which nim
which nimble
Both commands should return a path starting with /home/your_user/.nimble/bin/.
Step 4: Install Code Editor and Extension
While you can write Nim in any text editor, VS Code with the extension provides the best experience: syntax highlighting, autocompletion, debugging.
- Install VS Code (if not already installed):
- Ubuntu/Debian:
sudo apt install code(via Microsoft repository) or download the.debfrom the website. - Fedora:
sudo dnf install code - Arch:
sudo pacman -S code
- Ubuntu/Debian:
- Install the Nim extension:
- Launch VS Code.
- Go to the Extensions section (squares icon on the left).
- Search for
nim. - Select the official Nim extension (publisher:
nim-lang). - Click Install.
- Verify the extension works:
- Create a file with the
.nimextension. - Start typing code—autocompletion should appear.
- Create a file with the
💡 Tip: For more advanced development, also consider the Nim Language Server extension (if available) or configure
nimlspmanually.
Step 5: Create and Run a Test Project
Now let's create a simple application to confirm everything works.
- Create a project directory and navigate to it:
mkdir ~/nim_projects
cd ~/nim_projects
- Create a
hello.nimfile:
cat > hello.nim << 'EOF'
echo "Hello, world!"
EOF
- Compile and run it:
nim c -r hello.nim
The nim c command compiles the program into an executable, and -r runs it immediately.
The output should be:
Hello, world!
- You can also compile in optimized release mode:
nim c -d:release hello.nim
./hello
Step 6: Install the Nimble Package Manager
Nimble is the package manager for Nim, similar to npm for Node.js or pip for Python. It is usually installed automatically with Nim, but let's verify:
- Check the version:
nimble --version
If the command is not found, install it manually:
git clone https://github.com/nim-lang/nimble.git
cd nimble
git checkout v0.14.0 # or the latest version
nim c -d:release src/nimble.nim
sudo mv bin/nimble /usr/local/bin/
- Update the package list:
nimble refresh
- Try installing a package, for example
json:
nimble install json
Verification
Run the following commands and ensure they output information without errors:
nim --version
nimble --version
nim c -r hello.nim # from the test project directory
code --version # if VS Code is installed
Also, open VS Code, create a test.nim file, and check if syntax highlighting and autocompletion work.
Potential Issues
1. Error nim: command not found after installation
Cause: The ~/.nimble/bin path is not added to PATH.
Solution: Repeat Step 3, ensuring the line export PATH="$HOME/.nimble/bin:$PATH" is added to the correct shell configuration file and that you reload it (source ~/.bashrc).
2. Choosenim cannot download Nim (SSL or network errors)
Cause: Network issues or outdated certificates. Solution:
- Ensure you have an internet connection.
- Update
ca-certificates:sudo apt update && sudo apt install ca-certificates(Ubuntu/Debian). - Temporarily disable SSL verification (not recommended):
export CHOOSENIM_NO_VERIFY=1and run choosenim again.
3. Compilation errors due to insufficient memory
Cause: Nim requires sufficient RAM to compile large projects. Solution: Increase the swap file size or close other applications. This rarely occurs for small projects.
4. VS Code does not highlight Nim syntax
Cause: The extension is not activated for the current file. Solution:
- Ensure the file has a
.nimextension. - In the bottom-right corner of VS Code, select the
Nimlanguage (if prompted). - Restart VS Code.
5. Nimble cannot find a package
Cause: The package list is not updated or there are network restrictions.
Solution: Run nimble refresh and try again. If the package is not in the official repository, it may need to be installed from GitHub.
6. Error cannot open file: std/... during compilation
Cause: The Nim installation was incorrect or corrupted. Solution: Reinstall Nim via choosenim:
choosenim uninstall
choosenim stable
Ensure there are no old files in ~/.nimble that might conflict.
Environment ready! You can now study the Nim language, create projects, and use the rich package ecosystem via Nimble. For further learning, refer to the official Nim tutorial.