18

I am learning Clojure and coming from a Ruby background.

I am looking for something analogous to gem install <library>. The various incantations of lein install do not seem to fit this bill.

Is there a way to simply install a library locally so that it can be referenced in the REPL without the need to create a project?

5 Answers 5

6

Seems like, you want to install a library with lein. Here is the plugin, install it and use like

 lein localrepo install <filename> <[groupId/]artifactId> <version>
6

If your aim is merely to load libraries in the REPL consider using alembic. It loads dynamically classpaths, resolve dependencies and automatically pulls libraries from the repositories.

Here is a use case:

(require 'alembic.still)
(alembic.still/distill '[enlive "1.1.1"])

It simply requires you to add the following entry to your .lein/project.clj:

{:dev {:dependencies [[alembic "0.1.1"]]}}

See this answer.

4

Java and thus clojure do not generally have the the idea of globally installed libraries. You should always be creating a classpath with the minimal set of dependencies. You need somehow to specify and manage this classpath and the easiest way to do this is with leiningen, which requires a project.

leiningen automates the process of retrieving the remote libraries and placing them in your local repository which is somewhat analogous to gem install, but these libraries do not become automatically available to a REPL.

The easiest way to have a set of libraries always available is to have a 'scratch' project which you use for REPL experiments before starting a new project. It's not too much of an overhead.

4

In lein 2 you can update profiles.clj with package you want to install:

~\user\.lein\profiles.clj

With the first run of any project with lein, the local repo will be updated with what was incereased in profiles.clj.

Sometimes I just run lein deps without being in a project folder, this will update the local repo for you.

This way you can add any library to your project.clj or call it from repl and it will be extracted from local repo.

1

If you don’t have a project, you add your dependencies in your global lein user profile instead located at ~/.lein/profiles.clj.

The doc isn’t great for lein to be honest. So this part is confusing. But you edit that file as such:

{:user {:plugins [[lein-pprint "1.1.1"]]
 :dependencies [[slamhound "1.3.1"]]}} 

In the :plugins vector you add whatever global lein plugin you want to have. And in the :dependencies vector you add whatever library you want available globally.

Then anywhere you start a lein repl you’d have those dependencies available to you. And everywhere you run lein you’ll have the additional plugin features available to you.

If you use tools.deps instead of lein, aka, the clj command instead of the lein command. Then it is a bit different. You instead want to modify your ~/.clojure/deps.edn file. Where you’d add dependencies there instead:

{:deps {clj-time {:mvn/version "0.14.2"}}}

So if you put the above in your user deps.edn whenever you run clj command the clj-time library will be available to you.

Not the answer you're looking for? Browse other questions tagged or ask your own question.