Installing Nix on Ubuntu
This guide walks you through installing Nix on Ubuntu using the official supported method. Nix is a powerful package manager that enables reproducible development environments and declarative system configuration.
1. Overview
Installation Method:
Multi-user installation with daemon mode (recommended for Ubuntu)
What Nix Provides:
- ✅ Reproducible Environments: Consistent toolchains across machines
- ✅ Declarative Configuration: Define environments as code
- ✅ Isolated Packages: No conflicts between package versions
- ✅ Cross-Platform: Same configuration works on Linux, macOS, and WSL
Key Features:
- System-wide installation in
/nix - Dedicated build users for security
- Flakes support for modern workflows
- Compatible with all Ubuntu versions
2. Prerequisites
Before starting, ensure you have:
- Ubuntu (any recent version)
- sudo access (required for multi-user installation)
- Internet connection (for downloading Nix installer)
- curl (will be installed if missing)
- Basic familiarity with Linux command line
3. Update Your System
sudo apt update && sudo apt upgrade -y
4. Install Required Dependency
Nix requires curl to download the installer. Install it if it's not already present:
sudo apt install curl -y
5. Run the Official Nix Installer (Multi-User Mode)
This is the recommended installation method for Ubuntu. The multi-user installation provides better security and isolation.
sh <(curl -L https://nixos.org/nix/install) --daemon
What this does:
- Creates dedicated build users (
nixbld1,nixbld2, etc.) for secure package building - Installs Nix into
/nix(system-wide location) - Configures Nix daemon for multi-user access
- Sets up proper permissions and security isolation
You may be prompted for sudo access during installation. This is normal and required for the multi-user setup.
Installation Time: The installation typically takes 2-5 minutes depending on your internet connection.
6. Load Nix into Your Shell
After installation finishes, you need to load Nix into your current shell session. Either restart your terminal or run:
. /etc/profile.d/nix.sh
This command sources the Nix profile script, making Nix commands available in your current shell.
Note: After restarting your terminal or opening a new session, Nix will be automatically available. You don't need to run this command every time.
7. Verify Nix Installation
Verify that Nix is installed correctly:
nix --version
Expected output:
nix (Nix) 2.x.x
The version number will vary depending on the latest release. If you see a version number, Nix is installed correctly.
8. Test Nix by Running a Package
Test Nix by running a simple package. The hello package is a classic test:
nix-shell -p hello
hello
Expected output:
Hello, world!
This confirms that Nix can download, build, and run packages correctly.
Note: The nix-shell -p hello command creates a temporary shell environment with the hello package available. Type exit to leave the shell when done.
9. Enable Flakes (Recommended)
Flakes are the modern way to use Nix, providing better reproducibility and easier sharing of configurations. Enable them by creating the Nix configuration directory and adding the experimental features:
Step 1: Create the config directory
sudo mkdir -p /etc/nix
Step 2: Enable flakes and the new CLI
echo "experimental-features = nix-command flakes" | sudo tee -a /etc/nix/nix.conf
Step 3: Restart your shell
Open a new terminal session or restart your current one for the changes to take effect.
Verify flakes are enabled:
nix flake --version
You should see output indicating flakes are available.
10. Quick Example: Using Flakes
Now that flakes are enabled, try a simple example:
mkdir -p ~/nix-test && cd ~/nix-test
cat > flake.nix <<'EOF'
{
description = "Simple Nix flake example";
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
outputs = { self, nixpkgs }:
let
system = "x86_64-linux";
pkgs = import nixpkgs { inherit system; };
in {
devShells.${system}.default = pkgs.mkShell {
packages = [ pkgs.hello ];
};
};
}
EOF
Enter the development shell:
nix develop
Then run:
hello
You should see "Hello, world!" confirming flakes are working correctly.
11. Best Practices Summary
- ✅ Use Multi-User Installation: Always use
--daemonflag for better security and isolation - ✅ Enable Flakes: Flakes are the future of Nix and provide better reproducibility
- ✅ Keep System Updated: Regularly update your Ubuntu system before installing Nix
- ✅ Use Flake-Based Projects: Prefer
flake.nixovershell.nixfor new projects - ✅ Version Control Configurations: Commit your
flake.nixfiles to share environments across teams - ✅ Use Nix Develop: Prefer
nix developovernix-shellfor flake-based projects
12. Troubleshooting
Issue: Nix commands not found after installation
Solution: Restart your terminal or run . /etc/profile.d/nix.sh
Issue: Permission denied errors
Solution: Ensure you have sudo access and the Nix daemon is running:
sudo systemctl status nix-daemon
Issue: Flakes not working
Solution: Verify the configuration was added correctly:
cat /etc/nix/nix.conf
Make sure you see experimental-features = nix-command flakes in the output.
13. References
- Nix Official Documentation: https://nixos.org/manual/nix/stable/
- Nix Installation Guide: https://nixos.org/download.html
- Nix Flakes Documentation: https://nixos.wiki/wiki/Flakes
- Nix Package Search: https://search.nixos.org/
- NixOS Wiki: https://nixos.wiki/
Top comments (0)