How to Install and Configure doctl
How to Install and Configure doctl on Ubuntu
If you’re a developer working with DigitalOcean, you’ve likely heard of doctl, a command-line interface (CLI) that allows you to manage your DigitalOcean infrastructure easily. With doctl, you can create and manage Droplets, Kubernetes clusters, and other DigitalOcean services straight from the terminal. In this comprehensive guide, I’ll show you how to install and configure doctl on Ubuntu. Let’s dive in!
What is doctl?
doctl is the official DigitalOcean command-line client. It simplifies managing resources on your DigitalOcean account, including creating and controlling virtual machines (Droplets), managing databases, DNS, and even networking tasks. It’s especially useful if you prefer working with a terminal or want to integrate DigitalOcean management into scripts or automated workflows.
Prerequisites
Before you begin, make sure you have:
- A DigitalOcean account.
- An Ubuntu system (version 18.04 or later recommended).
- Access to the terminal with sudo privileges.
Step 1: Update Your System
The first step before installing any new software is to update your system packages to their latest versions. Open a terminal and run the following command:
sudo apt update && sudo apt upgrade -y
This ensures that your system is up-to-date and minimizes potential conflicts during the installation.
Step 2: Install doctl
- Method 1: Using the Official DigitalOcean APT Repository
Add the doctl APT Repository:
DigitalOcean provides an official APT repository to make installation straightforward. Add the repository by executing:
curl -sL https://repos.insights.digitalocean.com/DO_key.asc | sudo apt-key add –
echo “deb https://repos.insights.digitalocean.com/apt/do-agent/ stable main” | sudo tee /etc/apt/sources.list.d/digitalocean-agent.list
Update the APT Cache and Install doctl:
sudo apt update
sudo apt install doctl -y
- Method 2: Download the Binary Directly
You can also download the doctl binary directly from DigitalOcean’s GitHub releases page.
Download the Latest Release:
curl -s https://api.github.com/repos/digitalocean/doctl/releases/latest | grep browser_download_url | grep linux-amd64 | cut -d ‘”‘ -f 4 | wget -i –
Install the Binary:
tar xf doctl-*-linux-amd64.tar.gz
sudo mv doctl /usr/local/bin/
Check Installation:
To verify the installation, type:
doctl version
You should see the version details of doctl.
- Step 3: Authenticate doctl with Your DigitalOcean Account
Now that doctl is installed, you’ll need to authenticate it to interact with your DigitalOcean account.
Generate a Personal Access Token:
- Log in to your DigitalOcean account.
- Go to API > Tokens/Keys.
- Click on Generate New Token.
- Provide a name and set the desired access level.
- Click Generate Token and copy it.
- Authenticate doctl:
Use the following command to authenticate doctl:
doctl auth init
You’ll be prompted to enter your personal access token. Paste the token you copied earlier and press Enter.
Step 4: Test doctl Commands
To ensure everything is set up correctly, try a few doctl commands:
List Available Droplets:
doctl compute droplet list
List Account Information:
doctl account get
List Regions:
doctl compute region list
If you see outputs for these commands, congratulations! You’ve successfully installed and authenticated doctl.
Step 5: Basic Usage Examples
Now that doctl is installed and authenticated, let’s see how to use it to perform some common tasks.
Create a Droplet
To create a new Droplet:
doctl compute droplet create example-droplet –size s-1vcpu-1gb –image ubuntu-20-04-x64 –region nyc1 –ssh-keys
Replace with your SSH key ID. You can list your existing SSH keys with:
doctl compute ssh-key list
Manage Kubernetes Clusters
List All Kubernetes Clusters:
doctl kubernetes cluster list
Create a New Kubernetes Cluster:
doctl kubernetes cluster create example-cluster –region nyc1 –version 1.21.5-do.0
Control Databases
List All Databases:
doctl databases list
Create a PostgreSQL Database:
doctl databases create example-db –engine pg –version 13 –size db-s-1vcpu-1gb –region nyc1
Step 6: Automating with Scripts
You can use doctl in scripts to automate tasks. For example, to back up all your Droplets, you could write a simple bash script:
#!/bin/bash
for droplet in $(doctl compute droplet list –no-header –format ID); do
doctl compute droplet-action snapshot $droplet –snapshot-name “backup-$(date +%Y%m%d)”
done
Save this as backup.sh, give it executable permissions (chmod +x backup.sh), and run it. This script will take snapshots of all your Droplets with a timestamp.
Conclusion
In this guide, we’ve covered everything you need to install and configure doctl on an Ubuntu system. You’ve learned how to authenticate with your DigitalOcean account, run basic doctl commands, and even integrate it into scripts. With doctl, managing your DigitalOcean infrastructure becomes more efficient and streamlined.
Thank you for visiting our website! If you’re looking to improve your skills with Linux systems and would like to read our article on “How to Install AWS CLI on Linux,” you can access it through the link below. Happy learning! 🙂