<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Deep Panchal</title>
    <description>The latest articles on DEV Community by Deep Panchal (@deepanchal).</description>
    <link>https://dev.to/deepanchal</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F427211%2Fa6a662cf-7ce5-413a-b1b9-c09e1cf8cddd.png</url>
      <title>DEV Community: Deep Panchal</title>
      <link>https://dev.to/deepanchal</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/deepanchal"/>
    <language>en</language>
    <item>
      <title>Setup asdf &amp; direnv</title>
      <dc:creator>Deep Panchal</dc:creator>
      <pubDate>Tue, 14 Jun 2022 22:23:16 +0000</pubDate>
      <link>https://dev.to/deepanchal/setup-asdf-direnv-5afo</link>
      <guid>https://dev.to/deepanchal/setup-asdf-direnv-5afo</guid>
      <description>&lt;h1&gt;
  
  
  ⚙️ Environment setup
&lt;/h1&gt;

&lt;h2&gt;
  
  
  ASDF Version Manager
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;asdf&lt;/code&gt; is a tool version manager. All tool version definitions are contained within one file (&lt;code&gt;.tool-versions&lt;/code&gt;) 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.&lt;/p&gt;

&lt;p&gt;Reference: &lt;a href="https://asdf-vm.com/guide/introduction.html"&gt;https://asdf-vm.com/guide/introduction.html&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Install asdf
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Make sure you have &lt;code&gt;curl&lt;/code&gt;, &lt;code&gt;git&lt;/code&gt;, and &lt;code&gt;unzip&lt;/code&gt; installed.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Download asdf with&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git clone https://github.com/asdf-vm/asdf.git ~/.asdf &lt;span class="nt"&gt;--branch&lt;/span&gt; v0.10.0
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;If you are using bash, add the following to your &lt;code&gt;~/.bashrc&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;.&lt;/span&gt; &lt;span class="nv"&gt;$HOME&lt;/span&gt;/.asdf/asdf.sh

&lt;span class="c"&gt;# configure completions&lt;/span&gt;
&lt;span class="nb"&gt;.&lt;/span&gt; &lt;span class="nv"&gt;$HOME&lt;/span&gt;/.asdf/completions/asdf.bash
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;If you are using zsh, add the following to your &lt;code&gt;~/.zshrc&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;.&lt;/span&gt; &lt;span class="nv"&gt;$HOME&lt;/span&gt;/.asdf/asdf.sh

&lt;span class="c"&gt;# if using oh-my-zsh, add `asdf` to your plugins list&lt;/span&gt;
&lt;span class="c"&gt;# for example: plugins=(... asdf ...)&lt;/span&gt;

&lt;span class="c"&gt;# if you are not using oh-my-zsh, add the following to your `.zshrc` file:&lt;/span&gt;

