4

I try to install "System.Random" by cabal through Powershell & Git Bash.

getting this result.

PS C:\Users\xxx> cabal install random
Resolving dependencies...
Up to date
Warning: You asked to install executables, but there are no executables in
target: random. Perhaps you want to use --lib to install libraries instead.

And then I try to input like this

cabal install --lib random
Resolving dependencies...

and 

cabal install random --lib

both output Resolving dependencies... Up to date without warning. but ghci Could not find module `System.Random'

input cabal install random still having the same result with warning.

1 Answers1

7

It is a bad idea to install packages globally, so cabal install doesn't do that. The package is built and placed into the Cabal package database, but GHC won't find it unless you specifically tell Cabal to point GHC at it:

cabal repl -b random # -b is short for --build-depends
# Note that cabal install isn't really necessary: the above command would have installed random if it wasn't there already

I think your Cabal/GHC installation might be outdated, however. When you do cabal install --lib random, recent versions of Cabal should write out an "environment file" at %APPDATA%\ghc\arch-os-ghcversion\environments\default, which GHC should then automatically read (GHCi should say something like Loaded package environment from ...), and it should then find the installed package. If you are using the latest version of everything,

cabal install --lib random
ghci

should work.

HTNW
  • 27,182
  • 1
  • 32
  • 60
  • Thank you, it works for me. This is my first time using cabal. So what it did was telling GHC to download the package from cabal itself, If the package doesn't exist in cabal, then using install, do I correct? And is the meaning of "globally" means all the account in my machine can access it? And I think my cabal version was the latest, cabal-install version 3.2.0.0. Probably still having some unknown issue. –  Jun 04 '20 at 22:09
  • GHC doesn't know about the internet. Cabal has an online database of packages (called Hackage). `cabal install` downloads packages from Hackage, compiles them with GHC, and stores them into a local database. Cabal tells GHC is which files to compile and where the dependencies are. It can't tell it to download anything because GHC can't download anything. GHC cannot handle missing packages: it would just error. Cabal can detect if packages missing on your computer are on Hackage, and if it detects that, it will download the package, tell GHC to compile it, and move it into the package database. – HTNW Jun 04 '20 at 22:12
  • @ms "Global" does not mean everyone can access. "Global" means "picked up by every invocation of GHC". In particular, I believe there is a "system" global database (which has the packages GHC itself ships with) and a "user" global database. The reason it's bad is because GHC can't handle two versions of the same package, nor is there an easy way to uninstall packages, so you can never upgrade a global package and it's super easy to mess something up and have to delete everything. Cabal's database fixes this by only telling GHC about those packages it needs at any particular moment. – HTNW Jun 05 '20 at 02:04
  • The fact that GHC doesn't see your environment file is somewhat strange. Perhaps cabal doesn't generate them on Windows. (I did check GHC's manual and its code, both of which suggest it should be finding the file if it exists.) Perhaps that's grounds for a new question. – HTNW Jun 05 '20 at 02:06
  • I found some other problems today. It only work for the Prelude it opened. And the Prelude it opened can load the .hs file but can't find the function(Not in scope error). –  Jun 05 '20 at 17:35
  • maybe that Prelude run under cabal? –  Jun 05 '20 at 21:29