DEV Community

Deep Panchal
Deep Panchal

Posted on • Updated on

Setup asdf & direnv

⚙️ Environment setup

ASDF Version Manager

asdf is a tool version manager. All tool version definitions are contained within one file (.tool-versions) which you can check in to your project's Git repository to share with your team, ensuring everyone is using the exact same versions of tools.

Reference: https://asdf-vm.com/guide/introduction.html

Install asdf

  • Make sure you have curl, git, and unzip installed.
  • Download asdf with

    git clone https://github.com/asdf-vm/asdf.git ~/.asdf --branch v0.10.0
    
  • If you are using bash, add the following to your ~/.bashrc file:

    . $HOME/.asdf/asdf.sh
    
    # configure completions
    . $HOME/.asdf/completions/asdf.bash
    
  • If you are using zsh, add the following to your ~/.zshrc file:

    . $HOME/.asdf/asdf.sh
    
    # if using oh-my-zsh, add `asdf` to your plugins list
    # for example: plugins=(... asdf ...)
    
    # if you are not using oh-my-zsh, add the following to your `.zshrc` file:
    
    # append completions to fpath
    fpath=(${ASDF_DIR}/completions $fpath)
    
    # initialise completions with ZSH's compinit
    autoload -Uz compinit && compinit
    

Install plugins

After installing asdf, add all the plugins found in the .tool-versions file. This depends on the tools defined in your .tool-versions file depending on the repository.

# Add all the plugins to asdf.
cat .tool-versions | awk '{print $1}' | xargs -I _ asdf plugin add _

# Install all tools according to .tool-versions.
asdf install
Enter fullscreen mode Exit fullscreen mode

Example

If your project's .tool-versions file contains the following:

ruby 2.6.3
python 3.10.4
nodejs 16.0.0
Enter fullscreen mode Exit fullscreen mode

Then you can install all the tools with their specified versions by running asdf install.

Global tool versions

Optionally, you can add the following .tool-versions file to your home directory:

direnv 2.31.0
nodejs 16.13.2
pnpm 7.1.5
java adoptopenjdk-8.0.272+10
minikube 1.25.1
helm 3.6.3
helmfile 0.139.9
kubectl 1.23.3
kubespy 0.6.0
kubectx 0.9.4
jq 1.6
golang 1.15.15
poetry 1.1.13
Enter fullscreen mode Exit fullscreen mode

!!! Note
Make sure you stay up to date with the latest or stable versions of tools.

Refer to official asdf documentation for more information.

Direnv

direnv is an extension for your shell. It augments existing shells with a new feature that can load and unload environment variables depending on the current directory.

Setup any project with direnv by creating a .envrc file that looks like the following:

#!/usr/bin/env bash
export CR_PAT=<YOUR_GITHUB_PAT_WITH_REPO_READ_AND_WRITE>
export NODE_AUTH_TOKEN=$CR_PAT
export DEBUG="*"

cat <<EOF >.env
VITE_BASE_URL=${VITE_BASE_URL}
NODE_AUTH_TOKEN=${CR_PAT}
EOF
Enter fullscreen mode Exit fullscreen mode

Hook asdf with direnv

Setup

  • Install asdf-plugin for direnv using the following:

    asdf plugin-add direnv
    
  • Setup shell

    # change --shell flag based on your current shell (bash, zsh, fish, etc.)
    asdf direnv setup --shell zsh --version latest
    
  • Once direnv is hooked up, you can run the following command on the root directory of your project to update your environment given that you have a .envrc file in your project root.

    asdf direnv local
    

Creating temporary environments

You can create a temporary environment without editing .envrc or .tool-versions

# Enter a new shell having python and node
asdf direnv shell python 3.8.10 nodejs 14.18.2

# Just execute a npx command under some node version
asdf direnv shell nodejs 14.18.2 -- npx create-react-app
Enter fullscreen mode Exit fullscreen mode

References:

Top comments (0)