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.
Install
snapd
:Run
snap version
to find out if snap is installed on your system:user@host:~$
snap version
snap 2.63+24.04ubuntu0.1
snapd 2.63+24.04ubuntu0.1
series 16
ubuntu 24.04
kernel 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.
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.
Enter the following command to install LXD:
sudo snap install lxd
If you get an error message that the LXD 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
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
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 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.
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.
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.
Copy the first container into a container called
third
:lxc copy first third
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.
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
Query more information about each instance with:
lxc info first lxc info second lxc info third lxc info ubuntu-vm
We don’t need all of these instances for the remainder of the tutorial, so let’s clean some of them up:
Stop the second container:
lxc stop second
Delete the second container:
lxc delete second
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:
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
Check the current configuration and compare it to the configuration of the first (unlimited) container:
lxc config show limited lxc config show first
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.
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.
You can also update the configuration while your container is running:
Configure a memory limit for your container:
lxc config set limited limits.memory=128MiB
Check that the configuration has been applied:
lxc config show limited
Check the amount of memory that is available to the container:
lxc exec limited -- free -m
Note that the number has changed.
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 (quota) of the root disk device for a VM:
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/shm
tmpfs 193M 604K 193M 1% /run
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 50M 14M 37M 27% /run/lxd_agent
/dev/sda15 105M 6.1M 99M 6% /boot/efi
Override the size of the root disk device:
lxc config device override ubuntu-vm root size=30GiB
Restart the VM:
lxc restart ubuntu-vm
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/shm
tmpfs 193M 588K 193M 1% /run
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 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:
Run the
bash
command in your container:lxc exec first -- bash
Enter some commands, for example, display information about the operating system:
cat /etc/*release
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:
Pull a file from the container:
lxc file pull first/etc/hosts .
Add an entry to the file:
echo "1.2.3.4 my-example" >> hosts
Push the file back to the container:
lxc file push hosts first/etc/hosts
Use the same mechanism to access log files:
lxc file pull first/var/log/syslog - | less
Note
Press
q
to exit theless
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.
Create a snapshot called “clean”:
lxc snapshot first clean
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.Break the container:
lxc exec first -- rm /usr/bin/bash
Confirm the breakage:
lxc exec first -- bash
Note
You do not get a shell, because you deleted the
bash
command.Restore the container to the state of the snapshot:
lxc restore first clean
Confirm that everything is back to normal:
lxc exec first -- bash exit
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!