<?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: Ed DeVries</title>
    <description>The latest articles on DEV Community by Ed DeVries (@ehdevries).</description>
    <link>https://dev.to/ehdevries</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%2F284044%2F7a611cd6-5205-44dd-9d94-cfb70ca072eb.jpeg</url>
      <title>DEV Community: Ed DeVries</title>
      <link>https://dev.to/ehdevries</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ehdevries"/>
    <language>en</language>
    <item>
      <title>F# Scripting on Linux in 2020</title>
      <dc:creator>Ed DeVries</dc:creator>
      <pubDate>Mon, 06 Jan 2020 16:20:40 +0000</pubDate>
      <link>https://dev.to/ehdevries/f-scripting-on-linux-in-2020-385o</link>
      <guid>https://dev.to/ehdevries/f-scripting-on-linux-in-2020-385o</guid>
      <description>&lt;p&gt;I recently had occasion to write some F# scripts on Linux. I ran into a few hiccups getting my environment set up and had trouble finding documented solutions, so I thought I'd share what worked for me.&lt;/p&gt;

&lt;p&gt;My machine is running &lt;a href="https://zorinos.com/"&gt;Zorin OS 15&lt;/a&gt;, which is based on &lt;a href="https://ubuntu.com/"&gt;Ubuntu 18.04 LTS&lt;/a&gt;. As long as you're running a distribution that supports &lt;a href="https://snapcraft.io/"&gt;snaps&lt;/a&gt;, your experience will likely be similar to mine.&lt;/p&gt;

&lt;h2&gt;
  
  
  tl;dr
&lt;/h2&gt;

&lt;p&gt;Here's one way to get up and running with F# scripting on Linux:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Install the .NET Core SDK snap with &lt;code&gt;sudo snap install dotnet-sdk --classic&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Add &lt;code&gt;/snap/dotnet-sdk/current&lt;/code&gt; to &lt;code&gt;$PATH&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Install the VS Code snap with &lt;code&gt;sudo snap install code --classic&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Install the Ionide extension with &lt;code&gt;code --install-extension ionide.ionide-fsharp&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Add the following to VS Code's &lt;code&gt;settings.json&lt;/code&gt; file: &lt;code&gt;"FSharp.dotNetRoot": "/snap/dotnet-sdk/current"&lt;/code&gt; and &lt;code&gt;"FSharp.useSdkScripts": true&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Use &lt;a href="https://fsprojects.github.io/Paket/"&gt;Paket&lt;/a&gt; to manage NuGet packages&lt;/li&gt;
&lt;li&gt;Change the &lt;code&gt;paket.dependencies&lt;/code&gt; file's &lt;code&gt;storage&lt;/code&gt; setting from &lt;code&gt;none&lt;/code&gt; to &lt;code&gt;packages&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That's the quick and dirty version. Read on for the why and the how, as well as some nifty bonus tips. 💡&lt;/p&gt;

&lt;h2&gt;
  
  
  Installing .NET Core
&lt;/h2&gt;

&lt;p&gt;F# runs on .NET, so my first step is to install the &lt;a href="https://devblogs.microsoft.com/dotnet/announcing-net-core-3-1/"&gt;recently released .NET Core 3.1 LTS&lt;/a&gt;. The &lt;a href="https://dotnet.microsoft.com/download"&gt;.NET Core downloads page&lt;/a&gt; links to some docs that recommend installation using a package manager. This is a fine solution, as is &lt;a href="https://hub.docker.com/_/microsoft-dotnet-core/"&gt;Docker&lt;/a&gt;, but I prefer to use snap packages whenever I can. It turns out there is an &lt;a href="https://snapcraft.io/dotnet-sdk"&gt;official snap for the .NET Core SDK&lt;/a&gt;. The following command installs it:&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="nb"&gt;sudo &lt;/span&gt;snap &lt;span class="nb"&gt;install &lt;/span&gt;dotnet-sdk &lt;span class="nt"&gt;--classic&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Cool! That seemed straightforward enough. Let's try out the dotnet CLI.&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="nv"&gt;$ &lt;/span&gt;dotnet &lt;span class="nt"&gt;--version&lt;/span&gt;
Command &lt;span class="s1"&gt;'dotnet'&lt;/span&gt; not found, but can be installed with:
&lt;span class="nb"&gt;sudo &lt;/span&gt;snap &lt;span class="nb"&gt;install &lt;/span&gt;dotnet-sdk
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hmmm. Pretty sure I did that already. Maybe I need to reboot or something?&lt;/p&gt;

