First steps with LXD

This tutorial guides you through the first steps with LXD. It covers installing and initializing LXD, creating and configuring some instances, interacting with the instances, and creating snapshots.

After going through these steps, you will have a general idea of how to use LXD, and you can start exploring more advanced use cases!

Note

Ensure that you have 20 GiB free disk space before starting this tutorial.

Install and initialize LXD

The easiest way to install LXD is to install the snap package. If you prefer a different installation method, or use a Linux distribution that is not supported by the snap package, see How to install LXD.

  1. Install snapd:

    1. Run snap version to find out if snap is installed on your system:

      user@host:~$ snap version
      snap    2.63+24.04ubuntu0.1snapd   2.63+24.04ubuntu0.1series  16ubuntu  24.04kernel  5.15.0-117-generic

      If you see a table of version numbers, snap is installed and you can continue with the next step of installing LXD.

    2. If the command returns an error, run the following commands to install the latest version of snapd on Ubuntu:

      sudo apt update
      sudo apt install snapd
      

      Note

      For other Linux distributions, see the installation instructions in the Snapcraft documentation.

  2. Enter the following command to install LXD:

    sudo snap install lxd
    

    If you get an error message that the snap is already installed, run the following command to refresh it and ensure that you are running an up-to-date version:

    sudo snap refresh lxd
    
  3. Check if the current user is part of the lxd group (the group was automatically created during the previous step):

    getent group lxd | grep "$USER"
    

    If this command returns a result, you’re set up correctly and can continue with the next step.

    If there is no result, enter the following commands to add the current user to the lxd group (which is needed to grant the user permission to interact with LXD):

    sudo usermod -aG lxd "$USER"
    newgrp lxd
    
  4. Enter the following command to initialize LXD:

    lxd init --minimal
    

    This will create a minimal setup with default options. If you want to tune the initialization options, see How to initialize LXD for more information.

Launch and inspect instances

LXD is image based and can load images from different image servers. In this tutorial, we will use the official ubuntu: image server.

You can list all images (long list) that are available on this image server with:

lxc image list ubuntu:

You can list the images used in this tutorial with:

lxc image list ubuntu: 24.04 architecture=$(uname -m)

See Images for more information about the images that LXD uses.

Now, let’s start by launching a few instances. With instance, we mean either a container or a virtual machine. See About containers and VMs for information about the difference between the two instance types.

For managing instances, we use the LXD command line client lxc. See About lxd and lxc if you are confused about when to use the lxc command and when to use the lxd command.

  1. Launch a container called first using the Ubuntu 24.04 LTS image:

    lxc launch ubuntu:24.04 first
    

    Note

    Launching this container takes a few seconds, because the image must be downloaded and unpacked first.

  2. Launch a container called second using the same image:

    lxc launch ubuntu:24.04 second
    

    Note

    Launching this container is quicker than launching the first, because the image is already available locally.

  3. Copy the first container into a container called third:

    lxc copy first third
    
  4. Launch a VM called ubuntu-vm using the Ubuntu 24.04 LTS image:

    lxc launch ubuntu:24.04 ubuntu-vm --vm
    

    Note

    Even though you are using the same image name to launch the instance, LXD downloads a slightly different image that is compatible with VMs.

  5. Check the list of instances that you launched:

    lxc list
    

    You will see that all but the third container are running. This is because you created the third container by copying the first, but you didn’t start it.

    You can start the third container with:

    lxc start third
    
  6. Query more information about each instance with:

    lxc info first
    lxc info second
    lxc info third
    lxc info ubuntu-vm
    
  7. We don’t need all of these instances for the remainder of the tutorial, so let’s clean some of them up:

    1. Stop the second container:

      lxc stop second
      
    2. Delete the second container:

      lxc delete second
      
    3. Delete the third container:

      lxc delete third
      

      Since this container is running, you get an error message that you must stop it first. Alternatively, you can force-delete it:

      lxc delete third --force
      

See How to create instances and How to manage instances for more information.

Configure instances

There are several limits and configuration options that you can set for your instances. See Instance options for an overview.

