It’s frowned upon, because it’s a bad practice for real work that need reproducibility and isolation between things.
That said, you can do it, here is how:
— Taken from a reddit comment of mine about the same:
In Clojure those are called libraries or lib(s) for short. You don’t install them; you simply declare a dependency on them. This is normally done by editing the dependency configuration for your project.
If you’re using lein
for your Clojure development, then you want to edit the project.clj
file, which is where libraries you depend on are configured.
If you’re using Clojure CLI
clj
or clojure
command for your Clojure development, then you want to edit the deps.edn
file, which is where libraries you depend on are configured.
Say you’re using lein
, and you want to use conch from inside your project, modify the project.clj
file like so:
(defproject org.example/sample "1.0.0-SNAPSHOT"
:dependencies [[org.clojure/clojure "1.11.1"]
[me.raynes/conch "0.8.0"]] ;;<-- We added the library here
...)
If you’re using Clojure CLI, and you want to use conch from inside your project, modify the deps.edn
file like so:
{:paths ["src" "target/classes"]
:deps {org.clojure/clojure {:mvn/version "1.11.1"}
me.raynes/conch {:mvn/version "0.8.0"}}
...}
If you don’t have a project, say you’re just working in a script without any directory structure and without a project-level project.clj
or deps.edn
file, you need to instead modify the global profiles.clj
for lein or deps.edn
for Clojure CLI file in the same way I showed above. Those define the default global library dependencies and are the closest you can get to a global install.
For lein
you find the file at: ~/.lein/profiles.clj
and this time it looks a bit different:
{:user {:dependencies [[me.raynes/conch "0.8.0"]]}}
You add the dependency in a similar way, but it goes inside the :user
profile under :dependencies
.
For Clojure CLI, you find the file at: ~/.clojure/deps.edn
and for this one its very similar to what you did before:
{:deps {me.raynes/conch {:mvn/version "0.8.0"}}}
Just add it to the :deps
key.
Now if you start a REPL or run a script anywhere those libraries will always be available.
Note that defining global dependencies like that is considered a bad practice in general, but when you’re a total beginner, it can be easier until you learn more and understand how to manage projects and local dependencies.
With deps.edn, you can also create global aliases, those would already be a bit cleaner, in that you can tack them on to bring in some dependencies ad-hoc, but it might be too advanced for now, but once you learn about aliases in tools.deps, just know you can define some global ones as well in your ~/.clojure/deps.edn
config file, and they can then be used from anywhere as well.