&lt;span class="c"&gt;# append completions to fpath&lt;/span&gt;
&lt;span class="nv"&gt;fpath&lt;/span&gt;&lt;span class="o"&gt;=(&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;ASDF_DIR&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;/completions &lt;span class="nv"&gt;$fpath&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;

&lt;span class="c"&gt;# initialise completions with ZSH's compinit&lt;/span&gt;
autoload &lt;span class="nt"&gt;-Uz&lt;/span&gt; compinit &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; compinit
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Install plugins
&lt;/h3&gt;

&lt;p&gt;After installing &lt;code&gt;asdf&lt;/code&gt;, add all the plugins found in the &lt;code&gt;.tool-versions&lt;/code&gt; file. This depends on the tools defined in your &lt;code&gt;.tool-versions&lt;/code&gt; file depending on the repository.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Add all the plugins to asdf.&lt;/span&gt;
&lt;span class="nb"&gt;cat&lt;/span&gt; .tool-versions | &lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="s1"&gt;'{print $1}'&lt;/span&gt; | xargs &lt;span class="nt"&gt;-I&lt;/span&gt; _ asdf plugin add _

&lt;span class="c"&gt;# Install all tools according to .tool-versions.&lt;/span&gt;
asdf &lt;span class="nb"&gt;install&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Example
&lt;/h4&gt;

&lt;p&gt;If your project's &lt;code&gt;.tool-versions&lt;/code&gt; file contains the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight markdown"&gt;&lt;code&gt;ruby 2.6.3
python 3.10.4
nodejs 16.0.0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then you can install all the tools with their specified versions by running &lt;code&gt;asdf install&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Global tool versions
&lt;/h3&gt;

&lt;p&gt;Optionally, you can add the following &lt;code&gt;.tool-versions&lt;/code&gt; file to your home directory:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight markdown"&gt;&lt;code&gt;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
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;!!! Note&lt;br&gt;
    Make sure you stay up to date with the latest or stable versions of tools.&lt;/p&gt;

&lt;p&gt;Refer to &lt;a href="https://asdf-vm.com/guide/getting-started.html#_1-install-dependencies"&gt;official asdf documentation&lt;/a&gt; for more information.&lt;/p&gt;
&lt;h2&gt;
  
  
  Direnv
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://direnv.net/"&gt;&lt;code&gt;direnv&lt;/code&gt;&lt;/a&gt; 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.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Install &lt;a href="https://direnv.net/docs/installation.html"&gt;&lt;code&gt;direnv&lt;/code&gt;&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://direnv.net/docs/hook.html"&gt;Hook&lt;/a&gt; direnv with your shell&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Setup any project with &lt;code&gt;direnv&lt;/code&gt; by creating a &lt;code&gt;.envrc&lt;/code&gt; file that looks like the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/usr/bin/env bash&lt;/span&gt;
&lt;span class="nb"&gt;export &lt;/span&gt;&lt;span class="nv"&gt;CR_PAT&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&amp;lt;YOUR_GITHUB_PAT_WITH_REPO_READ_AND_WRITE&amp;gt;
&lt;span class="nb"&gt;export &lt;/span&gt;&lt;span class="nv"&gt;NODE_AUTH_TOKEN&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;$CR_PAT&lt;/span&gt;
&lt;span class="nb"&gt;export &lt;/span&gt;&lt;span class="nv"&gt;DEBUG&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"*"&lt;/span&gt;

&lt;span class="nb"&gt;cat&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;EOF&lt;/span&gt;&lt;span class="sh"&gt; &amp;gt;.env
VITE_BASE_URL=&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;VITE_BASE_URL&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;
NODE_AUTH_TOKEN=&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;CR_PAT&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;
&lt;/span&gt;&lt;span class="no"&gt;EOF
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Hook asdf with direnv
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Setup
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Install &lt;a href="https://github.com/asdf-community/asdf-direnv"&gt;asdf-plugin&lt;/a&gt; for direnv using the following:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;asdf plugin-add direnv
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Setup shell&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# change --shell flag based on your current shell (bash, zsh, fish, etc.)&lt;/span&gt;
asdf direnv setup &lt;span class="nt"&gt;--shell&lt;/span&gt; zsh &lt;span class="nt"&gt;--version&lt;/span&gt; latest
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;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 &lt;code&gt;.envrc&lt;/code&gt; file in your project root.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;asdf direnv &lt;span class="nb"&gt;local&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Creating temporary environments
&lt;/h3&gt;

&lt;p&gt;You can create a temporary environment without editing &lt;code&gt;.envrc&lt;/code&gt; or &lt;code&gt;.tool-versions&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Enter a new shell having python and node&lt;/span&gt;
asdf direnv shell python 3.8.10 nodejs 14.18.2

&lt;span class="c"&gt;# Just execute a npx command under some node version&lt;/span&gt;
asdf direnv shell nodejs 14.18.2 &lt;span class="nt"&gt;--&lt;/span&gt; npx create-react-app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;References:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://asdf-vm.com/guide/introduction.html#direnv"&gt;https://asdf-vm.com/guide/introduction.html#direnv&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/asdf-community/asdf-direnv#setup"&gt;https://github.com/asdf-community/asdf-direnv#setup&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/asdf-community/asdf-direnv#per-project-environments"&gt;https://github.com/asdf-community/asdf-direnv#per-project-environments&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>asdf</category>
      <category>direnv</category>
      <category>webdev</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
