GHC does not see cabal package - haskell

This is my algorithm, which I have followed on my Mac to work with OpenGL.
$ cabal init sandbox
$ cabal install OpenGL
All done. They I import the module Graphics.Rendering.OpenGL and neither ghc or ghci see the module.
What have I done wrong?

As the comments say, when you install into a sandbox, you need to run ghc and ghci in the sandbox (i.e. via cabal commands like cabal repl or cabal build) to have access to the packages you have installed into a sandbox.

Related

What is the difference between install, v1-install, v2-install and new-install?

The commands install, v1-install, v2-install and new-install are simply described to "Install packages" when running man cabal. Are they different from one another? Which is the preferred option?
For modern versions of cabal-install, install, new-install and v2-install are the same. v1-install and the other v1- commands are obsolete and should not be used anymore.
Notice that, when developing a cabal package, the install command is largely unnecessary. Running cabal build and cabal repl will automatically install the required dependencies (listed in the build-depends: section of the cabal file).
cabal install is still useful in cases like the following:
Installing an executable from a package. Like the warp executable from the package wai-app-static. In those cases, the options --overwrite-policy, --install-method and --installdir are useful.
Creating a local package environment in some folder, so that "bare" invocations of ghc and ghci see some desired set of libraries. Like this. In those cases, the options --lib and --package-env are useful.

Can't install "System.Random" by cabal

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.
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.

Haskell Cabal v2 and Sandbox

We are told that this is now legacy mode of cabal, to manage a user-defined sandbox:
cabal init sandbox
cabal install <some stuff>
Which is later loaded at your discretion using
cabal exec bash
Question: How is an equivalent operation performed using the new implementation of Cabal?
The documentation is (as it currently stands) very cryptic with zero usage example. That would be helpful to facilitate migration.
Currently contemplating Cabal 2.4.0.0 with GHC 8.6.5.
There are no sandboxes. You can install packages and they end up in the user's cabal store using v2-install and v2-install --lib. Let's use cabal to install a few packages:
cabal v2-udpate
cabal v2-install --lib generic-trie containers
Now we can load modules from these packages in GHCi:
ghci
> import Data.GenericTrie
> import Data.Map
Why does this work? Because the cabal v2-install --lib modifies the default environment GHCi looks at to load packages. This file is found at .ghc/x86_64-linux-8.6.5/environments/default.
You can delete the environment file any time to start mostly fresh. This is handy if cabal is telling you some set of packages have conflicting dependencies with prior packages. Think of it as a global store but as just pointers to the nix-style builds, so it's cheap to blow away and redo.

ubuntu haskell ghci 7.4.1 could not find module "System.Random"

When I tried to load a .hs in ghci, it failed on the line:
import System.Random
Message:
Could not find module `System.Random'
Use -v to see a list of the files searched for.
I remember it worked yesterday and all of a sudden today it does not work.
This problem only occurs on Ubuntu 12.04 LTS, GHCi 7.4.1 , when i tried this on windows haskell platform and other linux machines i worked.
I tried to use
sudo apt-get remove ghc
sudo apt-get install ghc
to remove and re install ghc, but however the problem persists.
Could someone help me? Thanks
You must install the random package. You can probably get it from your package manager (as something like libghc-random-dev or similar), or you may cabal install random.
Edit in 2021 These days, cabal install is not the way. Instead, use cabal init to create a package and list random in your build-depends field. Compilation can be performed with cabal build (just build) or cabal run (to execute the result); interactive use is available via cabal repl.

How do you install packages/libraries without Cabal or Cabal-Install?

I'm trying to set up Haskell from scratch, on Ubuntu 11.04, without using the outdated Debian repository or Haskell-Platform.
I've installed GHC-7.0.4 from source with no problem, and now need to install Cabal (which appears to already be included in GHC in /usr/local/lib/ghc-7.0.4/Cabal-1.10.2.0) and Cabal Install.
The latter specifies several dependencies (parsec and network), each of which has several dependencies of their own (mtl, text, etc).
What's the command to install these packages, that I downloaded from hackage in tar.gz form?
Unpack, then runhaskell doesn't work.
I see Setup.lhs, but it's not clear what that's for or how to use it.
Most of the Haskell documentation I've found assumes you've installed from a repo or Haskell-Package and doesn't really explain this well.
cabal-install has a shell script that does this. If you download it from hackage and install it, you can start bootstrap.sh to install cabal-install. You can then use it to install other packages.
There are two different packages: Cabal and cabal-install. Cabal is a library, and cabal-install is an executable named cabal.
To install a package, cabal-install is an optional convenience wrapper around Cabal, but Cabal is required.
According to http://hackage.haskell.org/trac/ghc/wiki/Commentary/Libraries , Cabal is a 'zero-boot' package, so when you build GHC, Cabal and its dependencies are built for you automatically.
You can use ghc-pkg executable to check which packages are already installed:
# ghc-pkg list
Check if Cabal is in the list after you build GHC. If yes, you can install more packages without cabal-install using this documentation:
http://haskell.org/haskellwiki/Cabal/How_to_install_a_Cabal_package
I suggest you to install cabal-install first, and then install everything else using cabal-install executable. A usual commandine for global installation is this:
# runhaskell Setup configure
# runhaskell Setup build
# sudo runhaskell Setup install
Unpack a package tarball and run the commands in the folder with Setup.hs or Setup.lhs files. Note that a per-user non-root installation is also supported - Use runhaskell Setup configure --user
When you install cabal executable and its dependencies this way, use cabal install {package-name} to install more packages.
Note that Haskell Platform exists mostly because of the pain of installing cabal-install by yourself.

Resources