&lt;p&gt;&lt;em&gt;reboots, tries again...&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Nope, no luck. I wonder if the dotnet CLI was installed to a location that isn't in my path. Come to think of it, what &lt;em&gt;is&lt;/em&gt; in my path?&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="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$PATH&lt;/span&gt;
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Okay, so &lt;code&gt;/snap/bin&lt;/code&gt; is already in my path, but apparently, the &lt;code&gt;dotnet&lt;/code&gt; command isn't there. I wonder what else is in the &lt;code&gt;/snap&lt;/code&gt; directory...&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="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;ls&lt;/span&gt; &lt;span class="nt"&gt;-1&lt;/span&gt; /snap
bin
code
core
core18
dotnet-sdk
node
README
slack
supertuxkart
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Aha! Aside from the fact that you now know what fun snaps I've installed, this is interesting information. Let's see what's in that &lt;code&gt;dotnet-sdk&lt;/code&gt; directory.&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="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;ls&lt;/span&gt; &lt;span class="nt"&gt;-1&lt;/span&gt; /snap/dotnet-sdk/
54
57
current

&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;ls&lt;/span&gt; &lt;span class="nt"&gt;-1&lt;/span&gt; /snap/dotnet-sdk/current
command-dotnet.wrapper
dotnet
dotnet-runtime
dotnet-sdk-3.0.100-linux-x64.tar.gz
etc
host
lib
LICENSE.txt
meta
packs
sdk
shared
snap
templates
ThirdPartyNotices.txt
usr
var

&lt;span class="nv"&gt;$ &lt;/span&gt;/snap/dotnet-sdk/current/dotnet &lt;span class="nt"&gt;--version&lt;/span&gt;
3.1.100
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Bingo! There's our dotnet CLI executable (or at least a symbolic link to it). Now I just need to modify my path so that my shell will be able to find it whenever I type &lt;code&gt;dotnet&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;I'm pretty sure the code in &lt;code&gt;~/.profile&lt;/code&gt; runs when I log into my machine, so that seems like as good a place as any to make sure the dotnet CLI location is added to my path. Here's a one-time script to modify &lt;code&gt;~/.profile&lt;/code&gt; appropriately:&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="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"
# set PATH so it includes dotnet core sdk snap and tools
if [ -d /snap/dotnet-sdk/current ]
then
  PATH=&lt;/span&gt;&lt;span class="se"&gt;\"\$&lt;/span&gt;&lt;span class="s2"&gt;PATH:/snap/dotnet-sdk/current&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt;
fi
if [ -d &lt;/span&gt;&lt;span class="se"&gt;\$&lt;/span&gt;&lt;span class="s2"&gt;HOME/.dotnet/tools ]
then
  PATH=&lt;/span&gt;&lt;span class="se"&gt;\"\$&lt;/span&gt;&lt;span class="s2"&gt;PATH:&lt;/span&gt;&lt;span class="se"&gt;\$&lt;/span&gt;&lt;span class="s2"&gt;HOME/.dotnet/tools&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt;
fi"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; ~/.profile
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now I can finally type &lt;code&gt;dotnet&lt;/code&gt; to invoke the dotnet CLI. We're in business!&lt;/p&gt;

&lt;p&gt;A quick aside: there is a simpler way to make the &lt;code&gt;dotnet&lt;/code&gt; command available without modifying the path. Snaps have the concept of an &lt;a href="https://forum.snapcraft.io/t/commands-and-aliases/3950"&gt;alias&lt;/a&gt;, which allows us to specify how we're going to invoke a snap on the command line. This is useful when the unique name of the snap (in this case, &lt;code&gt;dotnet-sdk&lt;/code&gt;) is different from how we expect to invoke it (in this case, &lt;code&gt;dotnet&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;For example, the NodeJS snap adds helpful aliases by default:&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="nv"&gt;$ &lt;/span&gt;snap aliases
Command       Alias    Notes
node.npm      npm      -
node.npx      npx      -
node.yarn     yarn     -
node.yarnpkg  yarnpkg  -
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At the time of writing, there is an &lt;a href="https://github.com/dotnet/cli/issues/11837"&gt;open issue in the backlog&lt;/a&gt; of the dotnet CLI repository to add a &lt;code&gt;dotnet&lt;/code&gt; snap alias on install. For now, we can add one manually with the following command:&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="nb"&gt;sudo &lt;/span&gt;snap &lt;span class="nb"&gt;alias &lt;/span&gt;dotnet-sdk.dotnet dotnet
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This allows us to type &lt;code&gt;dotnet&lt;/code&gt; to invoke the dotnet CLI, and it only took one command to get us there - no path wrangling needed. Nice!&lt;/p&gt;

