Keeping your dependencies updated automatically with Dependabot
Tags: python, productivity, github, tutorial
Published: 2021-05-19 12:28:39

In this tutorial, you will learn how to use the GitHub dependency bot to make sure your packages stay up to date without breaking your code.

I will be showing you how to add versions to Python dependencies and how to set up Dependabot to run on your GitHub repository to make sure you always have the latest version in your requirements.txt. Dependabot will send Pull Requests to your repo whenever a package is out of date so you can easily check the changelog, test the new version, and update your requirements file with a single click.

The Dependabot configuration steps can be applied to dependencies of many types and in many languages including `github-actions`, `docker`, `pip` (Python), `npm` (JavaScript), `composer` (PHP), and [many others](https://docs.github.com/en/code-security/supply-chain-security/configuration-options-for-dependency-updates#package-ecosystem).

This article was originally a video tutorial, which you can check out here:

{% youtube 22XrqdIe8oQ %}

## Make sure your packages have version numbers

When using a `requirements.txt` in a Python project, version numbers are optional. If they are not provided, running the command to install the packages will always get the latest version of the package.

> So why should we add version numbers?

It is very risky to install dependencies without specifying version numbers. Often times packages will receive updates that break or alter existing functionality and require the dependent to update their code to continue using the package. In the case of a new major release, your code may all of a sudden break just because you wrote it for a different version of a package you are using.

> How do you add version numbers?

In your `requirements.txt`, you can specify version numbers by appending `==` followed by the version to the package name. To find out what version of each package your are using, you can run `pip freeze` in your terminal.

![pip freeze](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n57rr8iihgar0uaixjut.gif) 

This is an example `requirements.txt` with version numbers included:

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tmvq6fv0yguea70s83op.png)
 
## Automatic updates with Dependabot

### Step 1 - Click on Insights

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ohlse9tl3jujtmhru7cq.png)

### Step 2 - Click "Dependency graph", then "Dependabot"

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/73uy9ju56boi9g195c94.png)

### Step 3 - Enable Dependabot and create a config file

Make sure to insert the name of your package ecosystem on the line that is highlighted below. See [this link](https://docs.github.com/en/code-security/supply-chain-security/configuration-options-for-dependency-updates#package-ecosystem) for the list of possible options.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mdgjmrnrir1dyt8kv949.png)

### Step 4 - Push the configuration file to your main branch and wait for pull requests

Within a few minutes, you should start receiving PRs (possibly longer if you already have everything up to date 😄)

## Reviewing Dependabot PRs

Here's what a Dependabot PR looks like:

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xg2e2f5essero76172xq.png)

It will give you details about the changes to the package and allow you run tests if that is applicable to your repo.

Once your code is tested and seems to be working with the new version, you can update your requirements by simply clicking "Merge Pull Request".

Whenever a change is made to the requirements, Dependabot will automatically rebase all of it's open PRs to ensure there are never any merge conflicts.

## Conclusion

I hope you found this tutorial useful.

Check out [the full video](https://www.youtube.com/watch?v=22XrqdIe8oQ) for further explanations and be sure to like and subscribe!

\- Jonah Lawrence

Twitter: [@DenverCoder1](https://twitter.com/DenverCoder1)

YouTube: [Jonah Lawrence - Dev Pro Tips](https://youtube.com/c/DevProTips)

Discord server: https://discord.gg/fPrdqh3Zfu