Let’s create another container with some resource limits:

  1. Launch a container and limit it to one vCPU and 192 MiB of RAM:

    lxc launch ubuntu:24.04 limited --config limits.cpu=1 --config limits.memory=192MiB
    
  2. Check the current configuration and compare it to the configuration of the first (unlimited) container:

    lxc config show limited
    lxc config show first
    
  3. Check the amount of free and used memory on the parent system and on the two containers:

    free -m
    lxc exec first -- free -m
    lxc exec limited -- free -m
    

    Note

    The total amount of memory is identical for the parent system and the first container, because by default, the container inherits the resources from its parent environment. The limited container, on the other hand, has only 192 MiB available.

  4. Check the number of CPUs available on the parent system and on the two containers:

    nproc
    lxc exec first -- nproc
    lxc exec limited -- nproc
    

    Note

    Again, the number is identical for the parent system and the first container, but reduced for the limited container.

  5. You can also update the configuration while your container is running:

    1. Configure a memory limit for your container:

      lxc config set limited limits.memory=128MiB
      
    2. Check that the configuration has been applied:

      lxc config show limited
      
    3. Check the amount of memory that is available to the container:

      lxc exec limited -- free -m
      

      Note that the number has changed.

  6. Depending on the instance type and the storage drivers that you use, there are more configuration options that you can specify. For example, you can configure the size of the root disk device for a VM:

    1. Check the current size of the root disk device of the Ubuntu VM:

      user@host:~$ lxc exec ubuntu-vm -- df -h
      Filesystem      Size  Used Avail Use% Mounted on/dev/root       9.6G  1.4G  8.2G  15% /tmpfs           483M     0  483M   0% /dev/shmtmpfs           193M  604K  193M   1% /runtmpfs           5.0M     0  5.0M   0% /run/locktmpfs            50M   14M   37M  27% /run/lxd_agent/dev/sda15      105M  6.1M   99M   6% /boot/efi
    2. Override the size of the root disk device:

      lxc config device override ubuntu-vm root size=30GiB
      
    3. Restart the VM:

      lxc restart ubuntu-vm
      
    4. Check the size of the root disk device again:

      user@host:~$ lxc exec ubuntu-vm -- df -h
      Filesystem      Size  Used Avail Use% Mounted on/dev/root        29G  1.4G   28G   5% /tmpfs           483M     0  483M   0% /dev/shmtmpfs           193M  588K  193M   1% /runtmpfs           5.0M     0  5.0M   0% /run/locktmpfs            50M   14M   37M  27% /run/lxd_agent/dev/sda15      105M  6.1M   99M   6% /boot/efi

See How to configure instances and Instance configuration for more information.

Interact with instances

You can interact with your instances by running commands in them (including an interactive shell) or accessing the files in the instance.

Start by launching an interactive shell in your instance:

  1. Run the bash command in your container:

    lxc exec first -- bash
    
  2. Enter some commands, for example, display information about the operating system:

    cat /etc/*release
    
  3. Exit the interactive shell:

    exit
    

Instead of logging on to the instance and running commands there, you can run commands directly from the host.

For example, you can install a command line tool on the instance and run it:

lxc exec first -- apt-get update
lxc exec first -- apt-get install sl -y
lxc exec first -- /usr/games/sl

See How to run commands in an instance for more information.

You can also access the files from your instance and interact with them:

  1. Pull a file from the container:

    lxc file pull first/etc/hosts .
    
  2. Add an entry to the file:

    echo "1.2.3.4 my-example" >> hosts
    
  3. Push the file back to the container:

    lxc file push hosts first/etc/hosts
    
  4. Use the same mechanism to access log files:

    lxc file pull first/var/log/syslog - | less
    

    Note

    Press q to exit the less command.

See How to access files in an instance for more information.

Manage snapshots

You can create a snapshot of your instance, which makes it easy to restore the instance to a previous state.

  1. Create a snapshot called “clean”:

    lxc snapshot first clean
    
  2. Confirm that the snapshot has been created:

    lxc list first
    lxc info first
    

    Note

    lxc list shows the number of snapshots. lxc info displays information about each snapshot.

  3. Break the container:

    lxc exec first -- rm /usr/bin/bash
    
  4. Confirm the breakage:

    lxc exec first -- bash
    

    Note

    You do not get a shell, because you deleted the bash command.

  5. Restore the container to the state of the snapshot:

    lxc restore first clean
    
  6. Confirm that everything is back to normal:

    lxc exec first -- bash
    exit
    
  7. Delete the snapshot:

    lxc delete first/clean
    

See Use snapshots for instance backup for more information.

Next steps

Now that you’ve done your first experiments with LXD, you should read up on important concepts in the Explanation section and check out the How-to guides to start working with LXD!