&lt;p&gt;Now for the bad news. When I tried adding a &lt;code&gt;dotnet&lt;/code&gt; snap alias, everything worked great on the command line, but the F# tooling in VS Code failed. I wasn't able to make it work, so I went back to the slightly-less-elegant solution of modifying my path. If you are able to make the F# tooling work with the snap alias, please let me know!&lt;/p&gt;

&lt;p&gt;In any case, .NET Core is now installed and operational, so let's move on.&lt;/p&gt;

&lt;h2&gt;
  
  
  Configuring VS Code
&lt;/h2&gt;

&lt;p&gt;I'll use &lt;a href="https://code.visualstudio.com/"&gt;VS Code&lt;/a&gt; with the &lt;a href="https://marketplace.visualstudio.com/items?itemName=Ionide.Ionide-fsharp"&gt;Ionide extension&lt;/a&gt;. This seems to be the &lt;em&gt;de facto&lt;/em&gt; tool set for cross-platform F# development. I've used it on Windows and really enjoyed it.&lt;/p&gt;

&lt;p&gt;Let's install the &lt;a href="https://snapcraft.io/code"&gt;VS Code snap&lt;/a&gt;, which, fortunately, works out of the box:&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="nb"&gt;sudo &lt;/span&gt;snap &lt;span class="nb"&gt;install &lt;/span&gt;code &lt;span class="nt"&gt;--classic&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then we can install Ionide from the command line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;code &lt;span class="nt"&gt;--install-extension&lt;/span&gt; ionide.ionide-fsharp
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Voilà! An F# development environment.&lt;/p&gt;

&lt;p&gt;Mostly.&lt;/p&gt;

&lt;p&gt;Ionide is not without its quirks. Occasionally, when I open VS Code, a &lt;code&gt;.ionide&lt;/code&gt; directory is created in my workspace, regardless of whether the workspace contains any F#. At the time of writing, there is an &lt;a href="https://github.com/ionide/ionide-vscode-fsharp/issues/1041"&gt;open issue&lt;/a&gt; in the Ionide repository to address this behavior. For now, I'll work around it by disabling the Ionide extension globally, then enabling it manually for each F# workspace. This can be done from the "Extensions" pane in VS Code.&lt;/p&gt;

&lt;p&gt;Another Ionide quirk affects one of my favorite features: CodeLens type signatures. Ionide can help us understand our code by showing the type signature of each function. Consider the following. If, somewhere in the code, &lt;code&gt;add&lt;/code&gt; is called with two integer parameters, we would see this (the comments are visible in the editor, but not part of the source code):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight fsharp"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;add&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="c1"&gt;// int -&amp;gt; int -&amp;gt; int&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When I first installed Ionide and opened an F# script file, the CodeLens type signatures were noticeably absent. All I saw was the code itself:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight fsharp"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;add&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After a bit of digging, I came upon two Ionide settings that needed to be explicitly set. The &lt;code&gt;dotNetRoot&lt;/code&gt; setting fixes the CodeLens problem by telling Ionide where to find the dotnet CLI, and the &lt;code&gt;useSdkScripts&lt;/code&gt; setting instructs Ionide to use the .NET Core version of FSharp Interactive (&lt;code&gt;dotnet fsi&lt;/code&gt;). I'll add these to my VS Code &lt;code&gt;settings.json&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="nl"&gt;"FSharp.dotNetRoot"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"/snap/dotnet-sdk/current"&lt;/span&gt;&lt;span class="err"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="nl"&gt;"FSharp.useSdkScripts"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we've got a first-class F# development environment. Let's get scripting!&lt;/p&gt;

&lt;h2&gt;
  
  
  Managing dependencies
&lt;/h2&gt;

&lt;p&gt;From here on, we'll discuss F# scripting in general, not limited to Linux.&lt;/p&gt;

&lt;p&gt;Although F# is a compiled language suitable for large projects, it works just as well as a scripting language. An F# script has a &lt;code&gt;.fsx&lt;/code&gt; extension rather than a &lt;code&gt;.fs&lt;/code&gt; extension, and stands alone - it does not make use of .NET project files (i.e. &lt;code&gt;*.fsproj&lt;/code&gt;, &lt;code&gt;*.csproj&lt;/code&gt;). If we want to use a library in our script, we have to download it and reference its path explicitly.&lt;/p&gt;

&lt;p&gt;The dotnet CLI can manage NuGet packages, but it assumes that we're working with full-fledged .NET projects. What we really need is a repeatable way to download NuGet packages to the location of our choosing so that we can reference them in our script. Fortunately, the F# community has developed a dependency manager that solves this problem: &lt;a href="https://fsprojects.github.io/Paket/index.html"&gt;Paket&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;To see how Paket works, we'll follow the &lt;a href="https://fsprojects.github.io/Paket/get-started.html"&gt;"Get started"&lt;/a&gt; guide and work through an example. Let's say that we have a bunch of data in a JSON file and we're writing an F# script to process that data. To deserialize our JSON data, we'll use the &lt;a href="https://www.newtonsoft.com/json"&gt;Newtonsoft.Json&lt;/a&gt; library.&lt;/p&gt;

&lt;p&gt;Right now, all we have is a directory with our data and our script:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.
|--data.json
`--script.fsx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Paket is available as a dotnet CLI tool. We have the option to install it globally, but for this example, we'll scope it to our codebase so that it's an explicit dependency. The following command will create a &lt;code&gt;.config&lt;/code&gt; directory with a skeleton &lt;code&gt;dotnet-tools.json&lt;/code&gt; manifest file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;dotnet new tool-manifest
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here's our directory so far:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.
|--.config
|  `--dotnet-tools.json
|--data.json
`--script.fsx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The following command will install the latest stable version of Paket locally and record that version in the manifest:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;dotnet tool &lt;span class="nb"&gt;install &lt;/span&gt;paket
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we can use Paket inside our codebase with &lt;code&gt;dotnet paket&lt;/code&gt;. So far so good!&lt;/p&gt;

&lt;p&gt;Paket uses a &lt;code&gt;paket.dependencies&lt;/code&gt; file to specify the NuGet packages we want. We'll create this file with the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;dotnet paket init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The default &lt;code&gt;paket.dependencies&lt;/code&gt; file looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;source https://api.nuget.org/v3/index.json

storage: none
framework: netcore3.0, netstandard2.0, netstandard2.1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we want the latest stable version of a NuGet package, we add the word &lt;code&gt;nuget&lt;/code&gt; followed by the name of the package - no version necessary:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;source https://api.nuget.org/v3/index.json

storage: none
framework: netcore3.0, netstandard2.0, netstandard2.1

nuget Newtonsoft.Json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we're finally ready to download the Newtonsoft.Json NuGet package. &lt;a href="https://fsprojects.github.io/Paket/learn-how-to-use-paket.html"&gt;"Learn how to use Paket"&lt;/a&gt; indicates we can do so with the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;dotnet paket &lt;span class="nb"&gt;install&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that this command creates a &lt;code&gt;paket.lock&lt;/code&gt; file, and it &lt;em&gt;does&lt;/em&gt; include a version number for each package, allowing us to share our code and restore exactly the same dependencies on another machine:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;STORAGE: NONE
RESTRICTION: || (== netcoreapp3.0) (== netstandard2.0) (== netstandard2.1)
NUGET
  remote: https://api.nuget.org/v3/index.json
    Newtonsoft.Json (12.0.3)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here's what our directory looks like now:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.
|--.config
|  `--dotnet-tools.json
|--paket-files
|  `--paket.restore.cached
|--data.json
|--paket.dependencies
|--paket.lock
`--script.fsx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hey, where are the NuGet packages?&lt;/p&gt;

&lt;p&gt;Let's take another look at &lt;code&gt;paket.dependencies&lt;/code&gt;. The default setting &lt;code&gt;storage: none&lt;/code&gt; instructs Paket to download NuGet packages to a global cache. This is efficient for full-fledged .NET projects, but it isn't helpful for scripting. We need to know exactly where to find &lt;code&gt;Newtonsoft.Json.dll&lt;/code&gt; relative to our script.&lt;/p&gt;

&lt;p&gt;According to &lt;a href="https://fsprojects.github.io/Paket/dependencies-file.html#Disable-packages-folder"&gt;the Paket documentation&lt;/a&gt;, we can instruct Paket to use a local &lt;code&gt;packages&lt;/code&gt; directory instead of a global cache by changing the &lt;code&gt;storage&lt;/code&gt; setting from &lt;code&gt;none&lt;/code&gt; to &lt;code&gt;packages&lt;/code&gt;. Here's our &lt;code&gt;paket.dependencies&lt;/code&gt; file after making the change:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;source https://api.nuget.org/v3/index.json

storage: packages
framework: netcore3.0, netstandard2.0, netstandard2.1

nuget Newtonsoft.Json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's try one more time:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;dotnet paket &lt;span class="nb"&gt;install&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Success! We have a &lt;code&gt;packages&lt;/code&gt; directory containing the Newtonsoft.Json NuGet package:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.
|--.config
|  `--dotnet-tools.json
|--packages
|  `--Newtonsoft.Json
|     `--[lots of stuff in here]
|--paket-files
|  `--paket.restore.cached
|--data.json
|--paket.dependencies
|--paket.lock
`--script.fsx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To reference it, we can add some code like this at the top of our script:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight fsharp"&gt;&lt;code&gt;&lt;span class="p"&gt;#&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="s2"&gt;"packages/Newtonsoft.Json/lib/netstandard2.0/Newtonsoft.Json.dll"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And away we go! With all of that setup behind us, we can focus on our code, bringing the power and elegance of F# to our scripting tasks.&lt;/p&gt;

&lt;p&gt;But before we close, there are a few things that can make our lives a bit easier.&lt;/p&gt;

&lt;h2&gt;
  
  
  Bonus tip: restoring dependencies
&lt;/h2&gt;

&lt;p&gt;The example above took the perspective of setting up Paket for the first time, but what should we do when we've committed our code to source control and cloned it on another machine?&lt;/p&gt;

&lt;p&gt;Fortunately, all of our dependencies - including Paket itself - are explicitly specified in the configuration files we created: &lt;code&gt;dotnet-tools.json&lt;/code&gt;, &lt;code&gt;paket.dependencies&lt;/code&gt;, and &lt;code&gt;paket.lock&lt;/code&gt;. We can use the dotnet CLI to restore Paket locally with the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;dotnet tool restore
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then we can use Paket to restore NuGet packages:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;dotnet paket restore
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These steps can be combined into a single &lt;code&gt;restore.sh&lt;/code&gt; shell script that we execute any time we pull down a new version of our F# script:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;dotnet tool restore
dotnet paket restore
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Bonus tip: ignoring generated files
&lt;/h2&gt;

&lt;p&gt;As we worked through the example above, we created quite a few files dedicated to dependency management. Some of these should be committed to source control along with our code, such as &lt;code&gt;dotnet-tools.json&lt;/code&gt;, &lt;code&gt;paket.dependencies&lt;/code&gt;, and &lt;code&gt;paket.lock&lt;/code&gt;. Some will be generated when we install or restore dependencies, and can thus be ignored by source control.&lt;/p&gt;

&lt;p&gt;If we add the following lines to our &lt;code&gt;.gitignore&lt;/code&gt; file, we can safely commit everything else:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.ionide/
.paket/
paket-files/
packages/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Bonus tip: setting the current directory
&lt;/h2&gt;

&lt;p&gt;Here's a little tip I picked up from the excellent &lt;a href="https://fsharpforfunandprofit.com/"&gt;&lt;em&gt;F# for Fun and Profit&lt;/em&gt;&lt;/a&gt; by Scott Wlaschin. His series on low-risk ways to use F# at work has some &lt;a href="https://fsharpforfunandprofit.com/posts/low-risk-ways-to-use-fsharp-at-work/#2-use-f-to-test-your-own-code-interactively"&gt;helpful example scripts&lt;/a&gt; that use this technique.&lt;/p&gt;

&lt;p&gt;When I'm writing a script, I often find that I need to read the contents of a file on disk, such as the &lt;code&gt;data.json&lt;/code&gt; file in our example above. I can put the input file in the same directory as the script, but sometimes I &lt;em&gt;invoke&lt;/em&gt; the script from a completely different directory. This makes coding the path to the input file a challenge.&lt;/p&gt;

&lt;p&gt;If I can control the current working directory at runtime, I can code a reliable path to the input file. F# has a built-in constant that can help us to accomplish this. The following code sets the current working directory to the F# script's directory:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight fsharp"&gt;&lt;code&gt;&lt;span class="nn"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nn"&gt;IO&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nn"&gt;Directory&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;SetCurrentDirectory&lt;/span&gt;&lt;span class="o"&gt;(__&lt;/span&gt;&lt;span class="nc"&gt;SOURCE_DIRECTORY__&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Bonus tip: you're awesome
&lt;/h2&gt;

&lt;p&gt;I hope you found this post helpful. Whether you develop on Linux, Mac, or Windows, have fun with F#!&lt;/p&gt;

</description>
      <category>fsharp</category>
      <category>dotnet</category>
      <category>linux</category>
    </item>
  </channel>
</rss>
